[PYTHON] Specifying the date with the Twitter API

Introduction

<!-Description the beginning and outline-> I want to specify the date with the Twitter API.

environment

<!-Development environment-> ・ Windows 10 -Python3.8 ・ Twitter API v1.1

table of contents

<!-Edit title and anchor name->

  1. Problem
  2. Conclusion
  3. References

<!-Each chapter->

problem

After "q =" since: or until: If you turn on, an error will be returned

import json
from requests_oauthlib import OAuth1Session

ck = 'xxxxxxxxxxxxxxxxxxxxxxxxx' #Consumer key
cs = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Consumer secret
at = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Access token
ats = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Access token secret

twitter = OAuth1Session(ck, cs, at, ats)#OAuth authentication


API_URL = "https://api.twitter.com/1.1/search/tweets.json?q="
keyword = "Halo-RT since:2020-12-20_00:00:00_JST until:2020-12-21_00:00:00_JST"
type = "resent"
count = 50
url = API_URL + keyword + "&result_type=" + type \
+ "&count=" + str(count) + "&exclude_replies=True"
response = twitter.get(url)

response_data = json.loads(response.text)
# response_data
{
  "errors": [
    {
      "code": 32,
      "message": "Could not authenticate you."
    }
  ]
}

Conclusion

keyword = "Halo% 20-RT520until% 3A2020-12-23% 20since% 3A2020-12-22% 20" Successful

Digression

Due to the specifications of the Twitter API, it seems that only tweets within a week can be searched.

reference

Twitter API Reference

Recommended Posts

Specifying the date with the Twitter API
Tweet regularly with the Go language Twitter API
Use Twitter API with Python
Try using the Twitter API
Try using the Twitter API
Support yourself with Twitter API
Call the API with python3.
Successful update_with_media with twitter API
Hit the Twitter API after Oauth authentication with Django
Try hitting the Twitter API quickly and easily with Python
Hit the Etherpad-lite API with Python
Streamline information gathering with the Twitter API and Slack bots
Access the Twitter API in Python
Collecting information from Twitter with Python (Twitter API)
Access the Docker Remote API with Requests
Touch around the twitter list with tweepy
Tweet using the Twitter API in Python
Install by specifying the version with pip
Post from another account with Twitter API
Get holidays with the Google Calendar API
Extract sudden buzzwords with twitter streaming API
Play with puns using the COTOHA API
Be careful when retrieving tweets at regular intervals with the Twitter API
Until you use the Kaggle API with Colab
Check the date of the flag duty with Python
Specifying the module loading destination with GAE python
Play with the power usage API provided by Yahoo
It's too easy to access the Twitter API with rauth and I have her ...
Twitter OAuth with Django
Crawl the URL contained in the twitter tweet with python
[Boto3] Search for Cognito users with the List Users API
[Life hack] Women's Anna support bot with Twitter API
Second half of the first day of studying Python Try hitting the Twitter API with Bottle
Predict the gender of Twitter users with machine learning
Crawling with Python and Twitter API 1-Simple search function
Create a tweet heatmap with the Google Maps API
Extrude with Fusion360 API
Post to your account using the API on Twitter
Get comments and subscribers with the YouTube Data API
Get date with python
[Python] Split the date
Note calling the CUDA Driver API with Python ctypes
Pick up only crispy Japanese with Twitter streaming API
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
The first API to make with python Djnago REST framework
[python] Decompose the acquired Twitter timeline into morphemes with MeCab
I tried follow management with Twitter API and Python (easy)
Create a real-time auto-reply bot using the Twitter Streaming API
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Throw appointments to others with the LINE WORKS calendar API
Create a Twitter BOT with the GoogleAppEngine SDK for Python
I tried saving the DRF API request history with django-request
Tweet Now Playing to Twitter using the Spotify API. [Python]
Image download with Flickr API
Use Trello API with python
Insert the debugger with nose
Create an API with Django
Twitter graphing memo with Python
Get Twitter timeline with python
Extract Twitter data with CSV