Ejemplo n.º 1
0
  def search_and_save_users(self):
    language = 'en'
    #use the lines below if you want to target another language besides english
    language = raw_input("Which language do you want to target 'es'espanish, 'en'english: ")
    language = language.lower()

    query = raw_input("Which words do you want to search? ")

    max_tweets = int(input("How many tweets do you want to retrieve? "))
    tweets = self.tw.search_tweets(query, language, max_tweets)

    list_user=[]
    list_hashtag=[]
    list_tweets =[]
    print('users found saved to the database: ')
    for elem in tweets:
      list_user.append({'user_id': elem.user.id, 'screen_name': elem.user.screen_name, 'description': elem.user.description})
      list_hashtag.append({'user_of_hashtag': elem.user.id, 'hashtag_text': query})
      list_tweets.append({'user_tweets': elem.user.id, 'tweet_message': elem.text, 'created_date': elem.created_at})

    for itemr, itemh, itemt in zip(list_user, list_hashtag, list_tweets):
      try:
        #The following is the same as the short version Recruited.create(**itemr)
        #a= Recruited.create(user_id = itemr['user_id'],screen_name = itemr['screen_name'],description=itemr['description'])
        a = Recruited.create(**itemr)
        b = Hashtag.create(**itemh)
        c = Tweets.create(**itemt)
        a.save()
        b.save()
        c.save()
      except IntegrityError:
        pass

    return list_user
Ejemplo n.º 2
0
    def search_and_save_users(self):
        language = 'en'
        #use the lines below if you want to target another language besides english
        #language = input("Which language do you want to target 'es'espanish, 'en'english: ")
        #language = language.lower()

        query = input("Which words do you want to search ?")

        max_tweets = int(input("How many tweets do you want to retrieve? "))
        searched_tweets = [
            status for status in tweepy.Cursor(
                twitter.api.search, q=query, lang=language).items(max_tweets)
        ]

        list_user = []
        list_hashtag = []
        list_tweets = []
        print('users found saved to the database: ')
        for elem in searched_tweets:
            #print(elem.user.screen_name)
            #print(elem.user.id)
            #print(elem.user.description)
            #print(elem.text)
            #print(elem.created_at)
            #print(query)
            list_user.append({
                'user_id': elem.user.id,
                'screen_name': elem.user.screen_name,
                'description': elem.user.description
            })
            list_hashtag.append({
                'user_of_hashtag': elem.user.id,
                'hashtag_text': query
            })
            list_tweets.append({
                'user_tweets': elem.user.id,
                'tweet_message': elem.text,
                'created_date': elem.created_at
            })
            print(elem.user.id)

        for itemr, itemh, itemt in zip(list_user, list_hashtag, list_tweets):
            try:

                #The following is the same as the short version Recruited.create(**itemr)
                #a= Recruited.create(user_id = itemr['user_id'],screen_name = itemr['screen_name'],description=itemr['description'])
                a = Recruited.create(**itemr)
                b = Hashtag.create(**itemh)
                c = Tweets.create(**itemt)
                a.save()
                b.save()
                c.save()
            except IntegrityError:
                pass

        return list_user
