-
Notifications
You must be signed in to change notification settings - Fork 2
/
snapshot-twitter-mastodon.py
executable file
·149 lines (130 loc) · 3.95 KB
/
snapshot-twitter-mastodon.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
#! /usr/bin/python3 -u
# -*- coding: utf-8 -*-
"""
Based in:
http://stackoverflow.com/questions/15870619/python-webcam-http-streaming-and-image-capture
"""
HOMEDIR = "/home/pi"
import pygame
import pygame.camera
import time
import sys
import os
import twitter
import ConfigParser
import datetime
# test machine?
if os.uname()[1] == 'elxaf7qtt32':
# my laptop
HOMEDIR = "/home/ehellou"
else:
HOMEDIR=os.getenv('HOME')
configuration = f"{HOMEDIR}/.twitterc"
agenda = f"{HOMEDIR}/pyconse.agenda"
SAVEDIR = f"{HOMEDIR}/pyconse"
def get_content():
if not os.path.exists(agenda):
return None
fd = open(agenda)
timestamp = time.strftime("%Y-%m-%d", time.localtime())
YYYY = int(time.strftime("%Y", time.localtime()))
MM = int(time.strftime("%m", time.localtime()))
DD = int(time.strftime("%d", time.localtime()))
now = datetime.datetime.now()
for line in fd.readlines():
print(line)
if not line[0] == '2':
continue
try:
tmstp_date, tmstp_timeslot, tmstp_info = line.split(",")
except:
continue
# not same day? move forward
if tmstp_date != timestamp:
print("No date")
continue
tmstp_begin, tmstp_end = tmstp_timeslot.split("-")
h_beg, m_beg = tmstp_begin.split(":")
beg = datetime.datetime(YYYY, MM, DD, int(h_beg), int(m_beg))
delta = now - beg
# before time?
if delta.days < 0:
print("It didn't start yet.")
continue
h_end, m_end = tmstp_end.split(":")
end = datetime.datetime(YYYY, MM, DD, int(h_end), int(m_end))
delta = end - now
if delta.days < 0:
print("It already finished")
continue
tmstp_info = tmstp_info.rstrip()
if tmstp_info == "EMPTY":
print("Got empty")
return None
print(f"Got here, so sending back \"{tmstp_info}\"" )
return tmstp_info
def TweetPhoto():
"""
"""
print("Pygame init")
pygame.init()
print("Camera init")
pygame.camera.init()
# you can get your camera resolution by command "uvcdynctrl -f"
cam = pygame.camera.Camera("/dev/video1", (1280, 720))
print("Camera start")
cam.start()
time.sleep(1)
print("Getting image")
image = cam.get_image()
time.sleep(1)
print("Camera stop")
cam.stop()
if not os.path.exists(SAVEDIR):
os.makedirs(SAVEDIR)
timestamp = time.strftime("%Y-%m-%d_%H%M%S", time.localtime())
year = time.strftime("%Y", time.localtime())
filename = f"{SAVEDIR}/{timestamp}.jpg"
print(f"Saving file {filename}")
pygame.image.save(image, filename)
cfg = ConfigParser.ConfigParser()
print(f"Reading configuration: {configuration}")
if not os.path.exists(configuration):
print("Failed to find configuration file {configuration}")
sys.exit(1)
cfg.read(configuration)
cons_key = cfg.get("TWITTER", "CONS_KEY")
cons_sec = cfg.get("TWITTER", "CONS_SEC")
acc_key = cfg.get("TWITTER", "ACC_KEY")
acc_sec = cfg.get("TWITTER", "ACC_SEC")
print("Autenticating in Twitter")
# App python-tweeter
# https://dev.twitter.com/apps/815176
tw = twitter.Api(
consumer_key = cons_key,
consumer_secret = cons_sec,
access_token_key = acc_key,
access_token_secret = acc_sec
)
print("Posting...")
msg = get_content()
if not msg:
now = time.strftime("%H:%M", time.localtime())
msg = (f"Just another shot at {now}")
if msg:
msg = f"{msg} #pyconse"
print(msg)
try:
tw.PostMedia(status = msg,media = filename)
print("done!")
except:
None
else:
print("no message available")
#print "Removing media file %s" % filename
#os.unlink(filename)
if __name__ == '__main__':
try:
TweetPhoto()
except KeyboardInterrupt:
sys.exit(0)