-
Notifications
You must be signed in to change notification settings - Fork 2
/
octo-track-twitter.py
executable file
·94 lines (79 loc) · 3.35 KB
/
octo-track-twitter.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
#! /usr/bin/env python
# -*- coding: utf-8 *-*
#
# Copyright (C) Nicolas Bareil <nico@chdir.org>
#
# This program is published under a GPLv2 license
from optparse import OptionParser
import logging
import fileinput
import sys
import os
import tweepy
import tweepy.streaming
class SearchStream(tweepy.streaming.StreamListener):
def on_status(self, status):
s = '%s @%s: %s\n' % (status.created_at, status.user.screen_name, status.text)
output.write(s.encode('utf-8'))
output.flush()
return
def on_limit(self, track):
log.error('limitation imposed for %s' % track)
def on_timeout(self):
log.error('Twitter timeout')
def on_error(self, status_code):
log.error('HTTP error: %d' % status_code)
return False
def track_search(terms):
searcher = SearchStream(api=api)
stream = tweepy.streaming.Stream(auth, searcher)
try:
stream.filter(track=terms)
except KeyboardInterrupt:
log.info('asking to leave...')
output.close()
stream.disconnect()
def usage(ret=1):
parser.print_help()
sys.exit(ret)
if __name__ == '__main__':
parser = OptionParser(usage=u'usage: %prog [options]')
parser.add_option('-C', '--consumer-key', dest='consumer_key', metavar="TOKEN",
help=u"Twitter consummer key")
parser.add_option('-S', '--consumer-secret', dest='consumer_secret', metavar="TOKEN",
help=u"Twitter consummer secret")
parser.add_option('-A', '--access-token', dest='access_token', metavar="TOKEN",
help=u"Twitter access key")
parser.add_option('-K', '--access-key', dest='access_key', metavar="TOKEN",
help=u"Twitter access key")
parser.add_option('-o', '--output', dest='output', metavar="FILE",
help=u"Write output in file")
parser.add_option('-v', '--verbose', dest='verbose', action="store_true",
default=False, help=u"Verbose mode")
parser.add_option('-d', '--debug', dest='debug', action="store_true",
default=False, help=u"Debug mode")
parser.add_option('-t', '--track', dest='track', action="store_true",
default=True, help="Tracks keywords in realtime")
parser.add_option('-O', '--old', dest='old', action="store_true",
default=False, help="Search past tweets (not implemented yet)")
(options,args) = parser.parse_args()
if not (options.consumer_secret and options.consumer_key
and options.access_token and options.access_key):
sys.stderr.write('You need to specify OAuth consumer and access token/secret\n\n')
usage()
if options.output:
output = open(options.output, 'a')
else:
output = sys.stdout
loglvl = logging.NOTICE if options.verbose else logging.INFO
loglvl = logging.DEBUG if options.debug else logging.INFO
logging.basicConfig(level=loglvl, format="%(asctime)s %(levelname)5s: %(message)s")
log = logging.getLogger(sys.argv[0])
auth = tweepy.OAuthHandler(options.consumer_key, options.consumer_secret)
auth.set_access_token(options.access_token, options.access_key)
api = tweepy.API(auth)
if options.old:
sys.stderr.write('Not implemented yet (XXX), sorry :(\n')
sys.exit(2)
elif options.track:
track_search(args)