Запрос на твит во временном интервале с помощью tweepy

Я пытаюсь использовать tweepy для запроса твита, который попадает в определенный интервал.

Используя приведенный ниже фрагмент для интервалов дней, работает:

page_count = 0
for tweets in tweepy.Cursor(api.search,q=query,count=100,result_type="recent",include_entities=True,since= "2016-02-18", until= "2016-03-18" ).pages():
    page_count+=1
    print tweets[0].text.encode('utf-8')
    if page_count >=20:
        break

но я хочу, чтобы это было в пределах временного интервала, например (между 06:00 и 13:00). Я пробовал использовать этот запрос, но он ничего не возвращает:

for tweets in tweepy.Cursor(api.search,q=query,count=100,result_type="recent",include_entities=True,since= "2016-03-18 05:30", until= "2016-03-18 08:30" ).pages():
    page_count+=1
    print tweets[0].text.encode('utf-8')
    if page_count >=20:
        break

Как мне это сделать. Спасибо


person Nosakhare Belvi    schedule 18.03.2016    source источник
comment
Насколько я знаю, это невозможно. Кроме того, такой метод не упоминается в документации.   -  person Anmol Singh Jaggi    schedule 18.03.2016


Ответы (1)


Возможно, это не лучший способ, но он работает для меня.

Мой подход заключался в том, чтобы сначала получить текущую дату, а затем использовать ее в запросе

currentTime = str(datetime.datetime.now().date())
for tweets in tweepy.Cursor(api.search,q=query,count=1,result_type="recent",include_entities=True,since = currentTime).pages():
    tweetTime = tweets[0].created_at # get the current time of the tweet
    now = datetime.datetime.now()
    interval = now - tweetTime # subtract tweetTime from currentTime
    if interval.seconds <= 3900: #get interval in seconds and use your time constraint in seconds (mine is 1hr and 5 mins = 3900secs)
            print tweets[0].text.encode('utf-8')
            print(tweets[0].created_at)
        else:
            shouldContinue = False
            print(interval.seconds)
            print(tweets[0].created_at)

        print('\n')

        if not shouldContinue: # check if tweet is still within time range. Tweet returned are ordered according to recent already.
            print('exiting the loop')
            break
person Nosakhare Belvi    schedule 19.03.2016