Exemple #1
0
def fetch_tweets(api, by_what, username_or_query, last_id):
    tweets_table = []
    tweets_table, error_message, last_fetched_id = init_params()
    count = get_count(last_id)
    try:
        if by_what == 'username':
            username = username_or_query
            tweets_table, last_fetched_id = create_tweets_table(
                api,
                api.user_timeline(username, tweet_mode='extended',
                                  count=count), last_id)
        elif by_what == 'newsfeed':
            tweets_table, last_fetched_id = create_tweets_table(
                api, api.home_timeline(tweet_mode='extended', count=count),
                last_id)
        elif by_what == 'query':
            query = username_or_query
            tweets_table, last_fetched_id = create_tweets_table(
                api, api.search(query, tweet_mode='extended', count=count),
                last_id)
        else:
            g_.log_entry(
                "The user needs to enter how s/he wants to fetch tweets.",
                g_.const_error)
    except Exception as e:
        error_message = str(e)
        return [], 0, error_message

    return tweets_table, last_fetched_id, error_message
def write_to_trackers_list(type_, name, username):
    filename = './lists/' + type_ + '.csv'
    file_write_to = open(filename, "a+", encoding='utf-8')
    file_write_to.write(name + "," + username + "\n")
    g_.log_entry(name + "," + username + " added to the tracking list.",
                 g_.const_info)
    file_write_to.close()
Exemple #3
0
import fetch_tweets as fetch
import global_ as g_
import time

api = g_.setup()

run_always = True

while run_always == True:
    search_term = input("Enter the seach term: ")
    last_fetched_id = g_.get_last_fetched_id(search_term)
    tweets_table, last_fetched_id, error_message = fetch.fetch_tweets(
        api, "query", search_term, last_fetched_id)
    if len(tweets_table) > 0:
        g_.dump_file(search_term, tweets_table)
        g_.update_last_fetched(search_term, last_fetched_id)
        g_.log_entry(
            str(len(tweets_table)) + " tweets for search term " +
            str(search_term), g_.const_info)

        for tweet in tweets_table:
            print(tweet[3])
            print("========")
    else:
        g_.log_entry("No new tweets for search term " + str(search_term),
                     g_.const_info)
    time.sleep(300)
api = g_.setup()

run_always = True

while run_always == True:

    users_list_file = open("./lists/politicians.csv", "r", encoding="utf-8")
    users_list = users_list_file.read()
    """
    Guide: 
    tweets_table, last_fetched_id, error_message = fetch_tweets(api, "username", username, last_id)
    tweets_table, last_fetched_id, error_message = fetch_tweets(api, "newsfeed", '', last_id)
    tweets_table, last_fetched_id, error_message = fetch_tweets(api, "query", query, last_id)
    """
    for user in users_list.split("\n"):
        if len(user) > 0:
            full_name = user.split(",")[0]
            username = user.split(",")[1]
            last_fetched_id = g_.get_last_fetched_id(username)
            tweets_table, last_fetched_id, error_message = fetch.fetch_tweets(
                api, "username", username, last_fetched_id)
            if len(tweets_table) > 0:
                g_.dump_file(username, tweets_table)
                g_.update_last_fetched(username, last_fetched_id)
                g_.log_entry(
                    str(len(tweets_table)) + " tweets from " + str(full_name),
                    g_.const_info)
            else:
                g_.log_entry("No new tweets from " + str(full_name),
                             g_.const_info)
    time.sleep(300)
api = g_.setup()
acceptable_type_s = [
    "politicians", "media", "industrialists", "sportspersons", "influencers"
]
mode = input(
    "Single link or import from file? Possible Answers: [single | file]")
if mode == 'single':
    add_more = True
    while add_more == True:
        type_ = input(
            "Choose --> Politicians | Media | Industrialists | Sportspersons | Influencers :"
        ).lower()
        if type_ not in acceptable_type_s:
            print("Please enter a valid option.")
        else:
            link = input("Enter the Twitter link: ").strip()
            import_by_link(link, type_)
            choice = input("Add another one? ").strip()
            if choice in ["yes", "yea", "ok"]:
                add_more = True
            else:
                add_more = False
if mode == 'file':
    type_ = input(
        "Choose --> Politicians | Media | Industrialists | Sportspersons | Influencers :"
    ).lower()
    if type_ not in acceptable_type_s:
        g_.log_entry("This is not an acceptable name.", g_.const_error)
    else:
        import_by_file(type_)