Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
the robot v0.1 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
xuechong87 committed Aug 18, 2013
1 parent 4864517 commit c58e913
Show file tree
Hide file tree
Showing 69 changed files with 8,380 additions and 1,249 deletions.
26 changes: 24 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@


#############
## Python
#############
.settings
*.py[co]

# Packages
*.egg
*.egg-info
dist/
build/
.classpath
.settings/
eggs/
parts/
var/
sdist/
develop-eggs/
.installed.cfg

# Installer logs
pip-log.txt


#Mr Developer
.mr.developer.cfg
18 changes: 3 additions & 15 deletions .project
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>message-robot</name>
<name>MoegirlWeixinRobot</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
50 changes: 50 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>

<pydev_project>
<pydev_variables_property name="org.python.pydev.PROJECT_VARIABLE_SUBSTITUTION">
<key>GOOGLE_APP_ENGINE</key>
<value>C:\Program Files (x86)\Google\google_appengine</value>
</pydev_variables_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/gaepy</path>
</pydev_pathproperty>
<pydev_pathproperty name="org.python.pydev.PROJECT_EXTERNAL_SOURCE_PATH">
<path>${GOOGLE_APP_ENGINE}</path>
<path>${GOOGLE_APP_ENGINE}/lib/PyAMF-0.6.1</path>
<path>${GOOGLE_APP_ENGINE}/lib/antlr3</path>
<path>${GOOGLE_APP_ENGINE}/lib/cherrypy</path>
<path>${GOOGLE_APP_ENGINE}/lib/concurrent</path>
<path>${GOOGLE_APP_ENGINE}/lib/django-0.96</path>
<path>${GOOGLE_APP_ENGINE}/lib/django-1.2</path>
<path>${GOOGLE_APP_ENGINE}/lib/django-1.3</path>
<path>${GOOGLE_APP_ENGINE}/lib/django-1.4</path>
<path>${GOOGLE_APP_ENGINE}/lib/django-1.5</path>
<path>${GOOGLE_APP_ENGINE}/lib/enum</path>
<path>${GOOGLE_APP_ENGINE}/lib/fancy_urllib</path>
<path>${GOOGLE_APP_ENGINE}/lib/google-api-python-client</path>
<path>${GOOGLE_APP_ENGINE}/lib/graphy</path>
<path>${GOOGLE_APP_ENGINE}/lib/grizzled</path>
<path>${GOOGLE_APP_ENGINE}/lib/httplib2</path>
<path>${GOOGLE_APP_ENGINE}/lib/ipaddr</path>
<path>${GOOGLE_APP_ENGINE}/lib/jinja2-2.6</path>
<path>${GOOGLE_APP_ENGINE}/lib/markupsafe-0.15</path>
<path>${GOOGLE_APP_ENGINE}/lib/oauth2</path>
<path>${GOOGLE_APP_ENGINE}/lib/prettytable</path>
<path>${GOOGLE_APP_ENGINE}/lib/protorpc</path>
<path>${GOOGLE_APP_ENGINE}/lib/python-gflags/tests</path>
<path>${GOOGLE_APP_ENGINE}/lib/setuptools-0.6c11</path>
<path>${GOOGLE_APP_ENGINE}/lib/simplejson</path>
<path>${GOOGLE_APP_ENGINE}/lib/sqlcmd</path>
<path>${GOOGLE_APP_ENGINE}/lib/webapp2-2.3</path>
<path>${GOOGLE_APP_ENGINE}/lib/webapp2-2.5.1</path>
<path>${GOOGLE_APP_ENGINE}/lib/webapp2-2.5.2</path>
<path>${GOOGLE_APP_ENGINE}/lib/webob-1.1.1</path>
<path>${GOOGLE_APP_ENGINE}/lib/webob-1.2.3</path>
<path>${GOOGLE_APP_ENGINE}/lib/webob_0_9</path>
<path>${GOOGLE_APP_ENGINE}/lib/yaml-3.10</path>
<path>${GOOGLE_APP_ENGINE}/lib/yaml/lib</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">py27</pydev_property>
</pydev_project>
84 changes: 84 additions & 0 deletions HandlerChain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# coding: utf-8
'''
Created on 2013-7-3
handler chain
@author: xuechong
'''
from moehandlers.FlowerHandler import FlowerHandler
from moehandlers.SellMoeHandler import SellMoeHandler
from moehandlers.HelpHandler import HelpHandler
from moehandlers.AnimeListHandler import AnimeListHandler
from Weixin import textReply
import logging
from moehandlers.SearchHandler import SearchHandler

__default_chain__ = (SellMoeHandler,SearchHandler,FlowerHandler,HelpHandler,AnimeListHandler)
__text_chain__=(SellMoeHandler,SearchHandler,FlowerHandler,HelpHandler,AnimeListHandler)

def textHandlerChain(userMsg):
"""
return a new instance of a default text msg handler chain
"""
return HandlerChain(userMsg,list(__text_chain__))

class HandlerChain(object):
"""
the handler chain
call doChain to get a reply xml str
"""
handlers = list()
userMsg = None

def __init__(self,userMsg,handlerList=None):
self.handlers = handlerList
if self.handlers==None:
self.handlers=list(__default_chain__)
logging.debug("new handlerChain" + str(self.handlers))
self.userMsg = userMsg

