Пример #1
0
    def get_twits_from_user(self, username):
        tweets = tweepy.Cursor(self.api.user_timeline,
                               id=username,
                               tweet_mode='extended',
                               wait_on_rate_limit=True).items()
        # Empty Array
        fake_trends = {}
        fake_trends[username] = {}
        fake_trends[username]["twits"] = []
        fake_trends[username]["url"] = username
        num_tweets = 0
        for item in tweets:
            twit_dict = {}
            twit_dict["user"] = item.user.name
            twit_dict["created_at"] = str(item.created_at)
            twit_dict["text"] = item.full_text
            twit_dict["retweet_count"] = item.retweet_count
            fake_trends[username]["twits"].append(twit_dict)
            num_tweets += 1
        filename = './data/%s_%s.json' % (
            username, datetime.datetime.now().strftime("%d%m%y"))
        with open(filename, 'w') as json_file:
            json.dump(fake_trends, json_file)
            gui.out("done.")

        status = None
        if tweets: status = 1
        return num_tweets, filename
Пример #2
0
def init():
    config = configparser.ConfigParser()
    if not config.read('./config/config.ini'):
        gui.out(" === RNN CONFIG === ")
        num_epochs=gui.inp("num_epochs: ")
        save_epochs=gui.inp("save_epochs: ")
        config["RNN"]={}
        config["RNN"]["num_epochs"]=num_epochs
        config["RNN"]["save_epochs"]=save_epochs
        
        gui.out(" === TWITTER CONFIG === ")
        consumer_key=gui.inp("consumer_key: ")
        consumer_secret=gui.inp("consumer_secret: ")
        access_token=gui.inp("access_token: ")
        access_token_secret=gui.inp("access_token_secret: ")
        COUNTRY_WOE_ID=gui.inp("COUNTRY_WOE_ID (by default 753692): ")
        include_promoted_tweets=gui.inp("include_promoted_tweets (0 or 1): ")
        max_trends_number =gui.inp("max_trends_number (recommended 5): ")
        config["TWITTER"] = {}
        config["TWITTER"]["consumer_key"]=consumer_key
        config["TWITTER"]["consumer_secret"]=consumer_secret
        config["TWITTER"]["access_token"]=access_token
        config["TWITTER"]["access_token_secret"]=access_token_secret
        if COUNTRY_WOE_ID == "": COUNTRY_WOE_ID=753692
        config["TWITTER"]["COUNTRY_WOE_ID"]=COUNTRY_WOE_ID
        config["TWITTER"]["include_promoted_tweets"]=include_promoted_tweets
        config["TWITTER"]["max_trends_number "]=max_trends_number 
        with open('./config.ini', 'w') as configfile:
            config.write(configfile)
    return config
Пример #3
0
    def load(self):
        header = """\n=== TwitterTrendingBot === 
        Load
        [0] From twits file
        [1] From a previous trained model
        [2] Back
        """
        flag = 1
        while flag:
            gui.out(header)
            i = gui.inp("> ")
            try:
                mode = int(i)
                if mode in [0, 1, 2]: flag = 0
            except:
                gui.out("<%s> no es un número válido." % (i))

        if mode == 0:
            filename = file_chooser.by_folder("./data", [".txt"])
            file, file_extension = os.path.splitext(filename)
            if file_extension == ".json":
                sort_twits.sort_by_retweets(filename)
                filename = file_formatter.json_to_txt(filename)
            self.myTextGenRnn = text_generator.TextGenRnn()
            self.train(filename)
        elif mode == 1:
            filename = file_chooser.by_folder("./trained_models", [".hdf5"])
            self.myTextGenRnn = text_generator.TextGenRnn()
            self.myTextGenRnn.load_model(filename)

            mode = 0
            header = """\n=== TwitterTrendingBot === 
            Model loaded.
            [0] Train
            [1] Generate
            [2] Back
            """
            main_flag = 1
            while main_flag:
                flag = 1
                while flag:
                    gui.out(header)
                    i = gui.inp("> ")
                    try:
                        mode = int(i)
                        if mode in [0, 1, 2]: flag = 0
                    except:
                        gui.out("<%s> no es un número válido." % (i))

                if mode == 0:
                    gui.out("choose a file to train with.")
                    filename = file_chooser.by_folder("./data", [".txt"])
                    self.train(filename)

                if mode == 1:
                    self.myTextGenRnn.textgen.generate()
                else:
                    main_flag = 0
        return
