-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
81 lines (60 loc) · 2.02 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! python wrapper/wrap.py
"""
A Reddit Bot that gets top Hot sorted picture from a given
Subreddit and sets it as background.
"""
import configparser
import logging
import pathlib
import sys
import requests
import bot
config = configparser.RawConfigParser()
config.read("etc/config.ini")
log_config = config["Logging"]
logging.basicConfig(
filename=log_config["filename"],
filemode=log_config["filemode"],
level=int(log_config["level"]),
format=log_config["format"]
)
logger = logging.getLogger(__name__)
def main():
logger.debug("Start of Program.")
print("""
+---------------------------+
| W A L L P A P E R B.O.T |
+---------------------------+
""")
# Create reddit object
login_config = config["Login"]
reddit = bot.get_reddit(login=login_config)
subreddit_name = input("Enter the subreddit you want to access: ")
subreddit = bot.get_subreddit(reddit=reddit, name=subreddit_name)
# Get picture submission based on 'Hot' sort
picture = bot.get_picture_post(subreddit_sort=subreddit.hot())
print(f"Title: {picture.title}")
print(f"Author: {picture.author.name}\n")
print("Downloading...")
try:
r = requests.get(url=picture.url)
r.raise_for_status()
logger.info("Downloading Picture")
logger.debug(f"Getting request from picture {r}")
except requests.RequestException as e:
print(f"Error: Connection broken; couldn't download image. Please try again.")
logger.exception(f"{e}")
logger.critical("Couldn't download image")
sys.exit()
print("Done.\n")
# Getting image extension from url
image_ext = pathlib.Path(picture.url).suffix
# Getting picture path
image_filename = picture.author.name + image_ext
image_path = pathlib.Path(".").resolve() /"images"/ image_filename
bot.save_image(path=image_path, image=r.content)
bot.set_image_background(image_path=str(image_path))
print("Exiting..\n")
logger.debug("End of program")
if __name__ == "__main__":
main()