Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created October 30, 2016 00:47
Show Gist options
  • Save tomoima525/79dc7f6f1d2de098454c3f3a139c35c1 to your computer and use it in GitHub Desktop.
Save tomoima525/79dc7f6f1d2de098454c3f3a139c35c1 to your computer and use it in GitHub Desktop.
from requests_oauthlib import OAuth1Session
import configparser
import json
import csv
config = configparser.ConfigParser()
config.read('config.txt')
url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=tomoaki_imai&count=200&exclude_replies=true'
params = {}
options = config["keys"]
twitter = OAuth1Session(options["ConsumerKey"], options["ConsumerSecret"], options["AccessToken"], options["AccessSecret"])
req = twitter.get(url, params = params)
if req.status_code == 200:
f = open('sample.csv', 'w')
writer = csv.writer(f, lineterminator='\n')
timeline = json.loads(req.text)
for twt in timeline:
str = '{},{},{},{}'.format(twt["retweet_count"], twt["id_str"],twt["text"].replace('\n',''),twt["created_at"])
writer.writerow([str])
else:
print ("Error: %d" % req.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment