0

I am using tweepy.Cursor to collect tweets and it works ok, I want to improve my code for limiting the collection of tweets to given pair of coordinates through bounding_box. I have very limited knowledge of python. I need help in improving this code. Code is as follows:

import sys
import os
import jsonpickle
import tweepy
import datetime
import time

consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

maxTweets = 1000000
tweetCount = 0

stamp = time.strftime('%b%d%y%H%M')
filename = 'AFG'+stamp+'.json'

print(filename)

searchQuery = ('afghanistan OR elections')

with open(filename, 'w') as f:
    for tweet in tweepy.Cursor(api.search, q=searchQuery, tweet_mode='extended').items(maxTweets):
        f.write(jsonpickle.encode(tweet._json, unpicklable=False) + '\n')
        tweetCount += 1

print("Downloaded {0} tweets.". format(tweetCount))

I don't know where to put bounding_box in above code. Bounding_box for Afghanistan is: 'AF': ('Afghanistan', (60.5284298033, 29.318572496, 75.1580277851, 38.4862816432))

  • This is a duplicate of https://stackoverflow.com/questions/22889122/how-to-add-a-location-filter-to-tweepy-module. – mike harrington Sep 29 '19 at 05:13
  • I doubt it as duplicate because my code does not use function and it deploys `tweepy.Cursor`. I want to avoid entirely new code based on your suggestion. – Ambrish Dhaka Sep 29 '19 at 05:28
  • If I use `geoLoc = ('69.2075 34.5553 100 km') #Kabul location` and then use it in `for tweet in tweepy.Cursor(api.search, geocode = geoLoc, q=searchQuery, tweet_mode='extended').items(maxTweets):` I get `0 tweets.` – Ambrish Dhaka Sep 29 '19 at 05:31

1 Answers1

0

Modify your the variable

searchQuery = "afghanistan OR elections bounding_box: [60.5284298033, 29.318572496, 75.1580277851, 38.4862816432]"