1

I am trying to retrieve historical data using searchtweets API but I keep getting twitter 422 error. Weird thing is, before this code I had a previous code where it had worked perfectly. But after this, the old code doesn't work either

I have followed the documentation, double checked that I have full archive access and even regenerated my tokens and access keys.

This is my python code :

import json
import csv

from searchtweets import collect_results, load_credentials, gen_rule_payload

enterprise_search_args = load_credentials("twitter_keys.yaml", yaml_key = "search_tweets_api", env_overwrite = False)

from_date = "2016-06-01"
to_date = "2016-06-23"

with open('map_data/london_borough.csv', 'r', newline = "") as csvFile:
    boroughs = list(csv.reader(csvFile, delimiter = ','))
    for num, borough in enumerate(boroughs):
        if num:
            name = boroughs[0]
            box_coord = borough[5]
            if box_coord:
                box_coord = '['+box_coord.replace(',',' ')+']'
                print(box_coord)
                rule = gen_rule_payload("brexit", results_per_call = 100, from_date = from_date, to_date = to_date)
                rule = json.loads(rule)
                rule['bounding_box'] = box_coord
                rule = json.dumps(rule)
                print(rule)
                tweets = collect_results(rule, max_results = 500, result_stream_args = enterprise_search_args)
                with open('%s.csv'%(name), 'w', newline = "") as writeFile:
                    for tweet in tweets:
                        writeFile.write('%s, %s, %s\n'%(tweet.name, tweet.created_at_string, tweet.all_text))

And my yaml file :

search_tweets_api:
account_type: premium
endpoint: https://api.twitter.com/1.1/tweets/search/fullarchive/StreamingTweets.json
consumer_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
consumer_secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Upon running the above code, I get this on the console:

Grabbing bearer token from OAUTH
[-0.105400 51.507354 -0.074673 51.521028]
{"query": "brexit", "maxResults": 100, "toDate": "201606230000", "fromDate": "201606010000", "bounding_box": "[-0.105400 51.507354 -0.074673 51.521028]"}
retrying request; current status code: 422
retrying request; current status code: 422

My computer clock is set to UTC co-ordinated universal time, I have quadruple checked my bearer token and regenerated a couple of times, the application access is set to read, write and access direct messages but still have no idea how to resolve this.

Does anyone know of a solution to this or where i may be going wrong? Any help will be greatly appreciated!

Tanmim Hanifa
  • 73
  • 1
  • 1
  • 9
  • Why are you converting box_coord into a string? I'd have thought the original array would be better, because it should be serialized into a JSON array, and I guess that's what the API wants. – Rup Apr 26 '18 at 00:36
  • @Rup how would i serialize it into a json array? Can you show me please – Tanmim Hanifa Apr 26 '18 at 01:27

0 Answers0