Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
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

code:
#IMG <filename>
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.

Adbot
ADBOT LOVES YOU

Maki
Apr 29, 2020

Odd Wilson posted:

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply