Instantly share code, notes, and snippets.
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save vim13/4748381 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
#!/usr/local/bin/python | |
#vim:fileencoding=utf-8 | |
import cgi | |
import sys | |
import os | |
import Cookie | |
sys.path.append('hoge') | |
import tweepy | |
consumer_key = 'hoge' | |
consumer_secret = 'hoge' | |
auth = tweepy.OAuthHandler(consumer_key,consumer_secret) | |
def oauth(): | |
try: | |
redirect_url = auth.get_authorization_url() | |
except tweepy.TweepError: | |
print 'Content-type: text/html; charset=UTF-8' | |
print '\r\n\r\n' | |
print 'Error: Failed to get request token.' | |
a_cookie = Cookie.SimpleCookie() | |
a_cookie['sszk_request_token_key'] = auth.request_token.key | |
a_cookie['sszk_request_token_secret'] = auth.request_token.secret | |
print 'Content-type: text/html; charset=UTF-8' | |
print a_cookie.output(), '\n\n' | |
print '<script type=\"text/javascript\">window.open(\"' + redirect_url + '\",\"_self\",\"\")</script>' | |
if __name__ == '__main__': | |
form = cgi.FieldStorage() | |
form_filled = 0 | |
if form.has_key("button1"): | |
form_filled = 1 | |
if form_filled == 0: | |
print 'Content-type: text/html; charset=UTF-8' | |
print '\r\n\r\n' | |
print 'Error: Failed to get request token.' | |
else: | |
oauth() |
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
#!/usr/local/bin/python | |
#vim:fileencoding=utf-8 | |
import re | |
import cgi | |
import os | |
import Cookie | |
import sys | |
import time | |
import datetime | |
from datetime import timedelta | |
sys.path.append('hoge') | |
import tweepy | |
consumer_key = 'hoge' | |
consumer_secret = 'hoge' | |
auth = tweepy.OAuthHandler(consumer_key,consumer_secret) | |
def get_tweets(api): | |
status = "" | |
for p in tweepy.Cursor(api.user_timeline).items(-1) : | |
if re.search(u'\u30fd.\u309c\u25bd\u3001\u309c.\u30ce', p.text): | |
status = p.created_at | |
break | |
return status | |
def tweet(api, status): | |
tweet = 'ヽ(゜▽、゜)ノ \n' | |
if status: | |
now = datetime.datetime.now() - timedelta( hours=9 ) | |
last = now - status | |
if last.days == 0: | |
tweet += str(last.seconds//3600) + '時間' + str( (last.seconds//60)%60 ) + '分ぶり' | |
else: | |
tweet += str(last.days) + '日と' + str(last.seconds//3600) + '時間ぶり' | |
api.update_status(tweet) | |
redirect_url = '/labs/success.html' | |
print 'Content-type: text/html; charset=UTF-8' | |
print '\r\n\r\n' | |
print '<script type=\"text/javascript\">window.open(\"' + redirect_url + '",\"_self\",\"\")</script>' | |
def request(verifier): | |
a_cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', '')) | |
request_token_key = a_cookie['sszk_request_token_key'].value | |
request_token_secret = a_cookie['sszk_request_token_secret'].value | |
auth.set_request_token(request_token_key, request_token_secret) | |
auth.get_access_token(verifier) | |
auth.set_access_token(auth.access_token.key, auth.access_token.secret) | |
if __name__ == '__main__': | |
form = cgi.FieldStorage | |
form_filled = 0 | |
if 'QUERY_STRING' in os.environ: | |
query = cgi.parse_qs(os.environ['QUERY_STRING']) | |
else: | |
query = {} | |
if query: | |
verifier = query['oauth_verifier'][0] | |
form_filled = 1 | |
if form_filled == 0: | |
print 'Content-type: text/html; charset=UTF-8' | |
print '\r\n\r\n' | |
print 'Error' | |
else: | |
request(verifier) | |
api = tweepy.API(auth) | |
status = get_tweets(api) | |
tweet(api,status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment