Skip to content

Instantly share code, notes, and snippets.

@gammaglitch
Created January 25, 2023 19:05
Show Gist options
  • Select an option

  • Save gammaglitch/515a2fecc5f11ca2e2d66ec593dfb054 to your computer and use it in GitHub Desktop.

Select an option

Save gammaglitch/515a2fecc5f11ca2e2d66ec593dfb054 to your computer and use it in GitHub Desktop.
This python script scrapes the saved posts of multiple accounts. It also generates a table of contents to use with Obsidian.
import praw
import json
from dotenv import load_dotenv
import os
load_dotenv()
user_agent='python_saved'
with open('config.json') as f:
config = json.load(f)
posts = []
for account in config:
reddit = praw.Reddit(client_id=account['client_id'],
client_secret=account['client_secret'],
username=account['username'],
password=account['password'],
user_agent=user_agent)
account_posts = reddit.user.me().saved(limit=None)
for post in account_posts:
posts.append(post)
with open('saved.md', 'w') as f:
subreddit_posts = {}
for item in posts:
if isinstance(item, praw.models.Submission):
subreddit = item.subreddit.display_name
if subreddit not in subreddit_posts:
subreddit_posts[subreddit] = []
subreddit_posts[subreddit].append(item)
f.write("## Table of Contents\n\n")
for subreddit, post_list in subreddit_posts.items():
f.write("- [[#{}]]\n".format(subreddit))
for item in post_list:
f.write(" - [[#{} ({})]]\n".format(item.title, item.id))
for subreddit, post_list in subreddit_posts.items():
f.write("## " + subreddit + '\n\n')
for item in post_list:
f.write("### {} ({})\n\n".format(item.title, item.id))
if item.is_self:
f.write(item.url + '\n\n')
selftext = item.selftext.split("\n")
for line in selftext:
f.write("> " + line + '\n')
else:
f.write(item.url + '\n')
f.write("\n---\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment