Last active
March 23, 2018 10:29
-
-
Save s4kr4/23968d25bc0eeb4db65670b5ccc831b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from requests_oauthlib import OAuth1Session | |
import json | |
import settings | |
import os | |
import sys | |
if len(sys.argv) != 2: | |
print("Please input search keyword!") | |
sys.exit() | |
twitter = OAuth1Session( | |
settings.CONSUMER_KEY, | |
settings.CONSUMER_SECRET, | |
settings.ACCESS_TOKEN, | |
settings.ACCESS_TOKEN_SECRET | |
) | |
uri = "https://api.twitter.com/1.1/search/tweets.json" | |
output_dir = sys.argv[1] + "_results" | |
os.makedirs(output_dir, exist_ok=True) | |
params = { | |
"q": sys.argv[1], | |
"lang": "ja", | |
"count": "100" | |
} | |
for i in range(5): | |
req = twitter.get(uri, params = params) | |
results = json.loads(req.text) | |
params["max_id"] = results["search_metadata"]["max_id_str"] | |
for result in results["statuses"]: | |
id = result["id"] | |
print(id) | |
file = open(f"{output_dir}/{id}.json", "w") | |
json.dump(result, file, indent=2, ensure_ascii=False) | |
file.close() | |
print('complete!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxx" | |
CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
ACCESS_TOKEN = "xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment