- Maki
- Apr 29, 2020
-
|
I've built myself a short python script to make uploading screenshots to lpix and including them in my posts easier.
How it works is, you write a bbcode post as normal, into a text file. Where you want to include images, you write a line of the form
The script then goes through the post, fetches the images from your harddrive, uploads them to lpix with the provided credentials, and replaces the line in the post with a bbcode img-tag linking to that lpix upload. You will need to edit in your username and password into the python file directly.
So, for example, you write
code:[b]My Update[/b]
Here is a pic:
#IMG testpic.png
and the script turns it into
code:[b]My Update[/b]
Here is a pic:
[img]<link>[/img]
no manual uploading required.
Usage is code:lptool.py [optional parameters] <filename>
. The optional parameters allow you to specify a gallery to upload to, the file into which the edited post should be written, the path to the image folder, and a prefix that should be prepended to the uploaded file names.
Python code:import requests
import argparse
url = "https://lpix.org/api"
parser = argparse.ArgumentParser(
prog="LP Tool", description="Parses LP script, uploads referenced media and inserts links")
parser.add_argument('infile')
parser.add_argument('--gallery', '-g', default="Default")
parser.add_argument('--outfile', '-o', default="out.txt")
parser.add_argument('--img_folder', '-i', default="imgs")
parser.add_argument('--prefix', '-p', default="")
args = parser.parse_args()
infile = open(args.infile, "r")
outfile = open(args.outfile, "w")
post_data = {
"username": "<username>",
"password": "<password>",
"gallery": args.gallery,
"output": "json",
}
for line in infile:
if line[0] == "#":
if line[1:5] == "IMG ":
imgfile = line[5:]
files = {'file': (args.prefix + imgfile, open(
f"{args.img_folder}/{imgfile}", 'rb'))}
post_response = requests.post(url, data=post_data, files=files)
if (not post_response.ok):
print(post_response.json())
exit(1)
else:
outfile.write(
f"[img]{post_response.json()['imageurl']}[/img]")
else:
outfile.write(line)
If you think this could be useful for you and try it out, please let me know if problems arise, this thing is a quick hack that works for me locally, I'm sure it contains horrible bugs that I didn't think about.
|
#
¿
Aug 1, 2023 11:24
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 16, 2024 08:43
|
|
- Maki
- Apr 29, 2020
-
|
Looks far more cleaner and professional than the old, janky code I wrote for myself a long time back that I'm still using. Also handles the upload process simultaneously too, which is nice, but does conflict with my current workflow a bit.
I'll give it a test if I can find the time.
For reference, can you maybe detail your workflow a bit? I'm winging it as I go, so I'm both curious how I can improve my workflow and how I can improve the script to handle more people's needs.
|
#
¿
Aug 1, 2023 17:22
|
|