Skip to content

Instantly share code, notes, and snippets.

@Hironsan
Created May 23, 2018 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hironsan/11d6eb0c714869bfb5d2447128a33eef to your computer and use it in GitHub Desktop.
Save Hironsan/11d6eb0c714869bfb5d2447128a33eef to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import os
# Load api keys.
consumer_key = os.environ.get('CONSUMER_KEY')
consumer_secret = os.environ.get('CONSUMER_SECRET')
access_token = os.environ.get('ACCESS_TOKEN')
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')
import tweepy
# Search tweets.
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = tweepy.Cursor(api.search, q='地震', lang='ja').items(100)
# Write tweets to json file.
import json
from datetime import datetime
filename = 'tweets_{}.jsonl'.format(datetime.now().strftime('%Y%m%d%H%M%S'))
with open(filename, 'w') as f:
for tweet in tweets:
dic = {'text': tweet.text,
'date': str(tweet.created_at),
'id': tweet.id,
# 'geo': tweet.geo,
'user_id':tweet.user.id}
json.dump(dic, f)
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment