-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchUrl.py
More file actions
executable file
·102 lines (78 loc) · 2.68 KB
/
fetchUrl.py
File metadata and controls
executable file
·102 lines (78 loc) · 2.68 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Fetch the data with the constructed URL
@author Ian Hiew(pixdo.et@gmail.com)
"""
import requests
import json
FEED_URL = "https://old.reddit.com/r/"
class Construct:
def __init__(self):
pass
def customConstruct(self, url):
"""
not sure why this is here... not referenced anywhere in the code
customConstruct - construct a custom feed url
:param url: url to construct into feed
"""
# for custom feeds
try:
f = open(url)
data = json.load(f)
url = FEED_URL
for subreddit in data["subreddits"]:
url = f"{url}+{subreddit}"
return f"{url}.json"
except IOError as ioe:
print(ioe)
def constructUrl(self, override, listName):
"""
constructUrl - constructs the feed url
:param override: override subreddits.json with a custom url string
"""
# check for custom url override
if override is None:
# check if is using a custom feed
if listName is None:
with open("subreddits.json", "r") as f:
data = json.load(f)
else:
with open(listName, "r") as f:
data = json.load(f)
url = FEED_URL
for subreddit in data["subreddits"]:
# append the subreddit into the constructed url
url = f"{url}+{subreddit}"
return f"{url}.json"
else:
# custom subreddit feed
url = f"{FEED_URL}{override}.json"
return url
def postListName(self):
with open("subreddits.json", "r") as f:
data = json.load(f)
return data["name"]
construct = Construct()
class Fetch:
def __init__(self):
pass
def fetch(self, override, listOverride):
"""
fetch - fetches the data from the constructed url
:param override: the subreddit(s) to override the request on (subreddit1+subreddit2)
"""
if override is None:
CONSTRUCTED_URL = construct.constructUrl(None, None)
else:
if listOverride is not None: # tf lmaoooooo
CONSTRUCTED_URL = construct.constructUrl(override, listOverride)
else:
CONSTRUCTED_URL = construct.constructUrl(override, None)
r = requests.get(
CONSTRUCTED_URL,
headers={
"Accept": "application/json",
# usefirefox
"User-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:100.0) Gecko/20100101 Firefox/100.0",
},
)
return r.json()