-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_reddit_wallpaper.py
executable file
·89 lines (75 loc) · 3.27 KB
/
get_reddit_wallpaper.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
82
83
84
85
86
87
88
#!/usr/bin/env python
# coding: utf-8
from bs4 import BeautifulSoup
import requests
import re
import os
import argparse
# Parse arguments into script
parser = argparse.ArgumentParser(description="Get Wallpaper from subreddit wallpaper.")
parser.add_argument( "--category",
type=str,
default = "top",
help = "Category: [hot | top | new | controversial | rising]")
parser.add_argument( "--directory",
help = "Path to directory in which images should be stored")
parser.add_argument( "--change_wallpaper",
default= "True",
help = "Change Wallpaper? ['True' | 'False']")
parser.add_argument( "--subreddit",
default= "wallpapers",
help = "Subreddit to download top image from")
args = parser.parse_args()
subreddit = "https://www.reddit.com/r/%s" % (args.subreddit)
headers = {'User-Agent': 'Mozilla/5.0'}
if args.directory is not None:
directory = os.path.abspath(args.directory)
else:
directory = os.popen("pwd").read().strip()
class get_wallpaper():
def __init__(self, url):
self.url = url
def get_href(self, page_link):
# get html from website
page_response = requests.get(page_link, headers=headers)
page_content = BeautifulSoup(page_response.text, "html.parser")
posts = page_content.find("div", {"class": "_3JgI-GOrkmyIeDeyzXdyUD _2CSlKHjH7lsjx0IpjORx14"})
img = posts.find("img")
href = posts.find("a").attrs["href"]
self.href = "%s/%s" % ("https://www.reddit.com", href)
attrs = img.attrs
src = attrs["src"]
self.src = src
def get_image(self, href):
next_page = requests.get(href, headers=headers)
next_content = BeautifulSoup(next_page.text, "html.parser")
# find image url
img_a = next_content.find("div", {"class": "media-preview-content"}).find("a")
self.img_href = img_a.attrs["href"]
self.title = next_content.find("a", {"class": "title"}).text.replace("/", "_").replace("?", "_").replace('"',
"_")
def download_image(self, title, img_href, pwd):
format_img = re.search("^.*(\..*)$", img_href).group(1)
title = (title + format_img)
self.path = ("%s/%s" % (pwd, title))
print("Downloaded %s and saved it in %s" % (title, self.path))
for cat in args.category.strip().split(","):
link = "%s/%s/" % (subreddit, cat)
print("Download image from: %s" % (link))
post = get_wallpaper(link)
post.get_href(link)
post.get_image(post.href)
post.download_image(post.title, post.img_href, directory)
full_name = "file://%s" % (post.path)
# create dir if it does not exist
if not os.path.exists(directory):
os.mkdir(directory)
# download image
if not os.path.exists(post.path):
f = open(post.path,'wb')
f.write(requests.get(post.img_href).content)
f.close()
# change wallpaper to downloaded image
if args.change_wallpaper == "True":
os.system("gsettings set org.gnome.desktop.background picture-uri '%s'" % (full_name))
print("Changed wallpaper to: %s" % (post.path))