Skip to content

Instantly share code, notes, and snippets.

@Hans5958
Last active August 1, 2023 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hans5958/3cb723d8c423ddf50a3efb79d45b46b9 to your computer and use it in GitHub Desktop.
Save Hans5958/3cb723d8c423ddf50a3efb79d45b46b9 to your computer and use it in GitHub Desktop.
A simple Reddit crossposter script with Python, using gallery-dl to fetch the posts. Example result: https://www.reddit.com/r/GlitchProductions/comments/15f72v0/glitch_prod_on_ig_what_they_looking_at/
import os
import json
import praw
import subprocess
# Run gallery-dl, get first 10 since a gallery can have at least 10
# subprocess.run(["gallery-dl", "--range", "1-10", "--write-metadata", "-D", "fetch", "https://instagram.com/glitch_prod"])
print("Done fetching")
# Get the last metadata by name
fetch_dir = os.path.join(os.getcwd(), "fetch")
json_files = [f for f in os.listdir(fetch_dir) if f.endswith(".json")]
json_files.sort()
# Read the latest ID that the previous run do
prev_id = ""
if os.path.exists(os.path.join("prev_id.txt")):
with open(os.path.join("prev_id.txt"), "r") as f:
prev_id = f.read()
# Get the position of the previous ID
ids = [os.path.basename(f).split(".")[0] for f in json_files]
if prev_id == "":
# If there aren't, just skip to the latest one and upload that
prev_id_index = len(json_files) - 1
prev_id = ids[prev_id_index]
else:
prev_id_index = ids.index(prev_id)
# If prev_id is the last one, then we're done. Otherwise, we need to upload the next one.
if prev_id_index == len(json_files) - 1:
print("No new media")
exit()
prev_id = ids[prev_id_index + 1]
# Write the new latest ID to the file
with open(os.path.join("prev_id.txt"), "w") as f:
f.write(prev_id)
json_file = json_files[prev_id_index]
# Read the metadata
with open(os.path.join(fetch_dir, json_file), "r", encoding='utf-8') as f:
data = json.loads(f.read())
print(data['media_id'])
print(data['description'])
print(data['date'])
print(data['category'])
category_abbreviations = {
"instagram": "IG",
"twitter": "TW",
"youtube": "YT",
}
# Build what is going to be submitted
submission_title = f"[@{data['username']} on {category_abbreviations[data['category']]}] {data['description']}"
submission_title = " ".join(submission_title.split())
media_file = json_file.replace(".json", "")
# exit()
# Submit to Reddit using praw
reddit = praw.Reddit("auto5958")
subreddit = reddit.subreddit("GlitchProductions")
if media_file.endswith(".mp4"):
subreddit.submit_video(title=submission_title, video_path=os.path.join(fetch_dir, media_file))
else:
subreddit.submit_image(title=submission_title, image_path=os.path.join(fetch_dir, media_file))
[auto5958]
client_id=placeholder_w30ATBmC5Q
client_secret=placeholder_g621aJPeR3xCcwZ9Md
user_agent=Auto5958 (https://gist.github.com/Hans5958/3cb723d8c423ddf50a3efb79d45b46b9)
username=Hans5958_
password=my_super_secret_password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment