/ Published in: Python
アルゴリズムコンテストã®å‹‰å¼·ä¼šã§ç´¹ä»‹ã—ãŸGoogle App Engineã§å‹•ãpythonコードã§ã™
Djangoフレームワークをå‰æã«ã—ã¦ã„ã¾ã™ã€‚
ã‚ã¾ã‚Šã‚³ãƒ¼ãƒ‰ã¨ã—ã¦ã¯å‚考ã«ãªã‚‰ãªã„ã‹ã‚‚知れã¾ã›ã‚“ãŒã€ã“ã‚Œã ã‘ã§ã‚‚ç°¡å˜ãªäººå·¥ç„¡è„³ãŒã§ãã‚‹ã‚“ã§ã™ã
Djangoフレームワークをå‰æã«ã—ã¦ã„ã¾ã™ã€‚
ã‚ã¾ã‚Šã‚³ãƒ¼ãƒ‰ã¨ã—ã¦ã¯å‚考ã«ãªã‚‰ãªã„ã‹ã‚‚知れã¾ã›ã‚“ãŒã€ã“ã‚Œã ã‘ã§ã‚‚ç°¡å˜ãªäººå·¥ç„¡è„³ãŒã§ãã‚‹ã‚“ã§ã™ã
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!-*- coding:utf-8 -*- import sys import os import string import urllib import urllib2 import math import htmlentitydefs import re from django.shortcuts import render_to_response from google.appengine.api import users from django.http import HttpResponseRedirect from BeautifulSoup import BeautifulSoup from google.appengine.api import urlfetch from base64 import b64encode #åˆæœŸãƒšãƒ¼ã‚¸ def Mainpage(request): return render_to_response('input.html',) #質å•ã‚’もらã£ã¦å›žç”を出ã™ã¾ã§ã®ãƒ¡ã‚½ãƒƒãƒ‰ def getAnswer(request): search_text = '' from_user = 'username' try: search_text=request.POST['query'] search_text=search_text.encode('utf-8') except:pass if search_text == '': search_text,from_user = GetresponseTwitter() #print >>sys.stderr,'search_text=%s'%search_text search_text = search_text.encode('utf-8') to_ = '' from_ = '' #質å•æ–‡ã‚’ãã®ã¾ã¾æ¤œç´¢ã«ã‹ã‘ã‚‹ if search_text!='' : url = 'http://pcod.no-ip.org/yats/search?query=%s&rss' % (search_text) xml_search2=urlfetch.fetch(url).content entry = BeautifulSoup(xml_search2)('entry') words=[] titledict=dict() answer = '' for en in entry: #print >>sys.stderr,en('summary')[0] words=en('summary')[0](text = True)[0].split(' ') update_before = en('updated')[0](text = True)[0] #print >>sys.stderr,status_id try: regexp = re.compile('username') if regexp.search(en('summary')[0](text = True)[0])== None:#tam_botã®ç™ºè¨€ã¯ç„¡è¦– from_=words[0].split('@')[1] to_=words[2].split('@')[1] url='http://pcod.no-ip.org/yats/search?query=user:'+to_+'%20%40'+from_+'%20&lang=ja&rss' #print >>sys.stderr,url xml_search=urlfetch.fetch(url).content title1to2 = BeautifulSoup(xml_search)('entry') for x in title1to2: #titledict[x('updated')[0](text = True)[0]]=x('summary')[0].contents[0] update_after=x('updated')[0](text = True)[0] if update_after > update_before: #print >>sys.stderr,'update_before=%s update_after=%s'%(update_before,update_after) tmp_answer=x('summary')[0](text = True)[0].split(' ')[3] regexp1 = re.compile('@') if regexp1.search(tmp_answer)==None and tmp_answer!='': print >>sys.stderr,'answer=%s'%tmp_answer answer = tmp_answer break#一個ã§ã‚‚ã¿ã¤ã‹ã‚Œã°break if tmp_answer != '': break except:pass #print >>sys.stderr,'answer=%s'%answer if answer != '':#検索çµæžœãŒã‚ã£ãŸå ´åˆ post = '@%s %s'%(from_user,answer) else:#検索çµæžœãŒãªã‹ã£ãŸå ´åˆ #############æ–‡ç« ã ã‘ã§å›žç”ãŒå‡ºã›ãªã„å ´åˆ############# question_word=GetKeitaiso_mecapi(search_text).encode('utf-8') q = {'query':question_word,'lang' : 'ja'} url = 'http://pcod.no-ip.org/yats/search?rss&' + urllib.urlencode(q) xml=urlfetch.fetch(url).content #print >>sys.stderr,xml entry = BeautifulSoup(xml)('entry') for en in entry: tmp_answer=en('summary')[0](text = True)[0].split(' : ')[1].rstrip() regexp = re.compile('tam_bot') if regexp.search(en('summary')[0](text = True)[0])== None:#tam_botã®ç™ºè¨€ã¯ç„¡è¦– regexp1 = re.compile('@') if regexp1.search(tmp_answer)==None and tmp_answer!='' and search_text.rstrip()!=tmp_answer.rstrip().encode('utf-8'): answer = tmp_answer #print >>sys.stderr,'tmp_answer=%s'%tmp_answer break post = '@%s %s'%(from_user,answer) #########最後ã¾ã§ã‚ã‹ã‚‰ãªã„å ´åˆ############## if answer == '': post = u'@%s %s'%(from_user,unicode(search_text,'utf-8')) #print >>sys.stderr,'post=%s'%post Twitter(post) return render_to_response('outanswer.html',{'search_text':search_text,'post':post}) #質å•æ–‡ç« ã‚’å½¢æ…‹ç´ è§£æžã‹ã‘ã¦å˜èªžã‚’å–り出㙠def GetKeitaiso_mecapi(question): question_word='' word_rank=dict() i=0 q={'format':'xml','sentence':question,'response':'','filter':'uniq'} url='http://mimitako.net/api/mecapi.cgi?'+ urllib.urlencode(q) xml_question=urlfetch.fetch(url).content #print >>sys.stderr,BeautifulSoup(xml_question)('mecabresult') ngword=[u'何',u'誰'] for mlist in BeautifulSoup(xml_question)('mecabresult'): for m in mlist('word'): term=m('feature')[0].contents[0].split(',') word=m('surface')[0] word=tr(u'!"#$%&()=-^~\|[]{}@:`*;+,.<>/\?_。ã€ãƒ»', u' ',word) word=string.replace(word, ' ', '') #print >>sys.stderr,'%s %s %s %s %s %s %s'%(term[0],term[1],term[2],term[3],term[4],term[5],word) if (term[0]==u'åè©ž' or term[0]==u'å‹•è©ž' or term[0]==u'形容詞') and word != '' and word not in ngword: cntwd = int(countWord(word)) #print >>sys.stderr,'%s %s %s'%(term[0],word,cntwd) if i<3 and cntwd<100000000: question_word = u'%s %s'%(question_word,word) print >>sys.stderr,'%s %s %s'%(term[0],word,cntwd) i=i+1 return question_word #Twitterã¸ã®postメソッド def Twitter(post): #basic auth username='username' password='password' url = 'http://twitter.com/statuses/update.json' payload = urllib.urlencode({"status": post.encode("utf-8")}) base64string =b64encode("%s:%s" % (username, password)) headers = {"Authorization": "Basic %s" % base64string} json_result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers).content return 1 #Twitterã‹ã‚‰ã‚るユーザã®ç›´è¿‘ã®è³ªå•ã‚’ã¨ã£ã¦ãã‚‹ def GetresponseTwitter(): #basic auth username='username' password='password' url = 'http://twitter.com/statuses/replies.xml' base64string =b64encode("%s:%s" % (username, password)) headers = {"Authorization": "Basic %s" % base64string} xml_result=urlfetch.fetch(url, payload='', method=urlfetch.POST, headers=headers).content result_comment=htmlentity2unicode(BeautifulSoup(xml_result)('text')[0].contents[0].split(' ')[1]) from_user=BeautifulSoup(xml_result)('screen_name')[0].contents[0] return result_comment,from_user #googleã®æ¤œç´¢çµæžœæ•°ã‚’見る def countWord(word): q = {#'q':'\"'+question+'\"', 'q':word.encode('utf-8'), 'v' : 1.0, 'hl' : 'ja', 'rsz': 'small' } url=u'http://ajax.googleapis.com/ajax/services/search/web?'+ urllib.urlencode(q) json_result=urlfetch.fetch(url).content json_result = string.replace(json_result, 'null', '"null"') try: dict_result=eval('dict('+json_result+')') return dict_result['responseData']['cursor']['estimatedResultCount'] except: return 0 def tr(pattern, repl, string): m = dict(zip(pattern, repl)) return ''.join((m.get(c,c) for c in string)) #数値文å—å‚照→文å—㸠def htmlentity2unicode(text): # æ£è¦è¡¨ç¾ã®ã‚³ãƒ³ãƒ‘イル reference_regex = re.compile(u'&(#x?[0-9a-f]+|[a-z]+);', re.IGNORECASE) num16_regex = re.compile(u'#x\d+', re.IGNORECASE) num10_regex = re.compile(u'#\d+', re.IGNORECASE) result = u'' i = 0 while True: # 実体å‚ç…§ or æ–‡å—å‚照を見ã¤ã‘ã‚‹ match = reference_regex.search(text, i) if match is None: result += text[i:] break result += text[i:match.start()] i = match.end() name = match.group(1) # 実体å‚ç…§ if name in htmlentitydefs.name2codepoint.keys(): result += unichr(htmlentitydefs.name2codepoint[name]) # æ–‡å—å‚ç…§ elif num16_regex.match(name): # 16進数 result += unichr(int(u'0'+name[1:], 16)) elif num10_regex.match(name): # 10進数 result += unichr(int(name[1:])) return result
URL: http://www.team-lab.com/news/index.php?itemid=469