Un script qui utilise OAuth1Session pour tweeter et rechercher à partir du terminal.
words = raw_input(u"input-tweet: ") search_words = raw_input(u"Keyword?: ")
Quittez la boucle si les deux éléments sont vides.
Tweeter depuis le terminal peut être étonnamment pratique.
twform.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
while True:
words = raw_input(u"input-tweet: ")
search_words = raw_input(u"Keyword?: ")
C_KEY = "************************************"
C_SECRET = "************************************"
A_KEY = "************************************"
A_SECRET = "************************************"
def Post_msg():
url = "https://api.twitter.com/1.1/statuses/update.json"
params = {"status": words,"lang": "ja"}
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
req = tw.post(url, params = params)
if req.status_code == 200:
print "Success! Your Tweet: ", unicode(words, "utf-8")
else:
print req.status_code
return Post_msg
def Limit_Status():
url = "https://api.twitter.com/1.1/application/rate_limit_status.json"
params = {}
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
req = tw.get(url, params = params)
if req.status_code == 200:
limit = req.headers["x-rate-limit-remaining"]
print ("API remain: " + limit)
return Limit_Status
def Search_words():
url = "https://api.twitter.com/1.1/search/tweets.json?"
params = {
"q": unicode(search_words, "utf-8"),
"lang": "ja",
"result_type": "recent",
"count": "20"
}
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
req = tw.get(url, params = params)
tweets = json.loads(req.text)
for tweet in tweets["statuses"]:
print (tweet["created_at"])
print (tweet["user"]["screen_name"]), (tweet["user"]["name"])
print (tweet["text"])
print "=" * 60
return Search_words
if words:
Post_msg()
Limit_Status()
elif search_words:
Search_words()
Limit_Status()
else:
break
Recommended Posts