-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
254 lines (208 loc) · 8.53 KB
/
script.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import praw
import urllib.request
import ctypes
import sys
import random
import os
import subprocess
def printHelp():
print('--------------------------------------------------------------------')
print('Help Section:')
print('Proper Usage: python script.py <arguments>')
print('\t[-limit] [-help] [change_to] [random]')
print(' -limit <int> :\tChanges the limit to the number upto which posts of each subreddit is checked. The default is 100.')
print(' -help :\tDisplays this Section.')
print(' change_to <int between 1 and limit (default = 100)> :\tChanges the backgroud to that rank if arranged on the basis of upvotes.')
print(' random :\tChanges the background to a random image from the select pool upto limit.')
print('\n\n\tIn case of conflicts the latter one will supercede the former.')
def setUp(usrnm, passwd, cl_id, cl_sc):
print('Getting Reddit Data...')
reddit = praw.Reddit(client_id=cl_id, client_secret=cl_sc, user_agent='reddit_back', username=usrnm, password=passwd)
return reddit
def firstTimeLinux():
file_path = '/home/' + os.environ.get('USER') +'/Pictures/'
file = open(file_path + "_rb.dat", "w")
file.write("Ran" + "\n")
print('Write every input inside quotes')
client_id = input('Please enter your Client ID:\t')
client_secret = input('Please enter your Client Secret:\t')
username = input('Please enter your Reddit Username:\t')
password = input('Please enter your Reddit Password:\t')
file.write(client_id + "\n")
file.write(client_secret + "\n")
file.write(username + "\n")
file.write(password + "\n")
file.close()
def swapper(str, ind, rep_ch):
return (str[0:ind] + rep_ch + str[ind+1:len(str)])
def nameCorrectorLinux(name):
# Corrects file name in line with ubuntu file naming rules
if len(name) > 255:
name = name[0:254]
for i in range(0,len(name)-1):
if name[i] == '/':
swapper(name, i, '-')
return name
def nameCorrectorWindows(name):
# Corrects file name in line with Windows file naming rules
if len(name) > 255:
name = name[0:254]
for i in range(0, len(name)-1):
if name[i] in ['/', '\\']:
swapper(name, i, '-')
if name[i] == '<':
swapper(name, i, '[')
if name[i] == '>':
swapper(name, i, ']')
if name[i] in [':', '|', '?']:
swapper(name, i, '_')
if name[i] == '\"':
swapper(name, i, '\'')
if name[i] == '*':
swapper(name, i, 'x')
return name
def changeBack(lim , proirind, reddit):
walllist = []
wallups = []
wallsubs = ['ultrahdwallpapers', 'WQHD_Wallpaper']
reddit.read_only = True
for wallsub in wallsubs:
subreddit = reddit.subreddit(wallsub)
for submission in subreddit.hot(limit = lim):
if not submission.stickied:
upvotes = submission.ups
wallups.append(upvotes)
walllist.append(upvotes)
walllist.append(submission.url)
walllist.append(submission.title+'.'+submission.url.split('.')[-1])
proirind = -proirind
urltemp = ''
chkind = -1
wallups.sort()
for i in walllist:
chkind+=1
if i == wallups[proirind]:
urltemp = walllist[chkind+1]
break
name = walllist[chkind+2]
# Checking for target machine to be a ubuntu desktop
if os.environ.get("DESKTOP_SESSION") in ["ubuntu", "gnome", "unity"]:
# Correcting the file names
name = nameCorrectorLinux(name)
# Downloading the file
print('Downloading... ' + name)
file_path = '/home/' + os.environ.get('USER') +'/Pictures/'
# Making the directory if it doesn't already exist
if not os.path.exists(file_path):
os.makedirs(file_path)
urllib.request.urlretrieve(urltemp,filename = (file_path + name))
print('Download Complete.\nSetting this as Wallpaper...')
# This part adds background support for ubuntu machines
command = 'gsettings set org.gnome.desktop.background picture-uri file://' + file_path + '\"' + name+ '\"'
status, output = subprocess.getstatusoutput(command)
if status == 0:
print('Wallpaper Set')
else:
print('Some error occured. Please Try Again; if the issue persists, pray')
# Checking for target machine to be a Windows desktop
if sys.platform in ["win32", "cygwin"]:
# Correcting the file names
name = nameCorrectorWindows(name)
# Downloading the file
print('Downloading...', name)
file_path = os.path.expanduser('~\\Pictures') +'\\Pictures\\'
# Making the directory if it doesn't already exist
if not os.path.exists(file_path):
os.makedirs(file_path)
urllib.request.urlretrieve(urltemp,filename = (file_path + name))
print('Download Complete.\nSetting this as Wallpaper...')
# This part adds back ground support for Windows machines
SPI_SETDESKWALLPAPER = 20
SPIF_UPDATEINFILE = 1
command = file_path + name
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, command , SPIF_UPDATEINFILE)
print('Wallpaper Set.')
# Checking for target machine to be Mate
if os.environ.get("DESKTOP_SESSION") == "mate":
# Correcting the file names
name = nameCorrectorLinux(name)
# Downloading th file
print('Downloading... ' + name)
file_path = '/home/' + os.environ.get('USER') +'/Pictures/'
# Making the directory if it doesn't already exist
if not os.path.exists(file_path):
os.makedirs(file_path)
urllib.request.urlretrieve(urltemp,filename = (file_path + name))
print('Download Complete.\nSetting this as Wallpaper...')
# This part adds background support for Mate machines
file_path += '\"' + name+ '\"'
command = 'mateconftool-2 -t string -s /desktop/mate/background/picture_filename ls' + file_path + ' | shuf -n1'
status, output = subprocess.getstatusoutput(command)
if status == 0:
print('Wallpaper Set')
else:
print('Some error occured. Please Try Again; if the issue persists, pray')
# Checking for the target machine to be Cinnamon
if os.environ.get("DESKTOP_SESSION") == "cinnamon":
# Correcting the file names
name = nameCorrectorLinux(name)
# Downloading th file
print('Downloading... ' + name)
file_path = '/home/' + os.environ.get('USER') +'/Pictures/'
# Making the directory if it doesn't already exist
if not os.path.exists(file_path):
os.makedirs(file_path)
urllib.request.urlretrieve(urltemp,filename = (file_path + name))
print('Download Complete.\nSetting this as Wallpaper...')
# This part adds background support for Cinnamon machines
file_path += '\"' + name+ '\"'
command = 'gsettings set org.cinnamon.desktop.background picture-uri file://' + file_path
status, output = subprocess.getstatusoutput(command)
if status == 0:
print('Wallpaper Set')
else:
print('Some error occured. Please Try Again; if the issue persists, pray')
if __name__ == "__main__":
print('Welcome to Reddit-Background. Use -help for list of Arguments.')
temp_limit = 100
temp_proirind = 1
arg_counter = -1
if len(sys.argv) < 2:
print('Please provide proper argument(s). Use -help to see the list of Arguments. Defaulting to the highest upvoted post.')
for argument in sys.argv:
arg_counter += 1
if argument == '-limit':
temp_limit = int(sys.argv[arg_counter+1])
if argument == '-help':
printHelp()
input()
sys.exit()
if argument == 'change_to':
temp_proirind = int(sys.argv[arg_counter+1])
if argument == 'random':
temp_proirind = random.randrange(1, temp_limit, 1)
#Will add the option to add multis and subreddtis
file_path = '/home/' + os.environ.get('USER') +'/Pictures/'
client_id = ''
client_secret = ''
username = ''
password = ''
if os.path.isfile(file_path + "_rb.dat"):
# If the file exists, meaning it has already been set up
file = open(file_path + "_rb.dat", "r")
lines = file.readlines()
client_id = lines[1].strip()
client_secret = lines[2].strip()
username = lines[3].strip()
password = lines[4].strip()
file.close()
# Running the commands:
reddit = setUp(username, password, client_id, client_secret)
changeBack(temp_limit, temp_proirind, reddit)
else:
print("This is an Wallpaper Changer Application that uses Reddit!\n")
print("To use this application you require a reddit account, you can create one at https://www.reddit.com/register")
print("or if you already have one, log in at https://.reddit.com/login and add \"reddit_back\" as a script.")
if os.environ.get("DESKTOP_SESSION") in ["ubuntu", "gnome", "unity", "mate", "cinnamon"]:
firstTimeLinux()
print('Reddit-Background is all Set up, run it again and enjoy!')