Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. import tweepy
  2. import json
  3. import datetime
  4. import time
  5. from flask import render_template
  6. from pythoncode.cleansing import clean
  7. # from pythoncode import nlp
  8. # from pythoncode import googletranslate
  9. from oauth2client.service_account import ServiceAccountCredentials
  10. from pythoncode.database import inputdata_mentah
  11. # from pythoncode.database import pilihdata
  12. # from pythoncode.database import data_hasil
  13. import pusher #PUSHER REALTIME
  14.  
  15. pusher_client = pusher.Pusher(
  16.   app_id='941528',
  17.   key='a3151284e7197002355d',
  18.   secret='ddf036b120a950a1fb2d',
  19.   cluster='ap1',
  20.   ssl=True
  21. ) #END PUSHER REALTIME
  22.  
  23. # ini proses collect data dari twitter
  24. def get_data(tweet_args,connector):
  25.     access_token = "464377265-2449viFyqknEmNQ4ufMwcJm3Ez8UMAKIMzNS5wFc"
  26.     access_token_secret = "CWJ1LhyHn0GFp8v4NV7ZuX7mYlKRMjq6OYKkRnK2ftCFY"
  27.     consumer_key = "n56wEItczJISenTBA1Na911dO"
  28.     consumer_secret = "4RmsWpV690xshXQMEUMtEQxEJLi7TGylF3J4FXuEsYG9APgqwB"
  29.     OAUTH_KEYS = {'consumer_key':consumer_key, 'consumer_secret':consumer_secret,
  30.         'access_token_key':access_token, 'access_token_secret':access_token_secret}
  31.     auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
  32.     api = tweepy.API(auth, wait_on_rate_limit=True)
  33.    
  34.     #ini proses crawling data di twitter
  35.     index=2
  36.     search = ""+tweet_args+" -filter:retweets"
  37.     for tweet in tweepy.Cursor(api.search, q=search, lang="id", since='2020-04-02', until='2020-04-03', tweet_mode='extended').items(): # need to figure out how to extract all tweets in the previous day, items () dictionary di python itu kayak array tingkat lanjut
  38.     # for tweet in tweepy.Cursor(api.search, q=search,lang="id",tweet_mode='extended', until=''+str(datetime.date.today())).items(): # ini ambil datanya pada hari ini, kl yang atas disesuaiin tanggalnya
  39.     # for twit in tweepy.Cursor(api.user_timeline, id=user_target tweet_mode='extended').items():
  40.     # bisa pake cara yang atas atau yang atas atau bawah
  41.  
  42.  
  43.             print("////////////////////////////////")
  44.             print ("Tweet created:", tweet.created_at)
  45.             print("status :", tweet.full_text)
  46.             print("user:",tweet.user.name)
  47.             print ("")
  48.  
  49.             username = tweet.user.name
  50.             sebelumcleansing = tweet.full_text
  51.  
  52.             #ini proses cleansing text dari twitter
  53.             setelahcleansing = clean(sebelumcleansing)
  54.             data = setelahcleansing.encode('ascii', 'ignore').decode('ascii') #membersihkan emoji
  55.             tanggal_tweet = tweet.created_at
  56.                
  57.             #ini proses habis ambil data dari twiiter langsung disimpen ke database
  58.             inputdata_mentah(connector,username,sebelumcleansing,tanggal_tweet,'twitter',tweet_args)
  59.        
  60.             #ini proses mengambil data dari python untuk ditampilkan ke dalam website menggunakan pusher
  61.             pusher_client.trigger('my-channel', 'my-event', {'message': sebelumcleansing,'username': username})
  62.  
  63.             print(data)
  64.    
  65.             #ini untuk analisis sentimen menggunakan google nlp
  66.             # try:
  67.             #     nlp.sentiment(data.lower(),sebelumcleansing,tanggal_tweet,username,"twitter", tweet_args)
  68.             # except:
  69.             #     print("error")
  70.            
  71.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement