· 6 years ago · Oct 29, 2019, 11:50 AM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import pathlib, urllib, time, os, datetime
4try:
5 import tweepy
6except:
7 print("You don't have tweepy installed")
8 raise SystemExit
9
10directory = "/home/fantoro"
11key = ""
12secret = ""
13
14last_tweet_id = ""
15
16def get_mins_to_nearest_post():
17 dt = datetime.datetime.now()
18 if(dt.minute >= 50):
19 return 60-dt.minute
20 nearest_minute = dt.minute
21 while (nearest_minute%10) != 0:
22 nearest_minute += 1
23 return nearest_minute-dt.minute
24
25def on_status(status,ltid):
26 if(ltid == status.id):
27 return ltid
28 if (status.user.id == "2907774137"):
29 return ltid
30 print("New Archillect tweet")
31 tweet_entities = status.entities
32 if not ("media" in tweet_entities):
33 return ltid
34 print("Tweet contains media")
35 media_entity = tweet_entities["media"][0]
36 if not (media_entity["type"] == "photo"):
37 return ltid
38 print("Included media is a photo, downloading")
39 response = urllib.request.urlopen(media_entity["media_url"])
40 data = response.read()
41 f = open("%s/wallpaper.new" % directory,"wb")
42 f.write(data)
43 f.close()
44 os.system('''qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '
45 var allDesktops = desktops();
46 print (allDesktops);
47 for (i=0;i<allDesktops.length;i++) {{
48 d = allDesktops[i];
49 d.wallpaperPlugin = "org.kde.image";
50 d.currentConfigGroup = Array("Wallpaper",
51 "org.kde.image",
52 "General");
53 d.writeConfig("Image", "file://%s/wallpaper.new")
54 }}
55 '
56 ''' % directory)
57 os.replace("%s/wallpaper.new" % directory,"%s/wallpaper" % directory)
58 os.system('''qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '
59 var allDesktops = desktops();
60 print (allDesktops);
61 for (i=0;i<allDesktops.length;i++) {{
62 d = allDesktops[i];
63 d.wallpaperPlugin = "org.kde.image";
64 d.currentConfigGroup = Array("Wallpaper",
65 "org.kde.image",
66 "General");
67 d.writeConfig("Image", "file://%s/wallpaper")
68 }}
69 '
70 ''' % directory)
71
72 print("Done")
73 return status.id
74
75print("Authenticating")
76
77try:
78 auth = tweepy.AppAuthHandler(key,secret)
79except Exception as e:
80 print(e)
81 raise SystemExit
82
83print("Creating API object")
84
85api = tweepy.API(auth)
86
87while True:
88 print("Fetching latest tweet")
89 tweets = api.user_timeline(id="2907774137",count=1)
90 last_tweet_id = on_status(tweets[0], last_tweet_id)
91 mins = get_mins_to_nearest_post()
92 print(mins)
93 time.sleep(mins*60)