-
Notifications
You must be signed in to change notification settings - Fork 32
/
joke.py
55 lines (41 loc) · 1.64 KB
/
joke.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
import logging
import re
from typing import Optional
from services.reddit import RedditService
# Constants
README_FILE = "README.md"
IMAGE_EXTENSIONS_PATTERN = re.compile(r"\.(jpg|jpeg|png)$", re.IGNORECASE)
# Configure logging
logging.basicConfig(level=logging.INFO)
def fetch_random_meme() -> Optional[dict]:
"""Get a random meme data from Reddit API."""
return RedditService.get_random_meme()
def update_readme_with_meme(meme: dict) -> bool:
"""Update README with meme data, returning whether successful."""
try:
with open(README_FILE, "r") as file:
contents = file.readlines()
for i, line in enumerate(contents):
if "![Funny Meme]" in line:
# Remove the previous author line
if i + 1 < len(contents) and contents[i + 1].startswith("* Meme Author:"):
del contents[i + 1]
meme_url = meme["url"]
meme_author = meme["author"]
markdown = f"![Funny Meme]({meme_url}?width=100&height=100)\n"
markdown += f"* Meme Author: [{meme_author}](https://www.reddit.com/user/{meme_author}/)\n"
contents[i] = markdown
break
with open(README_FILE, "w") as file:
file.writelines(contents)
return True
except (IOError, FileNotFoundError) as e:
logging.error(f"An error occurred: {e}")
return False
def main() -> None:
"""Update README with a new meme."""
meme = fetch_random_meme()
if meme and IMAGE_EXTENSIONS_PATTERN.search(meme["url"]):
update_readme_with_meme(meme)
if __name__ == "__main__":
main()