forked from BernieSumption/tweeting-catflap
/
main.py
75 lines (57 loc) · 1.96 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
from subprocess import call
from datetime import datetime
from grammar import Grammar
import os, shutil
from twython import Twython
import sys
try:
import settings
except ImportError:
print "Could not import settings module, did you create one?"
exit()
try:
from gpio_watcher import GPIOWatcher
except ImportError:
print "Import GPIOWatcher failed, the script will only work in test mode"
def goGoPaparazzo():
# get a pretty date time string
timestamp = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
hour = datetime.now().hour
#if hour < 8 or hour > 17:
# print "Yawn, I'm asleep. Wake me up when it's daytime"
# return
print "Activating Paparazzo at", timestamp
grammar = Grammar.from_file("grammar.txt")
while True:
message = grammar.generate()
if len(message) < 252: # allow space for picture URL
break
print "Message:", message
# capture image to capture.jpg
call(["./capture-image.sh"], shell=True)
# stop here if image capture failed
if not os.path.exists("capture.jpg"):
print "Error - no capture.jpg recorded"
return
# post to twitter
twitter = Twython(
app_key = settings.app_key,
app_secret = settings.app_secret,
oauth_token = settings.oauth_token,
oauth_token_secret = settings.oauth_token_secret
)
twitter.update_status_with_media(status=message, media=open("capture.jpg", 'rb'))
# archive the image and text
shutil.move("capture.jpg", "history/%s.jpg" % timestamp)
with open("history/%s.txt" % timestamp, "w") as f:
f.write("%s\n" % message)
if __name__ == "__main__":
if "--test" in sys.argv:
goGoPaparazzo()
else:
watcher = GPIOWatcher(7, onChange=goGoPaparazzo, debounceSeconds=20)
while True:
try:
watcher.enter_loop()
except Exception as e:
print "Error:", e