-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
103 lines (92 loc) · 2.37 KB
/
messages.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
# Twitter profile image updater
# http://twitter.com/account/update_profile_image.json
# image = [imagefile]
import sys
import os
import random
import re
import urllib
import json
import codecs
import urllib2
import time
import oauth2 as oauth
import wcommon
def writelog(body):
data = body
if len(data) == 0:
exit(0)
log = codecs.getwriter('utf8')(open('messages.log', 'a'))
if len(data) == 200:
print >>log, "-- May have lost some tweets here [%s]" % (time.strftime("%a %b %d %H:%M:%S +0000 %Y"))
for tweet in data:
print >>log, "<@%s to @%s> %s [%s]" % (tweet['sender_screen_name'], tweet['recipient_screen_name'], tweet['text'], tweet['created_at'])
log.close()
idfile = open('messages.id', 'w')
print >>idfile, data[0]['id']
idfile.close()
os.chdir("./logs/")
#Note: should not use exceptions here
# should be checking first if the file exists
lastid = None
try:
lastid = open('messages.id', 'r').readline().strip()
except IOError:
pass
if not lastid or lastid == '\n':
try:
log = open('messages.log', 'r')
lasttweet = ""
for line in log:
lasttweet = line
m = re.search(r"/status/(\d+)>", lasttweet)
if not m:
print "Problem with lastid"
exit(1)
lastid = int(m.group(1))
except IOError:
lastid = 12345 #fallback - download as many tweets as possible
received = 1
alldata = []
page = 1
while page < 2 or (received > 20 and page < 20):
url = 'http://api.twitter.com/1/direct_messages.json'
head = ['Expect: ']
data = [('count', 20), ('page', page)]
#data = [('count', 200)]
postdata = urllib.urlencode(data)
url = url+'?'+postdata
print url
r, c = wcommon.oauth_req(url)
if r.status > 200:
print "Request failed: %d" % (r.status)
sys.exit(1)
print c
jdata = json.loads(c)
#print jdata
alldata = alldata + jdata
received = len(jdata)
print str(received)+" messages"
page += 1
page = 1
while page < 2 or (received > 20 and page < 20):
url = 'http://api.twitter.com/1/direct_messages/sent.json'
head = ['Expect: ']
data = [('count', 200), ('page', page)]
#data = [('count', 200)]
postdata = urllib.urlencode(data)
url = url+'?'+postdata
print url
r, c = wcommon.oauth_req(url)
if r.status > 200:
print "Request failed: %d" % (r.status)
sys.exit(1)
#print c
jdata = json.loads(c)
#print jdata
alldata = alldata + jdata
received = len(jdata)
print str(received)+" messages"
page += 1
alldata.sort(key=lambda t: t['id'])
writelog(alldata)