422 error even when using premium Twitter.Api


#1

Using Twitter.Api and can’t seem to use the fromdate todate functions or the is:retweets to filter data. Any help?

from TwitterAPI import TwitterAPI
import xlsxwriter
import builtins

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
api = TwitterAPI(consumer_key, consumer_secret, access_token, access_token_secret)

def print_ucs2(*args, print=builtins.print, **kwds):
   args2 = []
   for a in args:
      a = str(a)
      if max(a):
         b = a.encode('utf-16le', 'surrogatepass')
         chars = [b[i:i+2].decode('utf-16le', 'surrogatepass')
                  for i in range(0, len(b), 2)]
         a = ''.join(chars)
      args2.append(a)
   print(*args2, **kwds)
builtins._print = builtins.print
builtins.print = print_ucs2

def tweet_to_xlsx(tweet_list):
   workbook = xlsxwriter.Workbook('ElonMusk.xlsx')
   worksheet = workbook.add_worksheet()
   row=0
   col=0
   for name, tweet in tweet_list:
      worksheet.write(row, col, name)
      worksheet.write(row, col + 1, tweet)
      row += 1
   workbook.close()

tweet_list =[]
r = api.request('tweets/search/fullarchive/:CEOs', {'query' : 'from:elonmusk lang:en -(is:retweet) -(is:reply) fromDate:201801010001 toDate:201901010001'})
for item in r:
     print(item['created_at'])
     print(item['text'])
     tweet_list.append([item['created_at'], item['text']])
     tweet_to_xlsx(tweet_list)


#2

These go in as separate parameters, not part of the query

so i think it should be something like `{‘query’ : ‘from:elonmusk lang:en’, ‘fromDate’:‘2018…’, ‘toDate’:‘2018…’} but i can’t double check right now

Also, is:retweet and is:reply are for paid premium and fail when used in “sandbox” premium mode