-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunsplashubuntu.py
More file actions
executable file
·85 lines (74 loc) · 2.11 KB
/
unsplashubuntu.py
File metadata and controls
executable file
·85 lines (74 loc) · 2.11 KB
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
#!/usr/bin/env python
#packages
import wget
import os
import Tkinter as tk
import time
import threading
import urllib2
#some headers
__author__ = "Ajay Krishna Teja Kavuri"
__email__ = "ajaykrishnateja@gmail.com"
#The main class that initiates the url and screen parameters
class UnsplashUbuntu(object):
def __init__(self, interval):
try:
print "Initializing"
self.baseUrl = "https://source.unsplash.com/random"
self.myScreen = tk.Tk()
self.screen_width = self.myScreen.winfo_screenwidth()
self.screen_height = self.myScreen.winfo_screenheight()
self.res = "/"+str(self.screen_width)+"x"+str(self.screen_height)
self.cwd = os.getcwd()
self.filename=str(self.screen_width)+"x"+str(self.screen_height)
self.interval = interval
wpThread = threading.Thread(target=self.run)
wpThread.daemon = True
wpThread.setName('UnsplashUbuntu')
wpThread.start()
wpThread.join()
except:
print "Exception"
#Obtain the baserurl and concat the string
def getWallpaper(self):
try:
self.fullUrl = self.baseUrl + self.res
self.filename = wget.download(self.fullUrl)
except:
print "Exception"
#Run the command to change the wallpaper
def setWallpaper(self):
try:
self.setCmd = "gsettings set org.gnome.desktop.background picture-uri file:///"+self.cwd+"/"+self.filename
os.system(self.setCmd)
except:
print "Exception"
#Remove the previous wallpaper before setting/downloading new one
def removeWallpaper(self):
self.filePath = self.cwd+"/"+self.filename
if os.path.exists(self.filePath):
os.remove(self.filePath)
#Check for the internet connection
def chkNetwork(self):
try:
res=urllib2.urlopen('http://wvu.edu',timeout=1)
return True
except urllib2.URLError as err:
pass
return False
#The main logic implementation
def run(self):
while True:
try:
if self.chkNetwork():
print("Daemon Running....")
self.removeWallpaper()
self.getWallpaper()
self.setWallpaper()
time.sleep(self.interval)
else:
time.sleep(self.interval/60)
except:
print "Exception"
#Initialize and call the class
thisWallpaper = UnsplashUbuntu(3600)