Skip to content

Instantly share code, notes, and snippets.

@laiso
Created September 13, 2011 11:29
Show Gist options
  • Save laiso/1213632 to your computer and use it in GitHub Desktop.
Save laiso/1213632 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path[:0] = ['.', ]
# secret.py
from secret import (
ID, PASSWORD,
)
TEMPLATE = u'''
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>ザ・インタビューズにログインせずにインタビューを投稿するやつ</title>
</head>
<body>
<ul>
<li>ログインせずにインタビューを投稿できる</li>
<li>自分自身にインタビューを投稿できる</li>
</ul>
<form method="POST">
<ul>
<li>user:<input name="user" /></li>
<li>text:<textarea name="title"></textarea></li>
<li><input type="submit" /></li>
</ul>
</form>
<p><author><a href="http://twitter.com/laiso">@laiso</a></author></p>
<body>
</html>
'''
import os
import logging
import re
import Cookie
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
"""
http://stackoverflow.com/questions/5633702/mechanize-not-working-for-automating-gmail-login-in-google-appengine/5651624#5651624
"""
import gaeopener
class MainHandler(webapp.RequestHandler):
def __init__(self):
self.opener = gaeopener.GAEOpener()
def get(self):
self.response.out.write(TEMPLATE)
def post(self):
user = str(self.request.get('user'))
assert isinstance(user, basestring)
title = self.request.get('title')
assert title
resp = self.opener.open('http://theinterviews.jp/login',
"account=%s&password=%s&mode=submit" % (ID, PASSWORD))
url = 'http://theinterviews.jp/%s/interview' % user
resp = self.opener.open(url)
interview_html = resp.content
assert interview_html
re_account_id = re.compile(r'name="target_user_id" value="(\d+)"')
logging.warn(interview_html)
if not match:
return self.response.out.write(u"失敗")
account_id = match.pop()
assert account_id
re_authenticity_token = re.compile(r'name="authenticity_token" value="(.+)"')
match = re_authenticity_token.findall(interview_html)
if not match:
return self.response.out.write(u"失敗")
authenticity_token = match.pop()
assert authenticity_token
try:
resp = self.opener.open('http://theinterviews.jp/lib_interview.php',
'target_user_id=%s&title=%s&mode=submit&authenticity_token=%s' %\
(account_id, title.encode('utf_8'), authenticity_token))
except urlfetch.DownloadError,e:
pass
self.response.out.write(u"<p>完了</p>")
self.response.out.write(u'<p><a href="/anonterviews/">もどる</a></p>')
def main():
debug = False
if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
debug = True
application = webapp.WSGIApplication([('/anonterviews/', MainHandler), ],
debug=debug)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment