-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsearch.py
executable file
·168 lines (140 loc) · 6.03 KB
/
tsearch.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/python
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import tweepy
import simplejson as json
import time
import HTMLParser
import unicodedata
import re
import sys
import textwrap
import socket
from datetime import datetime
from dateutil import tz,parser
skip_retweets = True
# halfassed attempt to match "any" url, since we don't want to print them
# see http://daringfireball.net/2010/07/improved_regex_for_matching_urls
GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
edits = [ ('@', '(at)'), ('<', '('), ('>', ')'), ('%', '(pct)'), ('=', '(eq)'),
('[', '('), (']', ')'), ('+', '.'), ('_', '-'), ('|', '!') ]
# oauth consumer and access tokens, these have to be done manually
consumer_key="YOUR_TWITTER_CONSUMER_KEY"
consumer_secret="YOUR_TWITTER_CONSUMER_SECRET"
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token="YOUR_ACCESS_TOKEN"
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
def store_last(lastblob):
try:
with open('/tmp/last_twitterid', 'w') as f: f.write(json.dumps(lastblob)+'\n')
except:
#print "failed to store last twitterid to /tmp/last_twitterid"
return
def fetch_last():
try:
with open('/tmp/last_twitterid', 'r') as f:
lastblob = json.loads(f.read())
except:
lastblob = {}
lastblob['last_twitterid'] = 1
lastblob['last_dm'] = 1
lastblob['last_mention'] = 1
return lastblob
def print_statuses(results, header):
hparser = HTMLParser.HTMLParser()
count=0
ids_printed = []
for result in results:
if hasattr(result, "retweeted_status"):
if skip_retweets:
continue
count = count + 1
if count > 100:
continue
if header and count == 1:
print header
screenname = hparser.unescape(result.user.screen_name)
screenname = unicodedata.normalize('NFKD', unicode(screenname)).encode('ascii', 'ignore')
author = hparser.unescape(result.user.name)
author = unicodedata.normalize('NFKD', unicode(author)).encode('ascii', 'ignore')
text = hparser.unescape(result.text)
text = unicodedata.normalize('NFKD', unicode(text)).encode('ascii', 'ignore')
pdate_utc = parser.parse(unicode(result.created_at))
utc = pdate_utc.replace(tzinfo=tz.tzutc())
pdate_local = utc.astimezone(tz.gettz('America/Los_Angeles'))
day=pdate_local.day
month=pdate_local.month
year=pdate_local.year
time=datetime.strftime(pdate_local, "%H:%M %a")
pdate_localshort = '%s %d/%d/%d' % (time, month, day, year)
# strip out anything that looks like a URL
text = re.sub(GRUBER_URLINTEXT_PAT, "URL", text)
# construct the whole block to be printed
#block = str(count)+') '+ author + ' ('+screenname+'), ' + pdate_localshort + ': ' + text + '\n'
block = author + ' ('+screenname+'), ' + pdate_localshort + ': ' + text + '\n'
for (orig, repl) in edits:
block = block.replace(orig, repl)
indent = 4
# justify/format only after all edits are made.
fmtblock = '\n'.join(textwrap.wrap(block, width=66,
initial_indent='', subsequent_indent=' '*indent))
print fmtblock
print ""
ids_printed.append(result.id)
return(ids_printed)
def print_directmessages(results, header):
hparser = HTMLParser.HTMLParser()
count=0
ids_printed = []
for result in results:
count = count + 1
if count > 100:
continue
if header and count == 1:
print header
screenname = hparser.unescape(result.sender.screen_name)
screenname = unicodedata.normalize('NFKD', unicode(screenname)).encode('ascii', 'ignore')
author = hparser.unescape(result.sender.name)
author = unicodedata.normalize('NFKD', unicode(author)).encode('ascii', 'ignore')
text = hparser.unescape(result.text)
text = unicodedata.normalize('NFKD', unicode(text)).encode('ascii', 'ignore')
pdate_utc = parser.parse(unicode(result.created_at))
utc = pdate_utc.replace(tzinfo=tz.tzutc())
pdate_local = utc.astimezone(tz.gettz('America/Los_Angeles'))
day=pdate_local.day
month=pdate_local.month
year=pdate_local.year
time=datetime.strftime(pdate_local, "%H:%M %a")
pdate_localshort = '%s %d/%d/%d' % (time, month, day, year)
# strip out anything that looks like a URL
text = re.sub(GRUBER_URLINTEXT_PAT, "URL", text)
# construct the whole block to be printed
#block = str(count)+') '+ author + ' ('+screenname+'), ' + pdate_localshort + ': ' + text + '\n'
block = author + ' ('+screenname+'), ' + pdate_localshort + ': ' + text + '\n'
for (orig, repl) in edits:
block = block.replace(orig, repl)
indent = 4
# justify/format only after all edits are made.
fmtblock = '\n'.join(textwrap.wrap(block, width=66,
initial_indent='', subsequent_indent=' '*indent))
print fmtblock
print ""
ids_printed.append(result.id)
return(ids_printed)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "usage: tsearch searchterms"
sys.exit()
# numtweets = int(sys.argv[1])
numtweets = 10
search = sys.argv[1:]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api=tweepy.API(auth)
# do search and print results
results = tweepy.Cursor(api.search, q=search, result_type="recent").items(numtweets)
ids_printed = print_statuses(results, 'search results:')
if len(ids_printed) == 0:
print "no results."