· 7 years ago · Oct 23, 2018, 11:36 AM
1from tweepy.streaming import StreamListener
2from tweepy import OAuthHandler
3from tweepy import Stream
4import tweepy
5import traceback
6
7import secrets
8import json
9
10import sqlite3
11import pandas as pd
12
13def create_connection(db_file):
14 """ create a database connection to the SQLite database
15 specified by db_file
16 :param db_file: database file
17 :return: Connection object or None
18 """
19 try:
20 conn = sqlite3.connect(db_file)
21 conn.execute('create table if not exists tweets(tweet text, response text)')
22 return conn
23 except Error as e:
24 print(e)
25
26 return None
27
28
29def insert_tweet(conn, tweet):
30 """
31 Create a new project into the projects table
32 :param conn:
33 :param project:
34 :return: project id
35 """
36 sql = ''' INSERT INTO tweets(tweet, response)
37 VALUES(?,?) '''
38 cur = conn.cursor()
39 cur.execute(sql, tweet)
40 return cur.lastrowid
41
42
43
44
45auth = OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
46auth.set_access_token(secrets.access_token_key, secrets.access_token_secret)
47api = tweepy.API(auth)
48
49
50
51class StdOutListener(StreamListener):
52
53 def __init__(self):
54 self.max=100
55
56 self.i = 0
57
58 def on_data(self, data):
59 try:
60 j = json.loads(data)
61 if str(j['user']['id']) == '85741735':
62 reply_to = api.get_status(str(j['in_reply_to_status_id']))
63 if reply_to:
64 print('{}\n\t{}'.format(reply_to.text,j['text']))
65 with conn:
66 record = (reply_to.text, j['text'])
67 insert_tweet(conn, record)
68 #if tweet:
69 # if self.i % 10 == 0:
70 # print(self.i)
71 # with conn:
72 # t = json.loads(tweet)
73 # record = (j['text'], t['text'])
74 # insert_tweet(conn, record)
75 # self.i=self.i+1
76
77 # if self.i==self.max:
78 # exit()
79 return True
80 except Exception as E:
81 traceback.print_stack()
82 print(E)
83 pass
84
85 def on_error(self, status):
86 print(status)
87 with open('results.json', 'w') as outfile:
88 json.dump(self.r, outfile)
89
90'po8765'
91conn = create_connection('test.db')
92listener = StdOutListener()
93stream = Stream(auth, listener)
94stream.filter(follow=['85741735'])
95
96#df = pd.read_sql_query('select * from tweets limit 5', conn)
97#print(df.head())