-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit_fetch_domain_to_text.py
62 lines (47 loc) · 1.95 KB
/
reddit_fetch_domain_to_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from datetime import datetime
import praw
# Constants
CONFIG_DIR = os.path.join(os.path.expanduser("~"), "AlteredAdmin_Reddit_Tools")
CONFIG_FILE = os.path.join(CONFIG_DIR, "reddit_fetch_domain_to_text_Configs.txt")
def read_config():
with open(CONFIG_FILE, "r") as f:
lines = f.readlines()
client_id = lines[0].strip()
client_secret = lines[1].strip()
return client_id, client_secret
def write_config(client_id, client_secret):
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
with open(CONFIG_FILE, "w") as f:
f.write(client_id + "\n")
f.write(client_secret + "\n")
def get_api_config():
if not os.path.exists(CONFIG_FILE):
print("Configuration file not found.")
client_id = input("Enter your Reddit Client ID: ")
client_secret = input("Enter your Reddit Client Secret: ")
write_config(client_id, client_secret)
else:
client_id, client_secret = read_config()
return client_id, client_secret
def main():
client_id, client_secret = get_api_config()
USER_AGENT = 'Reddit Domain Reaper'
domain = input("Enter the domain you want to fetch posts from (e.g. imgur.com): ")
try:
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret,
user_agent=USER_AGENT)
# Fetch and display submissions
for submission in reddit.domain(domain).new():
title = submission.title
created_time = datetime.fromtimestamp(submission.created)
url = submission.url
reddit_link = submission.permalink
print(
f"Title: {title}\nTime: {created_time}\nURL: {url}\nReddit Link: https://www.reddit.com{reddit_link}\n{'-' * 40}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()