Skip to content

Instantly share code, notes, and snippets.

@neelsomani
Created March 24, 2025 07:21
Show Gist options
  • Save neelsomani/3f4eb602175b6c25074cbda49c702b2c to your computer and use it in GitHub Desktop.
Save neelsomani/3f4eb602175b6c25074cbda49c702b2c to your computer and use it in GitHub Desktop.
Reddit Front Page Generator Script
import requests
from bs4 import BeautifulSoup
import random
import os
import json
def get_popular_subreddits():
base_url = "https://www.reddit.com/best/communities/"
subreddits = []
# Loop through pages 1 to 10
for page_num in range(1, 11):
url = f"{base_url}{page_num}/"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to retrieve page {page_num}: {response.status_code}")
continue
soup = BeautifulSoup(response.text, 'html.parser')
# Find subreddit links in the page
links = soup.find_all("a", class_="m-0")
for link in links:
href = link.get("href")
if href and "/r/" in href:
subreddit = href.split("/r/")[1].strip("/")
subreddits.append(subreddit)
return subreddits
def save_subreddits_to_disk(subreddits, filepath="subreddits.json"):
with open(filepath, "w") as file:
json.dump(subreddits, file)
def load_subreddits_from_disk(filepath="subreddits.json"):
if os.path.exists(filepath):
with open(filepath, "r") as file:
return json.load(file)
return None
def generate_random_subreddit_link(subreddits, count=5):
if len(subreddits) < count:
count = len(subreddits)
random_subreddits = random.sample(subreddits, count)
return f"https://www.reddit.com/r/{'+'.join(random_subreddits)}"
if __name__ == "__main__":
print("Checking for stored subreddits...")
popular_subreddits = load_subreddits_from_disk()
if not popular_subreddits:
print("No stored subreddits found. Fetching popular subreddits...")
popular_subreddits = get_popular_subreddits()
if popular_subreddits:
print(f"Fetched {len(popular_subreddits)} subreddits. Saving to disk...")
save_subreddits_to_disk(popular_subreddits)
else:
print("Failed to fetch any subreddits. Exiting.")
exit()
else:
print(f"Loaded {len(popular_subreddits)} subreddits from disk.")
link = generate_random_subreddit_link(popular_subreddits)
print(f"Random subreddit link: {link}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment