-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Post.py
More file actions
40 lines (34 loc) · 1.26 KB
/
Random Post.py
File metadata and controls
40 lines (34 loc) · 1.26 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
import datetime
import json
import random
import requests
import time
def meme(sub: str = None):
sub = str(input("What's the name of the sub?\n"))
start = datetime.datetime.now()
print("\nConnecting...\n")
url = f'https://www.reddit.com/r/{sub}/top.json?sort=top&t=week&limit=100'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/41.0.2228.0 Safari/537.36'}
r = requests.get(url, headers=headers)
if r.status_code != 200:
print("Failed D:")
time.sleep(3)
exit()
print("Connected!\n")
response = r.json()
i = random.randint(0, 99)
print(f'Selected Post: {i}\n')
title = response['data']['children'][i]['data']['title']
source = response['data']['children'][i]['data']['url']
if source.__contains__("v.redd.it") or source.__contains__("gifv") or source.__contains__("gfycat") \
or not source.__contains__(""):
print(f"{title}\n{source}")
else:
print(title, source, sep=" - ")
end = datetime.datetime.now()
total_time = (end - start).total_seconds()
print(f"\nTotal time taken = {total_time}")
input("Exit?")
if __name__ == '__main__':
meme()