1
\$\begingroup\$

What i have done here is used 2 scripts in which from my main file I am using streaming api for particular handles and that data being directly pushed to mongodb.

I pick that data -> copy necessary fields into processed_tweets and reply_tweets.

I run a sentiment analysis on the tweets instantly.And mark the negative tweets and those with negative marked I reply them something of my choice. I know a lot of improvement can be done in this code.

Below script is twee_2.py which is being used for accessing twitter api.

# Script to follow Twitter users from text file containing user IDs (one per line)

# Header stuff I've just thrown in from another script to authenticate

import json
import time
import tweepy
import pprint
import pymongo
from tweepy.parsers import RawParser
from auth import consumer_key,consumer_secret,access_key,access_secret
from datetime import datetime



auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_key, access_secret)

rawParser = RawParser()

api = tweepy.API(auth_handler = auth, parser = rawParser)

# Open to_follow.txt




# Follow everyone from list?!
def follow_user(list_user):

        try:
            api.create_friendship(list_user)
        except tweepy.TweepError as e:
            print e
            pass


#retweet from the list     
def retweet(list_id):

        try:
            api.retweet(list_id)
        except tweepy.TweepError as e:
            print e


#first time tweet
def tweet_user(list_user):

        text = "how can we help you?"

        try:
            api.update_status(status="@"+list_user+" "+text)
        except tweepy.TweepError as e:
            print e
            pass

#tweet favourite
def favourite_tweet(tweet_id):

    try:
        api.create_favorite(tweet_id)
    except tweepy.TweepError as e:
        print e

#second time reply
def second_reply(user_name):
        text = "See how we work: www.goo.gl/uvZnNZ .The app is still being built,so plz share details at: www.amicshop.com/complaints"
        try:
            api.update_status(status="@"+user_name+" "+text)
        except tweepy.TweepError as e:
            print e

#get the tweet text
def get_tweet(tweet_id):

    return api.get_status(tweet_id)

print "Done."

Below is main_script.py which is interacting with mongodb and it is being run in the infinite loop in future I want to make a bash script and run the below script into infinite loop and deploy it on aws.

import pymongo
from sklearn.externals import joblib
import re
import itertools
import json
from multiprocessing import Process
import sys
import twee_2
from twee_2 import follow_user,retweet,tweet_user,get_tweet,second_reply,favourite_tweet
import time

class twitt():

    def __init__(self):

        connection = pymongo.MongoClient("mongodb://localhost")
        connection_2 = pymongo.MongoClient("mongodb://localhost")

        twitter = connection_2.twitter_test
        self.tweets = twitter.tweets_test
        self.processed_tweets = twitter.processed_tweets
        self.reply_tweets = twitter.reply_tweets

    def stage_copy(self):


        z = self.tweets.find({},{"text":True,"id_str":True,"user":True,"_id":True,"in_reply_to_screen_name":True,"in_reply_to_status_id_str":True})

        for x in z:

            a = x["user"]
            print a["screen_name"]

            try:


                if (x["in_reply_to_screen_name"] =="nik_amic"):
                    print "fuck u"
                    self.reply_tweets.insert({"_id":x["_id"],"text":x["text"],"screen_name":a["screen_name"],"tweet_id":x["in_reply_to_status_id_str"]})

                elif (x["text"].lower().split("@")[0].strip() !="rt"):
                    print "fuck u2"
                    self.processed_tweets.insert({"_id":x["_id"],"text":x["text"].lower(),"screen_name":a["screen_name"],"tweet_id":x["id_str"],"sentiment":-1})
            except:pass

        array = []
        array1 = []
        maal = self.processed_tweets.find({},{"text":True,"_id":True})

        for masala in maal:
            array.append(masala["text"])
            array1.append(masala["_id"])

        for text,id in zip(array,array1):   
            self.stage_sentiment(text,id)



    def stage_sentiment(self,text,id):




                clf = joblib.load('/home/nikhil/Desktop/data_flow/twitter_script/DUMP/ml_dump.pkl') 


                v = clf.predict([text])

                if v =='N':

                    self.processed_tweets.update({"_id":id},{"$set":{"sentiment":"neg"}})
                else:
                    self.processed_tweets.update({"_id":id},{"$set":{"sentiment":"pos"}})

    def actions(self,twee_2):

        cursor = self.processed_tweets.find({"$and":[{"sentiment":{"$ne":-1}},{"sentiment":{"$ne":"pos"}}]},
            {"_id":True,"screen_name":True,"tweet_id":True,"text":True})


        for all in cursor:
            if all["text"].split("@")[0].strip() !="RT":
                twee_2.favourite_tweet(all["tweet_id"])#favourite
                time.sleep(10)

                twee_2.retweet(all["tweet_id"]) #retweet

                twee_2.follow_user(all["screen_name"])#follow
                time.sleep(15)

                twee_2.tweet_user(all["screen_name"])#tweet




    def reply(self,twee_2):

        cursor = self.reply_tweets.find({},{"screen_name":True,"text":True,"_id":True,"tweet_id":True})

        for all in cursor:

            #cursor_1 = self.processed_tweets.find({"tweet_id":all["tweet_id"]},{"text":True,"_id":False})
            cursor_1 = get_tweet(all["tweet_id"])
            print all["tweet_id"]


            a = json.loads(cursor_1)

            for key,value in a.iteritems():
                if key=="text":

                    if "how can we help you?" in value:

                        second_reply(all["screen_name"])

                #elif "See how we work: www.goo.gl/uvZnNZ.The app is still being built,so plz share details at: www.amicshop.com/complaints" in text:
                #   third_reply(all["screen_name"]) 
if __name__=='__main__':

    obj = twitt()
    while True:
        p1 = Process(target = obj.stage_copy())
        p1.start()

        p2 = Process(target = obj.actions(twee_2))
        p2.start()

        p3 = Process(target = obj.reply(twee_2))
        p3.start()

The code is running fine as it is now. Please tell me what I can improve in terms of logic and code base.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.