def doChain(self):
"""
invoke handlers and return reply xmlstr
"""
try :
result = self.invokeNext()
return result==None and textReply(self.userMsg) or result
except Exception as e:
logging.exception(str(e))
return textReply(self.userMsg,"555更新姬被玩坏了啦><")

def invokeNext(self):
"""
invoke next handler until there is no handler left or have result
returns None if no handler can hand the income msg
"""
result = None
if len(self.handlers)>0:
handler = self.handlers.pop()()
result = handler.handle(self)
if result==None and len(self.handlers)>0:
result = self.invokeNext()
return result

def getMsgType(self):
"""
get the msgtype of the income msg
"""
return self.userMsg.get("MsgType")

def getMsgContent(self):
"""
get the content of the income msg
"""
return self.userMsg.get("Content").encode("utf-8").strip()
def forceStop(self):
"""
stop the handler chain
after call this ,if you can return a None to reply user a default msg
and the rest of the handlers will not be invoked
*return a None*
"""
self.handlers = list()
return None


58 changes: 58 additions & 0 deletions MoeGirlWiki.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# coding: utf-8
'''
Created on 2013-8-7
http://zh.moegirl.org/api.php?format=json&action=query&list=search&srwhat=title&srsearch=%E5%A4%8F%E5%A8%9C&srlimit=10
http://zh.moegirl.org/api.php?format=json&action=query&list=search&srwhat=text&srsearch=%E5%A8%9C
@author: xuechong
'''
from utils.Commons import fetchContentFromUrl
import json
import logging
import urllib

_wikiurl = "http://zh.moegirl.org/api.php?format=json&action=query&list=search&srwhat=title&srsearch=$title&srlimit=$limit"

cleancontent = lambda x:x.replace("<span class='searchmatch'>", "").replace("</span>", "")


class WikiContent(object):

title=""
snippet=""
size=""
wordcount=""
timestamp=""

def searchTitle(title,size=10):
result = list()
url = _wikiurl.replace("$title", urllib.quote(title)).replace("$limit", "10")
logging.info(url)
remoteResult = fetchContentFromUrl(url)

allSubjects=json.loads(remoteResult)["query"]["search"]
for subject in allSubjects:
content = WikiContent()
content.title = cleancontent(subject["title"])
content.snippet = cleancontent(subject["snippet"])
content.size = subject["size"]
content.wordcount = subject["wordcount"]
content.timestamp = subject["timestamp"]
result.append(content)
return result

#if __name__ == '__main__':
# remoteResult = fetchContentFromUrl(_wikiurl.replace("$title", "夏娜").replace("$limit", "10"))
# print remoteResult + "\n"
# result = json.loads(remoteResult)
# allSubjects=result["query"]["search"]
# for subject in allSubjects:
# print "title=" + cleancontent(subject["title"]) + "\n"
# print "snippet=" + cleancontent(subject["snippet"]) + "\n"
# print "timestamp=" + str(subject["timestamp"]) + "\n"
#
#
#
#
#
# pass

23 changes: 8 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
Moegirlweixin
=============

微信更新姬自动条目推送功能
暂定:
通过以下地址获取最新更新

http://zh.moegirl.org/index.php?title=Special:%E6%9C%80%E6%96%B0%E9%A1%B5%E9%9D%A2&feed=atom&namespace=0

每2小时向微信推送一条更新消息

实际推送的内容应该经过关键词过滤.重复检查,时间检查

请用maven构建

萌娘百科的微信更新姬
=====
现有的功能:
*卖萌
*新番列表
*花语查询

如果有好的建议请联系我哦
51 changes: 51 additions & 0 deletions Weixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# coding: utf-8
'''
Created on 2013-7-3
constants & basic function for weixin api
@author: xuechong
'''

import hashlib
import logging
from xml.etree import ElementTree

__token = "token"
__text_answer = "<xml><ToUserName><![CDATA[${toUser}]]></ToUserName><FromUserName><![CDATA[${fromUser}]]></FromUserName><CreateTime>${createTime}</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[${Content}]]></Content><MsgId>${MsgId}</MsgId></xml>"

def validate(param):
"""
validate if the request is posted by weixin
"""
list_ = sorted([__token, param("timestamp"), param("nonce")])
sha1 = hashlib.sha1()
sha1.update("".join(list_))
return str(sha1.hexdigest()) == param("signature")

def textReply(originMsg,replyStr="阿嘞?人家不懂你在说什么啦,输入'帮助'可以查看帮助哦!"):
"""
return a text type reply xml
"""
result = __text_answer.replace("${toUser}", originMsg.get("FromUserName")).replace("${fromUser}",originMsg.get("ToUserName"))\
.replace("${createTime}",originMsg.get("CreateTime")).replace("${MsgId}",originMsg.get("MsgId"))\
.replace("${Content}",replyStr)
logging.debug(result)
return result

class MsgContent:
"""
the content of the user post msg
"""
content = {}

def __init__(self,xmlContent):
'''
xmlContent the str content of the xml
'''
root = ElementTree.fromstring(xmlContent)
_nodes = root.getiterator("xml")
for node in _nodes.pop().getchildren():
self.content[node.tag]=node.text

def get(self,key):
return self.content.get(key)

13 changes: 13 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
application: moegirlweixin
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /weixin
script: mainProcessor.app
- url: /test
script: tester.app
- url: /init
script: inits.app
Loading

0 comments on commit c58e913

Please sign in to comment.