#!/usr/bin/env python
#coding:utf-8
import wsgiref.handlers
import urllib
import base64
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
class MainPage(webapp.RequestHandler):
def get(self):
#self.response.headers['Content-Type'] = 'text/plain'
self.response.headers.add_header("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")
header(self)
self.response.out.write('''<div id="letter">
<form action="/sign" method="post">
<div>Twitter username:<input type="text" name="tusername" id="username" value="" />
password:<input type="text" name="tpassword" id="password" value="" /></div>
<div>Fanfou username:<input type="text" name="fu" id="username2" value="" />
password:<input type="text" name="fp" id="password2" value="" /></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post"></div>
</form></div>
''')
footer(self)
class PostPage(webapp.RequestHandler):
def post(self):
header(self)
msg = self.request.get('content')
page_msg=msg
msg = msg.encode('utf8')
tu = self.request.get('tusername')
tp = self.request.get('tpassword')
fu = self.request.get('fu')
fp = self.request.get('fp')
#update(tu,tp,'twitter',msg)
#update(fu,fp,'fanfou',msg)
self.response.out.write('''<div id="letter">''')
if update(tu,tp,'twitter',msg)==True:
self.response.out.write('''
<p>Twitter Post Susscessful</p>
''')
else:
self.response.out.write('''
<p>Twitter Post Faild</p>
''')
if update(fu,fp,'fanfou',msg)==True:
self.response.out.write('''
<p>Fanfou Post Susscessful</p>
''')
else:
self.response.out.write('''
<p>Fanfou Post Faild</p>
''')
self.response.out.write('''
<p><a href="/" >Back</></p></div>
''')
footer(self)
#self.redirect('/')
def header(self):
self.response.out.write('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<link type="text/css" rel="stylesheet" href="./styles/main.css" media="screen" />
<title>Hello,World</title>
</head>
<body>''')
def footer(self):
self.response.out.write('''
<div id="footer"><p><img src="http://code.google.com/appengine/images/appengine-silver-120x30.gif"
alt="Powered by Google App Engine" /></p><p>Powered by <a href="http://ioio.name/">IoIo.NaMe</a></p></div>
</body>
</html>''')
def update(username,password,twitter,msg):
authStr = username + ":" + password
#self.response.out.write(authStr.encode("base64")[0:-1])
if twitter=='twitter' :
url='http://twitter.com/statuses/update.json'
else:
url='http://api.fanfou.com/statuses/update.json'
#msg="Update by GAE.由GAE更新."
form_fields = {"status":msg}
form_data = urllib.urlencode(form_fields)
#self.response.out.write(form_data)
result = urlfetch.fetch(url=url,
payload=form_data,
method=urlfetch.POST,
headers={"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/xml",
"Authorization": "Basic "+authStr.encode("base64")[0:-1]})
if result.status_code == 200:
return True
else:
return False
def main():
application = webapp.WSGIApplication([('/', MainPage),
('/sign', PostPage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()