Created
April 11, 2015 13:16
-
-
Save KenjiOhtsuka/9f1bc72b6471c10e7a82 to your computer and use it in GitHub Desktop.
Use like "python TweetServer.py" with python 2. If you don't have requests_oauthlib, exec "pip install requests requests_oauthlib", at first.
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
import cgi | |
from requests_oauthlib import OAuth1Session | |
import SimpleHTTPServer | |
import SocketServer | |
class ApplicationConfig(): | |
Port = 80 | |
class Twitter(): | |
ConsumerKey = 'ojy8ZSbdv7fyt6QcmBr8coN8W' | |
ConsumerSecret = 'jN68LCW72XMCOGuFXXObRCagKblZNDHv55iFxdPZwvTOhu6Tlj' | |
AccessToken = '177199334-7bPzPJ0qhTvU6YbgInm7OJMZJxmZdvWK72T54YgX' | |
AccessSecret = '4jHXSqmSWTYO00X0PqnLLb1P1nH8nzKTR98MmzXvSREm5' | |
PostUrl = 'https://api.twitter.com/1.1/statuses/update.json' | |
@staticmethod | |
def post(message): | |
twitter = OAuth1Session( | |
Twitter.ConsumerKey, | |
Twitter.ConsumerSecret, | |
Twitter.AccessToken, | |
Twitter.AccessSecret) | |
request = twitter.post(Twitter.PostUrl, {'status': message}) | |
if request.status_code == 200: | |
return True | |
else: | |
return False | |
PostStatus = { | |
'NotYet': 0, | |
'Success': 1, | |
'Failed': 2 | |
} | |
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
self.returnHtml(PostStatus['NotYet']) | |
def do_POST(self): | |
form = cgi.FieldStorage( | |
fp=self.rfile, | |
headers=self.headers, | |
environ={ | |
'REQUEST_METHOD':'POST', | |
'CONTENT_TYPE':self.headers['Content-Type'] | |
}) | |
message = form.getvalue('message') | |
if len(message) > 0: | |
if Twitter.post(message): | |
self.returnHtml(PostStatus['Success']) | |
return | |
self.returnHtml(PostStatus['Failed']) | |
def returnHtml(self, postStatus): | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
self.wfile.write(self.html(postStatus)) | |
def html(self, postStatus): | |
if postStatus == PostStatus['Success']: | |
notice = "Posted Successfully." | |
elif postStatus == PostStatus['Failed']: | |
notice = "Post Failed." | |
else: | |
notice = "" | |
return self.htmlFrame( | |
self.htmlHead(), | |
self.htmlNotice(notice) + self.htmlForm()) | |
def htmlForm(self): | |
return """ | |
<form action="#" method="post"> | |
<textarea name="message" style="width:100%;" rows="10" cols="50" required="required"></textarea> | |
<button>POST</button> | |
</form> | |
""" | |
def htmlNotice(self, notice): | |
if len(notice) > 0: | |
return "<p>" + notice + "</p>" | |
return "" | |
def htmlHead(self): | |
return """ | |
<meta charset="utf-8" /> | |
<title>Post Twitter</title> | |
""" | |
def htmlFrame(self, head, body): | |
return """<!DOCTYPE html> | |
<html> | |
<head> | |
""" + head + """ | |
</head> | |
<body> | |
""" + body + """ | |
</body> | |
</body> | |
</html>""" | |
if __name__ == "__main__": | |
Handler = ServerHandler | |
httpd = SocketServer.TCPServer(("", ApplicationConfig.Port), Handler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment