#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  5 12:10:04 2020

@author: shachi
"""

import tweepy
from tweepy import OAuthHandler
import simplejson as json
import csv
import ssl
import time
from requests.exceptions import Timeout, ConnectionError
from requests.packages.urllib3.exceptions import ReadTimeoutError

import config


 
#Authentication with twitter
auth = OAuthHandler(config.consumer_key, config.consumer_secret)
auth.set_access_token(config.access_token, config.access_secret)
 

api = tweepy.API(auth)

keywords = ["#Netflix", "Netflix", "Hotstar", "prime"]

filename="tweet"

class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:
                
            #save the tweet obj
            tweet_object = status
            
            #if tweet greater than 140 characters
            if 'extended_tweet' in tweet_object._json:
                tweet = tweet_object.extended_tweet['full_text']
            else:
                tweet = tweet_object.text
                
            '''Convert all named and numeric character references
                (e.g. &gt;, &#62;, &#x3e;) in the string s to the
                corresponding Unicode characters'''
            tweet = (tweet.replace('&amp;', '&')
                     .replace('&lt;', '<').replace('&gt;', '>')
                     .replace('&quot;', '"').replace('&#39;', "'")
                     .replace(';', " ").replace(r'\u', " "))
                
                # Save the keyword that matches the stream
            keyword_matches = []
            for word in keywords:
                if word.lower() in tweet.lower():
                    keyword_matches.extend([word])
                
            keywords_strings = ", ".join(str(x) for x in keyword_matches)
                
                
                # Save other information from the tweet
            user = status.author.screen_name
            timeTweet = status.created_at
            source = status.source
            tweetId = status.id
            tweetUrl = "https://twitter.com/" + str(user) + "/status/" + str(tweetId)
            
            if not any((('RT @' in tweet, 'RT' in tweet,
                         tweet.count('@') >= 2, tweet.count('#') >= 3))):

                # Saves the tweet information in a new row of the CSV file
                writer.writerow([tweet, keywords_strings, timeTweet,user, source, tweetId, tweetUrl])

        except Exception as e:
            print('Encountered Exception:', e)
            pass
            
def work():
    
    #opening a csv file to save the tweets
    
    with open(filename+".csv", 'w') as file:
        global writer
        writer = csv.writer(file)
        
        #header row
        writer.writerow(["Tweet", "Keywords", "Date", "User", "Source", "Tweet_Id", "Tweet_URL"])
        
        try:
            streamingAPI = tweepy.streaming.Stream(auth, StreamListener())
            streamingAPI.filter(track=keywords, languages=["en"])
            
            #if twitter rate limit
            
        except tweepy.RateLimitError:
            print("RateLimitError..waiting ~15 min to resume")
            time.sleep(1001)
            streamingAPI = tweepy.streaming.Stream(auth, StreamListener()) 
            streamingAPI.filter(track=[keywords], languages=["en"]) 
         
        except (Timeout, ssl.SSLError, ReadTimeoutError,
                ConnectionError) as exc:
            print("Timeout/connection error...waiting ~15 minutes to continue")
            time.sleep(1001)
            streamingAPI = tweepy.streaming.Stream(auth, StreamListener())
            streamingAPI.filter(track=[keywords], languages=["en"])

        # Stop temporarily when getting other errors
        except tweepy.TweepError as e:
            if 'Failed to send request:' in e.reason:
                print("Time out error caught.")
                time.sleep(1001)
                streamingAPI = tweepy.streaming.Stream(auth, StreamListener())
                streamingAPI.filter(track=[keywords], languages=["en"])
            else:
                print("Other error with this user...passing")
                pass             
            
            
            
            
if __name__ == '__main__':

    work()
            
            
            
            
            
            