S
S
S_ith2015-01-20 11:38:59
Programming
S_ith, 2015-01-20 11:38:59

How can I implement the download of all tweets from the Twitter feed for the last 24 hours, say?

As far as I understand, by simply scrolling down the page, you can only download the last three hours.
I subscribed to a bunch of people on topics that are very interesting to me.
And I would like to read everything that appeared from them in the tape for a day or for a longer period of time, if at all possible.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Myvrenik, 2015-01-20
@S_ith

Create keys on https://apps.twitter.com/
Used:
https://pypi.python.org/pypi/twitter/
https://pypi.python.org/pypi/python-dateutil/
And more, if the script reaches the very end of the tape, it may loop or throw an exception, did not check this situation.

from pathlib import Path
import dateutil.parser
import datetime
import twitter

APP_NAME = "TwitterScrap"
CONSUMER_KEY = "xxxxxxxxxxxxx" # Берём отсюда: https://apps.twitter.com/
CONSUMER_SECRET = "xxxxxxxxxxxxx" # Берём отсюда: https://apps.twitter.com/

credentials_file = Path("twitter_oauth_token.txt")
if not credentials_file.exists():
    oauth_token, oauth_secret = twitter.oauth_dance(APP_NAME, CONSUMER_KEY, CONSUMER_SECRET, str(credentials_file))
else:
    oauth_token, oauth_secret = twitter.read_token_file(str(credentials_file))

oauth = twitter.OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET)
twitter_handler = twitter.Twitter(auth=oauth)

date = datetime.date.today()
until_date = date - datetime.timedelta(1) # Выводим твиты до вчерашнего дня
last_id = None
while(date > until_date):
    if last_id is not None:
        time_line = twitter_handler.statuses.home_timeline(exclude_replies=True, max_id=last_id)
    else:
        time_line = twitter_handler.statuses.home_timeline(exclude_replies=True)

    date = dateutil.parser.parse(time_line[-1]['created_at']).date()
    last_id = time_line[-1]['id']

    for i in time_line:
        print(i['user']['name'], "(@" + i['user']['screen_name'] + ")", i['created_at'])
        print(i['text'])
        print('--------------------')

M
Max, 2015-01-20
@AloneCoder

I made myself a grabber and through api I collect a crown

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question