Commit e621a60f authored by Rémy Decoupes's avatar Rémy Decoupes
Browse files

add filter account for stream

parent 409c1816
#### Twitter Credentials #### #### Twitter Credentials ####
params/credentials.py params/credentials.py
#### tweets ####
output/tweetoutput.json
#### Log #### #### Log ####
log/collectweets.log log/collectweets.log
#### libreoffice back up #### #### libreoffice back up ####
......
...@@ -15,6 +15,7 @@ import tweepy ...@@ -15,6 +15,7 @@ import tweepy
import sys import sys
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import pandas as pd
def exitscript(logger, message): def exitscript(logger, message):
...@@ -50,10 +51,26 @@ def logsetup(): ...@@ -50,10 +51,26 @@ def logsetup():
logger.addHandler(stream_handler) logger.addHandler(stream_handler)
return logger return logger
class Listener(tweepy.StreamListener):
def __init__(self, output_file=sys.stdout, logger=sys.stdout):
super(Listener, self).__init__()
self.output_file = output_file
self.logger = logger
self.logger.info("initiate stream listener")
def on_status(self, status):
print(status.text, file=self.output_file)
def on_error(self, status_code):
logger.error("Error on stream twiter: "+str(status_code))
return False
if __name__ == '__main__': if __name__ == '__main__':
print("Collect tweets : start")
# initialize a logger : # initialize a logger :
logger = logsetup() logger = logsetup()
logger.info("Collect tweets : start")
# try import credentials of MOODTwitter account # try import credentials of MOODTwitter account
try: try:
...@@ -69,7 +86,7 @@ if __name__ == '__main__': ...@@ -69,7 +86,7 @@ if __name__ == '__main__':
api = tweepy.API(auth) api = tweepy.API(auth)
# Get the User object for twitter... # Get the User object for twitter...
accountused = api.me() accountused = api.me()
logger.info("Log with: "+accountused.name) logger.info("Log with: " + accountused.name)
except tweepy.TweepError as twe: except tweepy.TweepError as twe:
msg = "Wrong credentials: please check credentials.py" msg = "Wrong credentials: please check credentials.py"
exitscript(logger, msg) exitscript(logger, msg)
...@@ -77,4 +94,23 @@ if __name__ == '__main__': ...@@ -77,4 +94,23 @@ if __name__ == '__main__':
msg = "Please double check credentials.py :" + e msg = "Please double check credentials.py :" + e
exitscript(logger, msg) exitscript(logger, msg)
# Get twitter ID of account
accounttofollowed = pd.read_csv("params/accountsFollowed.csv")
accounttofollowedlist = accounttofollowed['twitterID'].tolist()
accounttofollowedlist = list(map(str, accounttofollowedlist))
# Start a Twitter stream
tweetouputfilename = "output/tweetoutput.json"
tweetoutput = open(tweetouputfilename, 'w')
myStreamListener = Listener(tweetoutput, logger)
stream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
try:
logger.info("Start streaming")
stream.filter(follow=accounttofollowedlist)
except KeyboardInterrupt as e:
logger.info("Stream Keyboard Interrupt")
finally:
stream.disconnect()
tweetoutput.close()
logger.info("Collect tweets : proceeded normally") logger.info("Collect tweets : proceeded normally")
# Version empty directory
*
!.gitignore
\ No newline at end of file
account,country,adding_date,description account,twitterID,country,adding_date,description
@yleuutiset,Finland,2020-06-09,Yle Uutiset (in finnish) @yleuutiset,23502747,Finland,2020-06-09,Yle Uutiset (in finnish)
@ylenews,Finland,2020-06-09,Yle news (in english) @ylenews,21184887,Finland,2020-06-09,Yle news (in english)
@hsfi,Finland,2020-06-09,Helsingin Sanomat @hsfi,39540130,Finland,2020-06-09,Helsingin Sanomat
@iltalehti_fi,Finland,2020-06-09,Iltalehti @iltalehti_fi,66947137,Finland,2020-06-09,Iltalehti
@iltasanomat,Finland,2020-06-09,Iltasanomat @iltasanomat,29057955,Finland,2020-06-09,Iltasanomat
@SuomenKuvalehti,Finland,2020-06-09,Suomen kuvalehti @SuomenKuvalehti,84062296,Finland,2020-06-09,Suomen kuvalehti
@MaasTul,Finland,2020-06-09,Maaseudun tulevaisuus @MaasTul,2325044053,Finland,2020-06-09,Maaseudun tulevaisuus
@KauppalehtiFi,Finland,2020-06-09,Kauppalehti @KauppalehtiFi,34621907,Finland,2020-06-09,Kauppalehti
@Nordic_News,Finland,2020-06-09,Nordic News @Nordic_News,387220584,Finland,2020-06-09,Nordic News
@HelsinkiTimes,Finland,2020-06-09,Helsinki Times @HelsinkiTimes,46619850,Finland,2020-06-09,Helsinki Times
@lemondefr,France,2020-06-09,Le Monde @lemondefr,24744541,France,2020-06-09,Le Monde
@Le_Figaro,France,2020-06-09,Figaro @Le_Figaro,8350912,France,2020-06-09,Figaro
@LEXPRESS,France,2020-06-09,L'express @LEXPRESS,13353862,France,2020-06-09,L'express
@Sciences_Avenir,France,2020-06-09,Sciences et avenir @Sciences_Avenir,861574608,France,2020-06-09,Sciences et avenir
@20Minutes,France,2020-06-09,20MINUTES @20Minutes,2649991,France,2020-06-09,20MINUTES
@lobs,France,2020-06-09,L'OBS @lobs,21313364,France,2020-06-09,L'OBS
@lopinion_fr,France,2020-06-09,L'OPINION @lopinion_fr,1283588581,France,2020-06-09,L'OPINION
@LaTribune,France,2020-06-09,LA TRIBUNE @LaTribune,137253079,France,2020-06-09,LA TRIBUNE
@LesEchos,France,2020-06-09,LES ECHOS @LesEchos,92564226,France,2020-06-09,LES ECHOS
@le_Parisien,France,2020-06-09,LE PARISIEN @le_Parisien,38142665,France,2020-06-09,LE PARISIEN
@leJDD,France,2020-06-09,LE JDD @leJDD,49678509,France,2020-06-09,LE JDD
@OuestFrance,France,2020-06-09,OUEST FRANCE @OuestFrance,35777570,France,2020-06-09,OUEST FRANCE
@LaCroix,France,2020-06-09,LA CROIX @LaCroix,12272292,France,2020-06-09,LA CROIX
@libe,France,2020-06-09,LIBERATION @libe,68440549,France,2020-06-09,LIBERATION
@MarianneleMag,France,2020-06-09,MARIANNE @MarianneleMag,15823722,France,2020-06-09,MARIANNE
certifi==2020.4.5.2 certifi==2020.4.5.2
chardet==3.0.4 chardet==3.0.4
idna==2.9 idna==2.9
numpy==1.18.5
oauthlib==3.1.0 oauthlib==3.1.0
pandas==1.0.4
PySocks==1.7.1 PySocks==1.7.1
python-dateutil==2.8.1
pytz==2020.1
requests==2.23.0 requests==2.23.0
requests-oauthlib==1.3.0 requests-oauthlib==1.3.0
six==1.15.0 six==1.15.0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment