hyde
hyde

Reputation: 53

Twitter API v2 in Python - issues with datetime conversion

I upgraded my Python-Twitter API access from v1 to v2. But as it seems, Twitter does not any longer accept my timestamp-formats for the updated functions start_time and end_time. For the v1 API I was using simple date-time strings such as 20190304205520. But when I try to use these strings with v2, the console gives me the following error "The `start_time` query parameter value [20190304205520] is not a valid RFC3339 date-time.

To fix this, I applied the following code to convert my strings into the ISO format the Twitter documentation suggests to use, such as 2019-03-04T20:55:20Z.

start_time = pd.to_datetime(start_time, format='%Y%m%d%H%M')
start_time = start_time.isoformat()

The output is: '2019-03-04T20:55:20'

However, when I put these results into the gen_request function I get another error that says: unconverted data remains: 00:00:00Z

And in both cases I can't run my query.

I am using the regular searchtweets library on python, and my code is as follows:

query = gen_request_parameters(string, results_per_call=100, start_time=from_date, end_time=to_date). from_date and to_date by default hold a string format such as '20190304205520'.

To collect tweets, I use the function:

tweets = collect_results(query,
                          max_tweets= 500,
                          result_stream_args=search_args)

I know this is probably very simple, but I can't really wrap my head around it. It has been a while that I was using Python to access Twitter and I would highly appreciate if somebody could help me out.

Upvotes: 0

Views: 1783

Answers (1)

bodily11
bodily11

Reputation: 604

I don't know if you ever found a solution to this, but I just ran into this error so I thought it would be nice to document a solution for anyone searching.

Steps:

  1. pip install searchtweets-v2
  2. Use Twitter API V2 documentation recommended documentation to do something like the following:

from searchtweets import ResultStream, gen_request_parameters, load_credentials

search_args = load_credentials(filename="~/.twitter_keys.yaml",
                 yaml_key="search_tweets_v2",
                 env_overwrite=False)

query = gen_request_parameters(query, start_time=start_time, results_per_call=100, expansions='author_id,geo.place_id', tweet_fields='created_at,geo',user_fields='description,location')

rs = ResultStream(request_parameters=query,
                    max_results=10000,
                    max_pages=10000,
                    max_tweets=10000,
                    **search_args)

The error I received was "unconverted data remains: :00Z". When looking at the error, I noticed the start_time format it expects does NOT include seconds. So I reformatted my start timestamp to the format YYYY-MM-DDTHH:mm (note, no Z at the end and no seconds), then it worked just fine.

Not sure why the documentation says to use YYYY-MM-DDTHH:mm:ssZ. Maybe I somehow got an older version of the V2 api when I pip installed it.

Upvotes: 0

Related Questions