|
| 1 | +from twython import Twython |
| 2 | +from twython import TwythonStreamer |
| 3 | +from datetime import date |
| 4 | +class TweetStreamer(TwythonStreamer): |
| 5 | + def on_success(self, data): |
| 6 | + if 'text' in data: |
| 7 | + print (data['text'].encode('utf-8')) |
| 8 | + |
| 9 | + def on_error(self, status_code, data): |
| 10 | + print (status_code) |
| 11 | + self.disconnect() |
| 12 | +class twit: |
| 13 | + def __init__(self): |
| 14 | + #define needed parameter for twitterapi to access twitter account |
| 15 | + APP_KEY = 'X5gCNKpqL6NUsLvBJjIYIm4Pz' |
| 16 | + APP_SECRET = 'UrlJVaRjpWmZrMjmWH7uADCBCzXwdoceIWVv2ftC34z85wpmMR' |
| 17 | + OAUTH_TOKEN = '1267471839929880577-ks8UGM7ydtxJ8xWJpHtv55tUCVGh7Z' |
| 18 | + OAUTH_TOKEN_SECRET = 'QjKJ2FLe3R6gGtGD17lyBauOAqfptcnK75B5Sk8qZRSYN' |
| 19 | + self.twitter = Twython(APP_KEY, APP_SECRET,OAUTH_TOKEN, OAUTH_TOKEN_SECRET) |
| 20 | + self.streamer = TweetStreamer(APP_KEY, APP_SECRET, |
| 21 | + OAUTH_TOKEN, OAUTH_TOKEN_SECRET) |
| 22 | + def search(self,tag): |
| 23 | + #q => searched quote |
| 24 | + #result_type >> recent, popular, mixed |
| 25 | + results = self.twitter.search(q=tag,result_type = 'recent') |
| 26 | + #this function return results.keys() = ['statuses','meta_data'] we use only 'statuses' here |
| 27 | + statuses = results['statuses'] |
| 28 | + k = [] |
| 29 | + for i in range(len(statuses)) : |
| 30 | + #collect wanted results ((( cd.keys() = ['screen_name','id_user','tweet_when','tweet_id','text'] ))) |
| 31 | + #in list ((( k ))) |
| 32 | + cd = dict() |
| 33 | + cd['screen_name'] = statuses[i]['user']['screen_name'] |
| 34 | + cd['id_user'] = statuses[i]['user']['id'] |
| 35 | + cd['tweet_when'] = statuses[i]['created_at'] |
| 36 | + cd['tweet_id'] = statuses[i]['id'] |
| 37 | + cd['text'] = statuses[i]['text'] |
| 38 | + cd['location'] = statuses[i]['user']['location'] |
| 39 | + k.append(cd) |
| 40 | + return(k) |
| 41 | + def post(self,text): |
| 42 | + #to post status,but can not spam the same tweet at the time. limit 300 tweets/3 hour for standard account |
| 43 | + self.twitter.update_status(status=text) |
| 44 | + def search2(self,text,*args,**kwrgs): |
| 45 | + #q => searched quote |
| 46 | + #result_type >> recent, popular, mixed |
| 47 | + defKwrgs = {'result_type' : 'recent','lang' :'en','count' : 15,'until': str(date.today())} |
| 48 | + for k,item in kwrgs.items(): |
| 49 | + defKwrgs[k] = item |
| 50 | + |
| 51 | + results = self.twitter.search(q=text,\ |
| 52 | + result_type = defKwrgs['result_type'],\ |
| 53 | + lang =defKwrgs['lang'],\ |
| 54 | + count = int(defKwrgs['count']),\ |
| 55 | + until = defKwrgs['until'] ) |
| 56 | + #this function return results.keys() = ['statuses','meta_data'] we use only 'statuses' here |
| 57 | + statuses = results['statuses'] |
| 58 | + |
| 59 | + |
| 60 | + #for arg in args: |
| 61 | + k = [] |
| 62 | + |
| 63 | + for i,st in enumerate(statuses) : |
| 64 | + #collect wanted results ((( cd.keys() = ['screen_name','id_user','tweet_when','tweet_id','text'] ))) |
| 65 | + #in list ((( k ))) |
| 66 | + cd = dict() |
| 67 | + |
| 68 | + cd['screen_name'] = st['user']['screen_name'] |
| 69 | + cd['id_user'] = st['user']['id'] |
| 70 | + cd['tweet_when'] = st['created_at'] |
| 71 | + cd['tweet_id'] = st['id'] |
| 72 | + cd['text'] = st['text'] |
| 73 | + cd['location'] = st['user']['location'] |
| 74 | + temp = dict() |
| 75 | + if args[0] == 'all': temp = cd |
| 76 | + else: |
| 77 | + for arg in args: |
| 78 | + temp[arg] = cd[arg] |
| 79 | + k.append(temp) |
| 80 | + return k |
| 81 | + def stream(self,text): |
| 82 | + self.streamer.statuses.filter(track = '#lisa') |
0 commit comments