-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterhelper.py
More file actions
65 lines (57 loc) · 1.88 KB
/
Copy pathtwitterhelper.py
File metadata and controls
65 lines (57 loc) · 1.88 KB
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
__author__ = "p91 <p91.hasi.it>"
__version__ = 0.1
__date__ = "22.10.2012"
__status__ = "testing"
from logginghelper import logging
import time
try:
import twitter
if not twitter.__author__ == 'python-twitter@googlegroups.com':
raise AttributeError
except AttributeError, e:
logging.exception("Wrong twitter module used! Please use this one: https://code.google.com/p/python-twitter/")
logging.exception(e)
exit()
except ImportError, e:
logging.exception("Twitter module not found! Please download this one: https://code.google.com/p/python-twitter/")
logging.exception(e)
exit()
class TweetsError(Exception):
def __init__(self, e):
self.e = e
def __str__(self):
return self.e.__str__()
class Tweet:
def __init__(self, screen_name, id, text):
self.user = self.User(screen_name)
self.id = id
self.text = text
class User:
def __init__(self, screen_name):
self.screen_name = screen_name
def get_tweets_by_terms(terms, last_id, refresh_time = 10, tweets_per_page = 100, recursive = True):
tweet_list = []
id_list = []
for term in terms:
try:
tweets = get_tweets_by_term(term, last_id, refresh_time, tweets_per_page, recursive)
except Exception, e :
raise TweetsError(e)
#check for double tweets
for tweet in tweets:
if not tweet.id in id_list:
tweet_list.append(tweet)
id_list.append(tweet.id)
tweet_list.sort(key = lambda x: x.id, reverse=False)
return tweet_list
#from here on dependend on the avaiable twitter api
def get_tweets_by_term(term, last_id, refresh_time = 10, tweets_per_page = 100, recursive = True):
tweets = []
while True:
time.sleep(refresh_time)
tmptweets = twitter.Api().GetSearch(term=term, since_id=last_id, per_page=tweets_per_page, lang=None)
tweets.extend([Tweet(i.user.screen_name, i.id, i.text) for i in tmptweets])
if not (len(tmptweets) == tweets_per_page and recursive):
break
last_id = tweets[-1].id
return tweets