-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_memes.py
52 lines (44 loc) · 1.73 KB
/
get_memes.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
import os
import datetime
import requests
valid_endings = (".jpg", ".png", ".jpeg")
def get_memes(date, limit):
# Get 50-ish hot memes from r/dankmemes
payload = {"limit": limit}
headers = {"User-Agent": "python:meme-stealer:v0.0.2 (by /u/altarrel)"}
r = requests.get("https://www.reddit.com/r/dankmemes/hot.json", params=payload, headers=headers)
if r.status_code != requests.codes.ok:
print("Something went wrong with the request.")
print(r.status_code)
print(r.headers)
r.raise_for_status()
exit()
img_urls = []
for post in r.json()["data"]["children"]:
if "url" not in post["data"]:
print("Missing url")
continue
if any(post["data"]["url"].endswith(x) for x in valid_endings):
img_urls.append(post["data"]["url"])
print("Added {} to img_urls".format(post["data"]["url"]))
# Make sure the directories we will use exist
if not os.path.exists("./images"):
os.makedirs("./images")
os.makedirs("./images/{}".format(date))
if not os.path.exists("./images/{}".format(date)):
os.makedirs("./images/{}".format(date))
else:
print("Found previous meme folder for today, emptying it.")
files = os.listdir("./images/{}".format(date))
for file_name in files:
os.remove("./images/{}/{}".format(date, file_name))
i = 0
for url in img_urls:
extension = url[url.rfind("."):]
img = requests.get(url)
path = "images/{}/{}{}".format(date, str(i), extension)
with open(path, "wb") as f:
f.write(img.content)
print("Downloaded {}".format(url))
i += 1
return "./images/{}".format(date)