forked from kylemcdonald/snapitforward
/
snapitforward.py
108 lines (88 loc) · 2.66 KB
/
snapitforward.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
import glob
import json
import time
import os.path
import random
from datetime import datetime
from snapchat import Snapchat
def log(msg):
print ("{0}\t{1}".format(datetime.now(), msg))
def date_serializer(obj):
if isinstance(obj, datetime):
serial = obj.isoformat()
return serial
s = None
previous_snaps = None
credentials = json.load(open('credentials.json'))
try:
while True:
delay = 5 + random.random() * 10
time.sleep(delay)
if not s or not s.logged_in:
log ('Logging in to {0}'.format(credentials['username']))
s = Snapchat(credentials['username'], credentials['password'])
log ('Logged in')
snaps = s.get_snaps()
if previous_snaps == snaps:
log ('No new snaps')
continue
previous_snaps = snaps
for snap in snaps:
sender = snap['sender']
if snap['status'] == 2:
# log ('Status is 2, ignoring')
continue
if not sender:
# log ('Snap has no sender, ignoring')
continue
if sender == credentials['username']:
# log ('Snap is a sent message, ignoring')
continue
if snap['media_type']:
# log ('Snap is not an image, ignoring')
continue
log ('Processing snap {0} from {1}'.format(snap['id'], snap['sender']))
media_path = 'cache/' + snap['id']
if os.path.isfile(media_path):
log ('Already downloaded ' + snap['id'])
continue
log ('Writing history')
try:
with open('history.json'.format(datetime.now()), 'w') as f:
f.write(json.dumps(snaps, default=date_serializer, indent=2))
except TypeError as e:
log ('Error saving history: {0}'.format(e))
log ('Downloading ' + snap['id'])
media = s.get_media(snap['id'])
# log ('Marking {0} as seen'.format(snap['id']))
# s.mark_seen(snap['id'])
if media == None:
log ('Cannot download ' + snap['id'])
continue
log ('Successfully downloaded ' + snap['id'])
try:
with open(media_path, 'w') as f:
log ('Writing to cache from ' + snap['id'])
f.write(media)
except TypeError as e:
log ('Error saving file: {0}'.format(e))
queue = glob.glob('queue/*')
if len(queue):
queue_file = queue[0]
log ('Uploading item from {0}'.format(queue_file))
# possible that the media did not upload
media_id = s.upload(Snapchat.MEDIA_IMAGE, queue_file)
log ('Sending {0}'.format(media_id))
# possible that the media did not send
s.send(media_id, sender)
log ('Deleting ' + queue_file)
os.remove(queue_file)
try:
with open('queue/' + snap['id'], 'w') as f:
log ('Writing to queue from ' + snap['id'])
f.write(media)
except TypeError as e:
log ('Error saving file: {0}'.format(e))
except KeyboardInterrupt:
s.logout()
log('Logging out')