generated from jacobtomlinson/python-container-action
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
57 lines (48 loc) · 1.71 KB
/
main.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
import feedparser
import random
import os
import sys
import json
SUB_URL = "https://www.reddit.com/r/ProgrammerHumor"
FALLBACK = {
"meme_link": "https://raw.githubusercontent.com/Bhupesh-V/memer-action/master/images/header.png",
"title": "Oops :( looks like we are out of memes.",
"src": "https://github.com/Bhupesh-V/memer-action",
}
def getMeme(filter_posts="hot"):
memelist = []
memedict = {}
f = feedparser.parse(f"{SUB_URL}/{filter_posts}.rss")
for entry in f.entries:
post_content = entry["content"][0]["value"]
img = post_content[
post_content.find("https://i.redd.it") : post_content.find("link") - 3
]
if img != "":
memedict["title"] = entry["title"]
memedict["src"] = str(entry["link"])
memedict["meme_link"] = img
memelist.append(memedict)
if len(memelist) != 0:
random.shuffle(memelist)
# elif os.environ["INPUT_FALLBACK"]:
# fallback_dict = json.loads(os.environ["INPUT_FALLBACK"])
# if FALLBACK.keys() == fallback_dict.keys():
# memelist.append(fallback_dict)
# else:
# print("Key Error, make sure keys are", FALLBACK.keys())
# sys.exit(0)
else:
memelist.append(FALLBACK)
return memelist[0]
def main():
filter_by = os.environ["INPUT_FILTER"]
if filter_by not in ["hot", "top", "new", "rising"]:
print("filter must be one of hot, top, new or rising")
sys.exit(0)
meme = getMeme(filter_by)
print(f"::set-output name=meme::{meme['meme_link']}")
print(f"::set-output name=title::{meme['title']}")
print(f"::set-output name=source::{meme['src']}")
if __name__ == "__main__":
main()