Ejemplo n.º 3
0
    def retrieve_users(self, target_hashtag_idx, the_hashtags):
        """
         #retrieve all users that have used the selected hashtags and that have not received tweets recently (last 24 hours)
        :return: the list of selected  users
        """
        now = datetime.datetime.now()

        now = datetime.datetime.now()
        pop_user = []
        final_list = []
        print(target_hashtag_idx)
        the_target_hashtag = the_hashtags[target_hashtag_idx - 1]
        # this query gets the user_ids of those users that tweeted the selected hashtag
        users = Hashtag.select(Hashtag.user_of_hashtag).where(
            Hashtag.hashtag_text == the_target_hashtag)
        users_list = []
        for item in users:
            users_list.append(item.user_of_hashtag_id)
        print(users_list)
        selected_user = []
        # this query selects the datetime of the users that where retrieved with the last query (users who tweeted certain hashtag)
        for item in users_list:
            selected_user = SentDate.select(
                SentDate.user_sent, SentDate.date_tweet_sent,
                SentDate.tweet_sent_message).where(SentDate.user_sent == item)
            # if the user has been sent a tweet in the last 24 hours skip it
            print("selected users")
            for item in selected_user:
                print(item.user_sent_id)
            for item in selected_user:
                # print("printing selected user")
                # print (item.date_tweet_sent)
                # print(item.tweet_sent_message)
                # print(item.user_sent_id)

                date_retrieved = datetime.datetime.strptime(
                    item.date_tweet_sent, '%Y-%m-%d %H:%M:%S.%f')
                print('sent', date_retrieved)
                print('48 hours', now - datetime.timedelta(hours=48))
                if (now - datetime.timedelta(hours=48)) < date_retrieved:
                    print('Not have passed 48 hours, cannot send tweet')

                    if item.user_sent_id not in pop_user:
                        pop_user.append(item.user_sent_id)

        print("user to pop: ", pop_user)
        # only keeps the users that who didn't receive a tweet in the last 48 hours
        print('user_list', users_list)
        for elem in pop_user:
            users_list = [value for value in users_list if value != elem]

        print("final list", users_list)
        return users_list
Ejemplo n.º 4
0
    def get_hashtags(self):
        # Option 1: Retrieve the tweet to send from a previously filled table in the database
        # Option 2: Create the tweet to send on the fly (easier to do)
        """
         #retrieve all the hashtags from database
        :return: the lis of the hashtags
        """
        hashtag = Hashtag.select(Hashtag.hashtag_text).distinct()
        hashtag_list = []
        for item in hashtag:
            hashtag_list.append(item.hashtag_text)

        return hashtag_list
Ejemplo n.º 5
0
    def get_hashtags(self):
        # Option 1: Retrieve the tweet to send from a previously filled table in the database
        # Option 2: Create the tweet to send on the fly (easier to do)

        """
         #retrieve all the hashtags from database
        :return: the lis of the hashtags
        """
        hashtag = Hashtag.select(Hashtag.hashtag_text).distinct()
        hashtag_list = []
        for item in hashtag:
            hashtag_list.append(item.hashtag_text)

        return hashtag_list
Ejemplo n.º 6
0
    def retrieve_users(self, target_hashtag_idx, the_hashtags):
        """
         #retrieve all users that have used the selected hashtags and that have not received tweets recently (last 24 hours)
        :return: the list of selected  users
        """
        now = datetime.datetime.now()

        now = datetime.datetime.now()
        pop_user = []
        final_list = []
        print(target_hashtag_idx)
        the_target_hashtag = the_hashtags[target_hashtag_idx - 1]
        # this query gets the user_ids of those users that tweeted the selected hashtag
        users = Hashtag.select(Hashtag.user_of_hashtag).where(Hashtag.hashtag_text == the_target_hashtag)
        users_list = []
        for item in users:
            users_list.append(item.user_of_hashtag_id)
        print(users_list)
        selected_user = []
        # this query selects the datetime of the users that where retrieved with the last query (users who tweeted certain hashtag)
        for item in users_list:
            selected_user = SentDate.select(SentDate.user_sent, SentDate.date_tweet_sent, SentDate.tweet_sent_message).where(SentDate.user_sent == item)
            # if the user has been sent a tweet in the last 24 hours skip it
            print("selected users")
            for item in selected_user:
                print(item.user_sent_id)
            for item in selected_user:
                # print("printing selected user")
                # print (item.date_tweet_sent)
                # print(item.tweet_sent_message)
                # print(item.user_sent_id)

                date_retrieved = datetime.datetime.strptime(item.date_tweet_sent, '%Y-%m-%d %H:%M:%S.%f')
                print('sent', date_retrieved)
                print('48 hours', now - datetime.timedelta(hours=48))
                if (now - datetime.timedelta(hours=48)) < date_retrieved:
                    print('Not have passed 48 hours, cannot send tweet')

                    if item.user_sent_id not in pop_user:
                        pop_user.append(item.user_sent_id)

        print("user to pop: ", pop_user)
        # only keeps the users that who didn't receive a tweet in the last 48 hours
        print('user_list', users_list)
        for elem in pop_user:
            users_list = [value for value in users_list if value != elem]

        print("final list", users_list)
        return users_list