-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsearch.py
48 lines (41 loc) · 2.15 KB
/
search.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
import tweepy,sys,jsonpickle
consumer_key = 'isi dengan key token'
consumer_secret = 'isi dengan secret key'
print("#===============================================#")
print("Program Crawling Twitter menggunakan Python 3")
print("\t SARIKHIN TI.17.D.7")
print("\t PELITA BANGSA")
print("#===============================================#")
qry = input("Masukkan Query yang akan anda cari :") #input query yang akan dicari
fName = input("Nama File Hasil Crawling :") #input nama file hasil pencarian
maxTweets = 500# Isi sembarang nilai sesuai kebutuhan anda
tweetsPerQry = 100 # Jangan isi lebih dari 100, ndak boleh oleh Twitter
auth = tweepy.AppAuthHandler(consumer_key,consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
if (not api):
sys.exit('Autentikasi gagal, mohon cek "Consumer Key" & "Consumer Secret" Twitter anda')
sinceId, max_id, tweetCount = None, -1, 0
print("Mulai mengunduh maksimum {0} tweets".format(maxTweets))
with open(fName,'w') as f:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets=api.search(q=qry,count=tweetsPerQry)
else:
new_tweets=api.search(q=qry,count=tweetsPerQry,since_id=sinceId)
else:
if (not sinceId):
new_tweets=api.search(q=qry,count=tweetsPerQry,max_id=str(max_id - 1))
else:
new_tweets=api.search(q=qry,count=tweetsPerQry,max_id=str(max_id - 1),since_id=sinceId)
if not new_tweets:
print('Tidak ada lagi Tweet ditemukan dengan Query="{0}"'.format(qry));break
for tweet in new_tweets:
f.write(jsonpickle.encode(tweet._json,unpicklable=False)+'\n')
tweetCount+=len(new_tweets)
sys.stdout.write("\r");sys.stdout.write("Jumlah Tweets telah tersimpan: %.0f" %tweetCount);sys.stdout.flush()
max_id=new_tweets[-1].id
except tweepy.TweepError as e:
print("some error : " + str(e));break # Aya error, keluar
print ('\nSelesai! {0} tweets tersimpan di "{1}"'.format(tweetCount,fName))