def start(): today = str(datetime.date.today()) request_num = 0 jata = {"datetime": today} for query in all_politicians: request_num += 1 if request_num % 100 == 0: print("waiting for 15 minutes...") # sleep 15 minutes after 180 requests time.sleep(905) print("finished waiting") r = api.request("users/show", {"screen_name": query}) data = json.loads(r.text) # print(data) sample = { "name": data["name"], "followers_count": data["followers_count"], "friends_count": data["friends_count"], "listed_count": data["listed_count"], "statuses_count": data["statuses_count"], "tweets": [], } print("connecting to " + str(data["name"].encode("ascii", "ignore")) + " || request # " + str(request_num)) tweet_list = get_tweets(query, today) i = 0 for tweet_data in tweet_list: sample["tweets"].append( { "tweetID": tweet_data["tweetID"], "created_at": tweet_data["created_at"], "text": tweet_data["text"], "textblob": tweet_data["textblob"], "NLTK": tweet_data["NLTK"], "RT": tweet_data["RT"], "others": tweet_data["others"], } ) jata[query] = sample i += 1 f_today = datetime.datetime.today() file_date = str(f_today.year) + "_" + str(f_today.month) + "_" + str(f_today.day) print(file_date) with open("data/" + file_date + ".json", "w") as f: f.write(json.dumps(jata)) tweet_mail.tweet_mail()
def start(): today = str(datetime.date.today()) request_num = 0 jata = {"datetime": today} for query in all_politicians: request_num += 1 if request_num % 100 == 0: print('waiting for 15 minutes...') #sleep 15 minutes after 180 requests time.sleep(905) print('finished waiting') r = api.request('users/show', {'screen_name': query}) data = (json.loads(r.text)) # print(data) sample = { 'name': data['name'], 'followers_count': data['followers_count'], 'friends_count': data['friends_count'], 'listed_count': data['listed_count'], 'statuses_count': data['statuses_count'], 'tweets': [] } print('connecting to ' + str(data['name'].encode("ascii", "ignore")) + ' || request # ' + str(request_num)) tweet_list = get_tweets(query, today) i = 0 for tweet_data in tweet_list: sample['tweets'].append({ 'tweetID': tweet_data['tweetID'], 'created_at': tweet_data['created_at'], 'text': tweet_data['text'], 'textblob': tweet_data['textblob'], 'NLTK': tweet_data['NLTK'], 'RT': tweet_data['RT'], 'others': tweet_data['others'] }) jata[query] = sample i += 1 f_today = datetime.datetime.today() file_date = str(f_today.year) + '_' + str(f_today.month) + '_' + str( f_today.day) print(file_date) with open('data/' + file_date + '.json', 'w') as f: f.write(json.dumps(jata)) tweet_mail.tweet_mail()
def start(): request_num = 0 with open('data.json', 'r') as f: j_data = json.load(f) print('json data loaded') key = (len(j_data['sample'])) today = str(datetime.date.today()) j_data['sample'].append({"date":today}) for query in test: request_num += 1 if request_num == 180: print('waiting for 15 minutes...') #sleep 15 minutes after 180 requests time.sleep(905) print('finished waiting') r = api.request('users/show', {'screen_name': query}) data = (json.loads(r.text)) j_data['sample'][key][query] = { 'name': data['name'], 'followers_count': data['followers_count'], 'friends_count':data['friends_count'], 'listed_count':data['listed_count'], 'statuses_count':data['statuses_count'], 'tweets': {}} print('connecting to twitter...',request_num) tweet_list = get_tweets(query,today) i = 0 for tweet_data in tweet_list: j_data['sample'][key][query]['tweets'][i] = { 'tweetID': tweet_data['tweetID'], 'created_at': tweet_data['created_at'], 'text': tweet_data['text'], 'textblob': tweet_data['textblob'], 'NLTK': tweet_data['NLTK'], 'RT': tweet_data['RT'], 'others': tweet_data['others'] } i += 1 with open('data.json', 'w') as f: f.write(json.dumps(j_data)) tweet_mail.tweet_mail()