Пример #4
0
    def trending_twit_maker(self):
        gui.out("Retrieving twits...")
        filename = None

        myTwitterAPI = twitter_api.TwitterAPI()
        filename = myTwitterAPI.get_trending_twits()
        sort_twits.sort_by_retweets(filename)
        filename = file_formatter.json_to_txt(filename)

        self.myTextGenRnn = text_generator.TextGenRnn()
        self.train(filename)

        return
Пример #5
0
    def user_twit_maker(self):
        myTwitterAPI = twitter_api.TwitterAPI()
        username = gui.inp("username > ")
        status, filename = myTwitterAPI.get_twits_from_user(username)
        if status > 100:
            filename = file_formatter.json_to_txt(filename,
                                                  show_username=False)

            self.myTextGenRnn = text_generator.TextGenRnn()
            self.train(filename)
        elif status > 1:
            gui.out(
                "we couldn't retrieve all the tweets, or they aren't enough (they were %s)"
                % (status))
        else:
            gui.out("please, try again.")
        return
Пример #6
0
    def new(self):
        header = """\n=== TwitterTrendingBot === 
        [0] Trending twits
        [1] By username
        [2] Back
        """
        flag = 1
        while flag:
            gui.out(header)
            i = gui.inp("> ")
            try:
                mode = int(i)
                if mode in [0, 1, 2]: flag = 0
            except:
                gui.out("<%s> no es un número válido." % (i))

        if mode == 0:
            self.trending_twit_maker()
        if mode == 1:
            self.user_twit_maker()
        else:
            sys.exit()
        return
Пример #7
0
    def run(self):
        mode = 0
        header = """\n=== TwitterTrendingBot === 
        [0] New
        [1] Load model
        [2] Exit
        """
        while True:
            flag = 1
            while flag:
                gui.out(header)
                i = gui.inp("> ")
                try:
                    mode = int(i)
                    if mode in [0, 1, 2]: flag = 0
                except:
                    gui.out("<%s> no es un número válido." % (i))

            if mode == 0:
                self.new()
            if mode == 1:
                self.load()
            else:
                sys.exit()
Пример #8
0
    def get_trending_twits(self):
        filename = filename = './data/trends_%s.json' % (
            datetime.datetime.now().strftime("%d%m%y"))

        COUNTRY_WOE_ID = self.config["TWITTER"]["COUNTRY_WOE_ID"]
        country_trends = self.api.trends_place(COUNTRY_WOE_ID)
        trends = json.loads(json.dumps(country_trends, indent=1))

        num = 1
        for trend in trends[0]["trends"]:
            if num <= int(self.config["TWITTER"]["max_trends_number"]):
                if (not self.config["TWITTER"]["include_promoted_tweets"]
                        and not trend["promoted_content"]) or (
                            self.config["TWITTER"]["include_promoted_tweets"]):
                    trend_name = (trend["name"]).strip("#")
                    trend_url = trend["url"]

                    self.my_trends[trend_name] = {}
                    self.my_trends[trend_name]["url"] = trend_url

                    gui.out(">>> " + trend_name + " <<<")
                    gui.out((num, self.config["TWITTER"]["max_trends_number"]))

                    querry = "%s -filter:retweets" % (trend_name)
                    search = tweepy.Cursor(self.api.search,
                                           q=querry,
                                           lang="es",
                                           tweet_mode='extended',
                                           wait_on_rate_limit=True).items(200)
                    self.my_trends[trend_name]["twits"] = []
                    for item in search:
                        if (not item.retweeted) and ('RT @'
                                                     not in item.full_text):
                            twit_dict = {}
                            twit_dict["user"] = item.user.name
                            twit_dict["created_at"] = str(item.created_at)
                            twit_dict["text"] = item.full_text
                            twit_dict["retweet_count"] = item.retweet_count
                            self.my_trends[trend_name]["twits"].append(
                                twit_dict)
                            #print (" <@user> "+item.user.name)
                            #print (item.created_at)
                            #print (item.text)
                            #print (item.retweet_count)
                    num += 1
        with open(filename, 'w') as json_file:
            json.dump(self.my_trends, json_file)
        gui.out("done.")
        return filename