Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tweepy
- import json
- import datetime
- import time
- from flask import render_template
- from pythoncode.cleansing import clean
- # from pythoncode import nlp
- # from pythoncode import googletranslate
- from oauth2client.service_account import ServiceAccountCredentials
- from pythoncode.database import inputdata_mentah
- # from pythoncode.database import pilihdata
- # from pythoncode.database import data_hasil
- import pusher #PUSHER REALTIME
- pusher_client = pusher.Pusher(
- app_id='941528',
- key='a3151284e7197002355d',
- secret='ddf036b120a950a1fb2d',
- cluster='ap1',
- ssl=True
- ) #END PUSHER REALTIME
- # ini proses collect data dari twitter
- def get_data(tweet_args,connector):
- access_token = "464377265-2449viFyqknEmNQ4ufMwcJm3Ez8UMAKIMzNS5wFc"
- access_token_secret = "CWJ1LhyHn0GFp8v4NV7ZuX7mYlKRMjq6OYKkRnK2ftCFY"
- consumer_key = "n56wEItczJISenTBA1Na911dO"
- consumer_secret = "4RmsWpV690xshXQMEUMtEQxEJLi7TGylF3J4FXuEsYG9APgqwB"
- OAUTH_KEYS = {'consumer_key':consumer_key, 'consumer_secret':consumer_secret,
- 'access_token_key':access_token, 'access_token_secret':access_token_secret}
- auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
- api = tweepy.API(auth, wait_on_rate_limit=True)
- #ini proses crawling data di twitter
- index=2
- search = ""+tweet_args+" -filter:retweets"
- 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
- # 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
- # for twit in tweepy.Cursor(api.user_timeline, id=user_target tweet_mode='extended').items():
- # bisa pake cara yang atas atau yang atas atau bawah
- print("////////////////////////////////")
- print ("Tweet created:", tweet.created_at)
- print("status :", tweet.full_text)
- print("user:",tweet.user.name)
- print ("")
- username = tweet.user.name
- sebelumcleansing = tweet.full_text
- #ini proses cleansing text dari twitter
- setelahcleansing = clean(sebelumcleansing)
- data = setelahcleansing.encode('ascii', 'ignore').decode('ascii') #membersihkan emoji
- tanggal_tweet = tweet.created_at
- #ini proses habis ambil data dari twiiter langsung disimpen ke database
- inputdata_mentah(connector,username,sebelumcleansing,tanggal_tweet,'twitter',tweet_args)
- #ini proses mengambil data dari python untuk ditampilkan ke dalam website menggunakan pusher
- pusher_client.trigger('my-channel', 'my-event', {'message': sebelumcleansing,'username': username})
- print(data)
- #ini untuk analisis sentimen menggunakan google nlp
- # try:
- # nlp.sentiment(data.lower(),sebelumcleansing,tanggal_tweet,username,"twitter", tweet_args)
- # except:
- # print("error")
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement