Skip to content

Commit 58e7ea5

Browse files
authoredJul 19, 2020
Add files via upload
0 parents  commit 58e7ea5

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
 

‎demo1.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from twythonxx import twit #ใช้เรียกcalss จากไฟล twythonxx
2+
3+
e=twit() #ทำการประกาศตัวแปร eเป็นobjของ class twit
4+
#-----------------------------------------------
5+
#e.post("testtest")
6+
#ใช้สำหรับโพสโดยแก้ค่าในวงเล็บให้เป็นข้อความตามที่ต้องการโพส โดยมีลิมิตอยู่ที่300ครั้ง/3ชม
7+
#-----------------------------------------------
8+
#data=e.search("#lisa")
9+
#ใช้สำหรับค้นหาโดยแก้ค่าในวงเล็บให้เป็นข้อความที่ต้องหารค้นหา โดยมีลิมิตอยู่ที่180 ครั้ง/15นาที
10+
#ค่าที่ได้จะออกมาในรูปของlistของแต่ล่ะทวิตซึ่งแต่ล่ะทวิตจะเป็น dict ที่ประกอบด้วยสมาชิกดังนี้ 'screen_name','id_user','tweet_when','tweet_id','text'
11+
#-----------------------------------------------
12+
#ใช้สำหรับค้นหาแบบที่ต้องการกำหนดรายละเอียดการค้นหาโดยจะให้ผลเป็น listที่ประกอบขึ้นจากdictโดยแต่ล่ะdictแทนทวิตแต่ล่ะข้อความที่ค้นเจอ
13+
#x={'count' : 5}#ตัวแปรสำหรับระบุรายละเอียดการค้นหาประกอบด้วย 'result_type' 'lang' 'count' 'until'
14+
#data=e.search2("#lisa",'screen_name','tweet_id',**x) #สามารถระบุประเภทข้อมูลที่ต้องการได้โดยใช้ all หากต้องการทุกค่า ข้อมูลที่สามารถระบุได้มีดังนี้ 'screen_name','id_user','tweet_when','tweet_id','text'
15+
#-----------------------------------------------
16+
#e.stream('#lisa')#คำสั่งนี้จะใช้สำหรับติดตามแทกที่ต้องการโดยจะแสดงผลทวิตใหม่ทุกรั้งทีมีการทวิตแทกนี้
17+
#-----------------------------------------------
18+
#print(data) #ทำการแสดงผลตัวแปร dataที่ได้
19+
20+

‎fileAPI.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pandas as pd
2+
import os
3+
from Twythonx import *
4+
5+
class fileAPI:
6+
7+
def __init__(self,name):
8+
self.fileName = name
9+
10+
def readFile(self):
11+
#open csv files
12+
df = pd.read_csv(self.fileName)
13+
tempDict = df.to_dict('list')
14+
return tempDict
15+
16+
def writeFile(self,inDict):
17+
#save files in csv form
18+
df = pd.DataFrame(inDict)
19+
df.to_csv(path_or_buf= self.fileName ,index=False)
20+
return "success"
21+
22+

‎twythonxx.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)
Please sign in to comment.