<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>幻想曲.Net</title>
	<atom:link href="http://yibin.us/feed" rel="self" type="application/rss+xml" />
	<link>http://yibin.us</link>
	<description>面朝大海，春暖花开</description>
	<lastBuildDate>Fri, 17 Jun 2011 01:57:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MongoDB中自增ID的实现</title>
		<link>http://yibin.us/archives/6841</link>
		<comments>http://yibin.us/archives/6841#comments</comments>
		<pubDate>Fri, 17 Jun 2011 01:00:45 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6841</guid>
		<description><![CDATA[MongoDB自己采用的是ObjectId，因为是分布式存储，所以并没有用自增型的ID。 但有时我们却又需要这种类型的字段，如何去实现呢？ MongoDB中新建一个Collection，数据为 {“app”:collectionname,”UniqueId”:0} app为collection名称，UniqueId为当前的最大ID，利用MongoDB的FindAndModify方法，实现对指定collection中的UniqueId的自增操作。 这里采用的MongoDB驱动是官方推荐的MongoDB-CSharp]]></description>
			<content:encoded><![CDATA[<p>MongoDB自己采用的是ObjectId，因为是分布式存储，所以并没有用自增型的ID。</p>
<p>但有时我们却又需要这种类型的字段，如何去实现呢？</p>
<p>MongoDB中新建一个Collection，数据为</p>
<p>{“app”:collectionname,”UniqueId”:0}</p>
<p>app为collection名称，UniqueId为当前的最大ID，利用MongoDB的FindAndModify方法，实现对指定collection中的UniqueId的自增操作。</p>
<pre class="brush: csharp; title: ; notranslate">

Int64 GenerateUniqueId(string CollectionName,int InitValue)
 {
 Connect();
 CollectionName = CollectionName.Trim();

 var collection = db.GetCollection(&quot;UniqueStore&quot;);

 var query = Query.EQ(&quot;app&quot;, CollectionName);
 var update = Update.Inc(&quot;id&quot;, 1);

 var doc = collection.FindAndModify(query, SortBy.Null, update, true);
 if (doc.ModifiedDocument == null)
 {
 collection.Save(new BsonDocument { { &quot;app&quot;, CollectionName }, { &quot;UniqueId&quot;, InitValue } });
 return InitValue;
 }
 return Convert.ToInt64(doc.ModifiedDocument[&quot;UniqueId&quot;]);
 }
</pre>
<p>这里采用的MongoDB驱动是官方推荐的MongoDB-CSharp</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6841/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>python中的编码</title>
		<link>http://yibin.us/archives/6817</link>
		<comments>http://yibin.us/archives/6817#comments</comments>
		<pubDate>Wed, 02 Jun 2010 14:30:38 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[编码]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6817</guid>
		<description><![CDATA[在一些python讨论版里，经常会见到一些“月经帖”，类似于“我用python读取一个文件乱码”，然后就会抱怨python的编码很麻烦，其实不是python编码难搞定，而是没有真正理解python的编码。 如在windows环境下的以下示例代码： 此时的ansi.txt编码为ansi,我们在cmd窗口执行，看到如下结果： 此时一切正常，但，如果还是用上面的脚本去读取utf8.txt，文件是utf8编码，就会得到下面的结果： 经典的“乱码”出现了，有朋友可能会说了，我在python脚本里指定编码应该就解决了，于是： 再次运行： OMG，还是乱码。。。。 能不能正常输出中文不取决于#coding=utf-8，也不取决于目标文件的编码，而是取决于你的终端输出设备，这里就是CMD窗口，CMD窗口是不支持UTF-8的，它只支持GBK，所以，我们要转码。 那为什么第一个示例中，它能正确输出？因为目标文件本身就是GBK编码，所以不需要做任何转码就能正常输出。 在第二个示例中，我们只需要做一个小的改动，就可输出UTF8编码的文件： 结果： 正常输出。 做一个小结： python脚本中的#coding=utf-8并不是决定最终输出的编码，而是指定python脚本本身使用的编码；如果你的脚本本身内部有非ASCII码，那就应该指定编码#coding=gbk 如果文件的编码是GBK，你操作的终端也是支持GBK的，那就不需要编码，直接输出即可。 如果文件的编码不是GBK，但你的操作终端只支持GBK，不支持UTF-8，那么，你就需要对这些字符做转码操作。如文件编码是UTF-8，就需要先decode成UNICODE编码再encode成GBK]]></description>
			<content:encoded><![CDATA[<p>在一些python讨论版里，经常会见到一些“月经帖”，类似于“我用python读取一个文件乱码”，然后就会抱怨python的编码很麻烦，其实不是python编码难搞定，而是没有真正理解python的编码。</p>
<p>如在windows环境下的以下示例代码：</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
import sys
import os
def do():
    f = open('./ansi.txt')
    for a in [x.strip() for x in f]:
        print a
if __name__=='__main__':
    do()
</pre>
<p>此时的ansi.txt编码为ansi,我们在cmd窗口执行，看到如下结果：</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/06/00034.png"><img class="alignnone size-full wp-image-6818" title="00034" src="http://yibin.us/wp-content/uploads/2010/06/00034.png" alt="" width="387" height="217" /></a></p>
<p>此时一切正常，但，如果还是用上面的脚本去读取utf8.txt，文件是utf8编码，就会得到下面的结果：</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/06/00035.png"><img class="alignnone size-full wp-image-6819" title="00035" src="http://yibin.us/wp-content/uploads/2010/06/00035.png" alt="" width="493" height="249" /></a></p>
<p>经典的“乱码”出现了，有朋友可能会说了，我在python脚本里指定编码应该就解决了，于是：</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
#coding=utf-8     #在这里指定编码
import sys
import os

def do():

    f = open('./utf8.txt')
    for a in [x.strip() for x in f]:
        print a
if __name__=='__main__':
    do()
</pre>
<p>再次运行：</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/06/00036.png"><img class="alignnone size-full wp-image-6820" title="00036" src="http://yibin.us/wp-content/uploads/2010/06/00036.png" alt="" width="328" height="196" /></a></p>
<p>OMG，还是乱码。。。。</p>
<p>能不能正常输出中文不取决于#coding=utf-8，也不取决于目标文件的编码，而是取决于你的终端输出设备，这里就是CMD窗口，CMD窗口是不支持UTF-8的，它只支持GBK，所以，我们要转码。</p>
<p>那为什么第一个示例中，它能正确输出？因为目标文件本身就是GBK编码，所以不需要做任何转码就能正常输出。</p>
<p>在第二个示例中，我们只需要做一个小的改动，就可输出UTF8编码的文件：</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
#coding=utf-8
import sys
import os

def do():

    f = open('./utf8.txt')
    for a in [x.strip() for x in f]:
        print a.decode('UTF-8').encode('GBK')      #在这里转码
if __name__=='__main__':
    do()
</pre>
<p>结果：</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/06/00037.png"><img class="alignnone size-full wp-image-6821" title="00037" src="http://yibin.us/wp-content/uploads/2010/06/00037.png" alt="" width="319" height="211" /></a></p>
<p>正常输出。</p>
<p>做一个小结：</p>
<ol>
<li>python脚本中的#coding=utf-8并不是决定最终输出的编码，而是指定python脚本本身使用的编码；如果你的脚本本身内部有非ASCII码，那就应该指定编码#coding=gbk</li>
<li>如果文件的编码是GBK，你操作的终端也是支持GBK的，那就不需要编码，直接输出即可。</li>
<li>如果文件的编码不是GBK，但你的操作终端只支持GBK，不支持UTF-8，那么，你就需要对这些字符做转码操作。如文件编码是UTF-8，就需要先decode成UNICODE编码再encode成GBK</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6817/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Discuz发帖机Python版</title>
		<link>http://yibin.us/archives/6804</link>
		<comments>http://yibin.us/archives/6804#comments</comments>
		<pubDate>Fri, 19 Mar 2010 01:54:19 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[discuz]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6804</guid>
		<description><![CDATA[流程： 1、获取logging.php?action=login页的源代码，分析得到fromhash 2、输入用户名与密码，POST至/logging.php?action=login&#38;loginsubmit=yes地址 3、得到cookie并保存 4、访问发帖页面并附带上cookie 直接源码吧 运行截图]]></description>
			<content:encoded><![CDATA[<p>流程：</p>
<p>1、获取logging.php?action=login页的源代码，分析得到fromhash</p>
<p>2、输入用户名与密码，POST至/logging.php?action=login&amp;loginsubmit=yes地址</p>
<p>3、得到cookie并保存</p>
<p>4、访问发帖页面并附带上cookie</p>
<p><span id="more-6804"></span></p>
<p>直接源码吧</p>
<pre class="brush: python; title: ; notranslate">
#-*-coding:utf-8-*-
from urllib import urlencode
import cookielib, urllib2,urllib
import os,sys
import re
from xml.dom.minidom import parse, parseString
import getpass
import time
from Queue import Queue
import threading

class Discuz:
    def __init__(self,uid,pwd,**param):
        self.username = uid
        self.password = pwd
        self.para = param
        self.regex = {
            'loginreg':'&lt;input\s*type=&quot;hidden&quot;\s*name=&quot;formhash&quot;\s*value=&quot;([\w\W]+?)&quot;\s*\/&gt;',
            'postreg':'&lt;input\s*type=&quot;hidden&quot;\s*name=&quot;formhash&quot;\s*id=&quot;formhash&quot;\s*value=&quot;([\w\W]+?)&quot;\s*\/&gt;'
        }
        self.opener = None
        self.request = None
        self.islogin = False
        self.donecount = 0
        self.__login()
        self.threadcount = 100    #总线程数
        self.count = 0
        self.totalcount = 100000   #发帖量
    def __login(self):
        try:
            loginPage = urllib2.urlopen(self.para['loginurl']).read()
            formhash = re.search(self.regex['loginreg'],loginPage)
            formhash = formhash.group(1)
            print formhash
            print 'start login......'
            cookiejar = cookielib.CookieJar()
            self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
            values = {
                         'formhash':formhash,
                         'username':self.username,
                         'password':self.password,
                         'loginsubmit':'true'
                     }
            data = urllib.urlencode(values)
            self.request = urllib2.Request(self.para['loginsubmiturl'], data)
            rq = self.opener.open(self.request)

            #print 'login success......'
            self.islogin = True

        except Exception ,e:
            print 'Loggin Error:%s' % e
    def Post(self,subject,wysiwyg,content):
        threads = []
        for i in range(self.threadcount):
            t = threading.Thread(target=self.__postTopic,kwargs={'_subject':subject,'_wysiwyg':wysiwyg,'_body':content})
            threads.append(t)
        for i in range(self.threadcount):
            threads[i].start()
        lst = threading.enumerate()
        for i in range(self.threadcount):
            threads[i].join()
        print 'done'
    def __postTopic(self,**para):
        if not self.islogin:
            print 'please login......'
            return
        while self.count &lt; self.totalcount:
            try:
                print 'current count %d:' % self.count
                print 'current thread name %s' % (threading.currentThread().getName())
                self.request = urllib2.Request(self.para['posturl'])
                rq = self.opener.open(self.request)
                data = rq.read()
                formhash = re.search(self.regex['postreg'],data)
                formhash =  formhash.group(1)
                postdata = {
                    'addtags':'+可用标签',
                    'checkbox':'0',
                    'formhash':formhash,
                    'iconid':'',
                    'message':para['_body'],
                    'subject':para['_subject'],
                    'tags':'',
                    'updateswfattach' : '0',
                    'wysiwyg' : para['_wysiwyg']
                }
                self.request = urllib2.Request(self.para['postsubmiturl'],urllib.urlencode(postdata))
                self.opener.open(self.request)
                self.donecount+=1
                print '%d done.....' % self.donecount
            except Exception,e:
                print e
            if para.has_key('sleep'):
                time.sleep(float(para['sleep']))
            self.count +=1

if __name__=='__main__':
    name = raw_input('username:').strip()
    password = getpass.getpass('password:').strip()
    dz = Discuz(name,password,
    loginurl='http://localhost/logging.php?action=login',
    loginsubmiturl='http://localhost/logging.php?action=login&amp;loginsubmit=yes',
    posturl='http://localhost/post.php?action=newthread&amp;fid=28',
    postsubmiturl='http://localhost/post.php?&amp;action=newthread&amp;fid=28&amp;extra=&amp;topicsubmit=yes',
    sleep='1'
    )
    content='''新手如何 练级更加快？？送极品装备！新手必看
绑定升级钻石账号方法超简单：
1 新用户：直接到推广页面[url=http://tg.sdo.com/38797039]http://tg.sdo.com/38797039[/url]注册账号时在“推广员账号”一栏填推广员号adaccount直接升级钻石账号。
2 已经注册盛.大通行证的玩家在这里绑定直接成为 钻石账号 玩家
第一步: 点此 预绑定用户认证 [url=http://178.sdo.com/web/ConsumerBindUser.aspx]http://178.sdo.com/web/ConsumerBindUser.aspx[/url]
第二步: 点此进行最终绑定确认 [url=http://gift.sdo.com/Consumer/AIONBindUser.aspx]http://gift.sdo.com/Consumer/AIONBindUser.aspx[/url]

推广员填:adaccount'''

    dz.Post('送极品装备！新手必看','1',content)
</pre>
<p>运行截图</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/03/ScreenShot00094.jpg"><img class="alignnone size-full wp-image-6805" title="ScreenShot00094" src="http://yibin.us/wp-content/uploads/2010/03/ScreenShot00094.jpg" alt="" width="389" height="410" /></a></p>
<p><a href="http://yibin.us/wp-content/uploads/2010/03/ScreenShot00095.jpg"><img class="alignnone size-medium wp-image-6806" title="ScreenShot00095" src="http://yibin.us/wp-content/uploads/2010/03/ScreenShot00095-300x116.jpg" alt="" width="300" height="116" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6804/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>yibin.us</title>
		<link>http://yibin.us/archives/6786</link>
		<comments>http://yibin.us/archives/6786#comments</comments>
		<pubDate>Sat, 13 Mar 2010 01:25:09 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[没有分类]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6786</guid>
		<description><![CDATA[昨日在Godaddy购入yibin.us域名一枚，价格$4.99，按昨日的汇率，不足RMB35，现在终于有一个属于我的姓名的域名了，虽然后缀是.us。 Godaddy的域名购买过种那是相当容易，最主要是支持支付宝，免去用Paypal和信用卡之苦，当然，如果你有paypal也愿意用信用卡的美元支持也未尝不可。 Blog地址由原来的yibin.us变成yibin.us，并在.haccess对yibin001.com做了301跳转，全部跳到yibin.us 另外，阅读器的地址也已更改，gae现在不支持绑定非appspot.com的域名，不过，你可以通过注册一个企业套件来做反向代理。 rss4py.appspot.com已转到http://r.yibin.us，也已做301跳转： 相册：http://photo.yibin.us 书签：http://mark.yibin.us 如果你还未能访问yibin.us，可能是由于域名尚未解析到，稍等便可。]]></description>
			<content:encoded><![CDATA[<p><!--wsa:new-ads--><br />
昨日在Godaddy购入yibin.us域名一枚，价格$4.99，按昨日的汇率，不足RMB35，现在终于有一个属于我的姓名的域名了，虽然后缀是.us。</p>
<p>Godaddy的域名购买过种那是相当容易，最主要是支持支付宝，免去用Paypal和信用卡之苦，当然，如果你有paypal也愿意用信用卡的美元支持也未尝不可。</p>
<p>Blog地址由原来的yibin.us变成yibin.us，并在.haccess对yibin001.com做了301跳转，全部跳到yibin.us</p>
<pre class="brush: plain; title: ; notranslate">
# BEGIN WordPress
&lt;IfModule mod_rewrite.c&gt;
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yibin001.com$ [OR]
RewriteCond %{HTTP_HOST} ^yibin.us$
RewriteRule ^(.*)$ http://yibin.us/$1 [R=301,L]
&lt;/IfModule&gt;
# END WordPress
</pre>
<p>另外，阅读器的地址也已更改，gae现在不支持绑定非appspot.com的域名，不过，你可以通过注册一个企业套件来做反向代理。</p>
<p>rss4py.appspot.com已转到http://r.yibin.us，也已做301跳转：</p>
<pre class="brush: python; title: ; notranslate">
if request.headers[&quot;Host&quot;].lower() != settings.AppDomain.lower()[7:] and settings.Enable302:
            self.redirect(settings.AppDomain+self.pathinfo,True)
</pre>
<p>相册：http://photo.yibin.us<br />
书签：http://mark.yibin.us<br />
如果你还未能访问yibin.us，可能是由于域名尚未解析到，稍等便可。<br />
<!--wsa:new-ads--></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6786/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>google appengine中的url routing</title>
		<link>http://yibin.us/archives/6775</link>
		<comments>http://yibin.us/archives/6775#comments</comments>
		<pubDate>Thu, 04 Mar 2010 01:17:13 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[gae]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6775</guid>
		<description><![CDATA[在做rss4py的过程中发现这样的一个问题 ，直到现在也没有找到良好的解决方案。 在这里，有三种符合匹配条件的url被交给Main controller去处理，即： /member/1                                 成员ID为1的记录 /member/1/page/2                成员ID为1的第2页记录 /page/1                                         所有成页的第1页记录 Main Controller: [...]]]></description>
			<content:encoded><![CDATA[<p><!--wsa:new-ads--><br />
在做rss4py的过程中发现这样的一个问题 ，<del datetime="2010-03-04T04:10:22+00:00">直到现在也没有找到良好的解决方案。</del></p>
<pre class="brush: python; title: ; notranslate">
def main():
    webapp.template.register_template_library('filter')
    application = webapp.WSGIApplication(
                                       [
                                        (r'/member/(?P&lt;site&gt;[0-9]+)/?', Main),
                                        (r'/member/(?P&lt;site&gt;[0-9]+)/page/(?P&lt;page&gt;[0-9]+)/?', Main),
                                        (r'/page/(?P&lt;page&gt;[0-9]*)/?', Main),
                                        ('.*',Error404),
                                       ], debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == &quot;__main__&quot;:
    main()
</pre>
<p>在这里，有三种符合匹配条件的url被交给Main controller去处理，即：</p>
<ol>
<li>/member/1                                 成员ID为1的记录</li>
<li>/member/1/page/2                成员ID为1的第2页记录</li>
<li>/page/1                                         所有成页的第1页记录</li>
</ol>
<p>Main Controller:</p>
<pre class="brush: python; title: ; notranslate">
class Main():
    def get(self,site = 0,page=1):
        self.write('site : %s' % site)
        self.write('&lt;br /&gt;page: %s' % page)
        return
</pre>
<p>当访问/member/1/时，输出：site:1 page:1<br />
访问/member/1/page/2/ 时，输出： site:1 page:2<br />
访问/page/3 时，输出：site:3 page:1<br />
可以看出，前面二个URL的区配都是正确的，但第3个是错误的，需要的输出是：site:0 page:3，很明显，3被当成参数site被传递了，<br />
如果单纯交换Main/get方法中site/page参数的顺序的话，第三个URL匹配正确，第一、二个就不正确了。</p>
<p>方案1：将URL规则做如下修改：</p>
<pre class="brush: python; title: ; notranslate">
def main():
    webapp.template.register_template_library('filter')
    application = webapp.WSGIApplication(
                                       [
                                       (r'/(?P&lt;site&gt;member/[0-9]+)/?', Main),
                                       (r'/(?P&lt;site&gt;member/[0-9]+)/(?P&lt;page&gt;page/[0-9]+)/?', Main),
                                       (r'/(?P&lt;page&gt;page/[0-9]*)/?', Main),
                                       ], debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == &quot;__main__&quot;:
    main()
</pre>
<p>修改后的URL规则中将member/page都做为参数传递到Main的get方法中，即访问/member/1/page/5时，get方法中的site参数值为member/1，page参数值为page/5，这样轻易分析一下就能得到正确的site/page值。</p>
<p>方案2：一个URL规则搞定：</p>
<p>(?:/member/(?P&lt;site&gt;[0-9]+))?(?:/page/?(?P&lt;page&gt;[0-9]+))?/?</p>
<p>这个正则太狠了<br />
<!--wsa:new-ads--></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6775/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>臭蛋聚测试版发布</title>
		<link>http://yibin.us/archives/6772</link>
		<comments>http://yibin.us/archives/6772#comments</comments>
		<pubDate>Thu, 25 Feb 2010 03:13:15 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[gae]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6772</guid>
		<description><![CDATA[臭蛋聚是一个RSS聚合器，构建在Google App Engine上的，写的这个小应用是用来取代原有的feed.yibin001.com(和谐了)。 有段时间没写gae的小应用了，刚开始时还显得有点陌生，在此期间也遇到一些小问题，在这里一并写出来，希望对初次开发gae应用的朋友有一个帮助。 gae datastore，在gae中，所有Gql的排序字段都是需要建索引的，否则会出现经典的”NeedIndexError: no matching index found.”错误，附在错误后面的信息就是需要建立的索引，你可以复制下来，然后粘帖到index.yaml文件，并上传。上传后并不能马上使用，gae需要一个build的过程，这个时间长短要看数据量的大小，索引状态变成Serving时就意味着索引已创建成功。 Gql查询结果排序问题。排序的字段要有值，不能为None，否则结果集可能不是你所需要的。如:SELECT * FROM Feeds ORDER BY LastPubDate DESC，如果此时的LastPubDate都没有值，即使有10条数据，这条查询无法返回记录的。 对于GMT(rfc822)时间格式的解析。按rss标准，PubDate正确格式应该是Sat, 20 Feb 2010 07:04:52 +0000，直接用datetime模块解析不是很方便，可以用rfc822模块中的parsedate_tz函数进行转换。对于非标准GMT(rfc822)格式的时间，只能配合正则进行解析。 最后，臭蛋聚的地址为:http://rss4py.appspot.com/]]></description>
			<content:encoded><![CDATA[<p><a href="http://rss4py.appspot.com/" target="_blank">臭蛋聚</a>是一个RSS聚合器，构建在Google App Engine上的，写的这个小应用是用来取代原有的feed.yibin001.com(和谐了)。</p>
<p>有段时间没写gae的小应用了，刚开始时还显得有点陌生，在此期间也遇到一些小问题，在这里一并写出来，希望对初次开发gae应用的朋友有一个帮助。</p>
<ol>
<li>gae datastore，在gae中，所有Gql的排序字段都是需要建索引的，否则会出现经典的”NeedIndexError: no matching index found.”错误，附在错误后面的信息就是需要建立的索引，你可以复制下来，然后粘帖到index.yaml文件，并上传。上传后并不能马上使用，gae需要一个build的过程，这个时间长短要看数据量的大小，索引状态变成<strong>Serving</strong>时就意味着索引已创建成功。</li>
<li>Gql查询结果排序问题。排序的字段要有值，不能为None，否则结果集可能不是你所需要的。如:SELECT * FROM Feeds ORDER BY LastPubDate DESC，如果此时的LastPubDate都没有值，即使有10条数据，这条查询无法返回记录的。</li>
<li>对于GMT(rfc822)时间格式的解析。按rss标准，PubDate正确格式应该是Sat, 20 Feb 2010 07:04:52 +0000，直接用datetime模块解析不是很方便，可以用rfc822模块中的parsedate_tz函数进行转换。对于非标准GMT(rfc822)格式的时间，只能配合正则进行解析。</li>
</ol>
<p>最后，臭蛋聚的地址为:<a href="http://rss4py.appspot.com">http://rss4py.appspot.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6772/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>album4gae及imagespider版本更新</title>
		<link>http://yibin.us/archives/6763</link>
		<comments>http://yibin.us/archives/6763#comments</comments>
		<pubDate>Fri, 19 Feb 2010 10:48:39 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[没有分类]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6763</guid>
		<description><![CDATA[album4gae没有做较大的调整，后台图片上传改用swfupload的批量上传，单个文件同样受gae的限制，不能超过1M。 上传成功后的预览： 前台演示： http://album4gae.appspot.com imagespider增加对image.google.cn的搜索支持，图片保存在程序目录/download/搜索关键字下，目录安排更为合理。 album4gae请到这里下载： http://code.google.com/p/album4gae/downloads/list imagespider下载： http://u.115.com/file/f7c8516c94]]></description>
			<content:encoded><![CDATA[<p><!--wsa:new-ads--><br />
<strong>album4gae</strong>没有做较大的调整，后台图片上传改用swfupload的批量上传，单个文件同样受gae的限制，不能超过1M。</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/02/00002.png"><img class="alignnone size-full wp-image-6764" title="00002" src="http://yibin.us/wp-content/uploads/2010/02/00002.png" alt="" width="417" height="270" /></a></p>
<p>上传成功后的预览：</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/02/00003.png"><img class="alignnone size-full wp-image-6765" title="00003" src="http://yibin.us/wp-content/uploads/2010/02/00003.png" alt="" width="574" height="301" /></a></p>
<p>前台演示：</p>
<p><a href="http://album4gae.appspot.com" target="_blank">http://album4gae.appspot.com</a></p>
<p><strong>imagespider</strong>增加对image.google.cn的搜索支持，图片保存在程序目录/download/搜索关键字下，目录安排更为合理。</p>
<p><a href="http://yibin.us/wp-content/uploads/2010/02/00004.png"><img class="alignnone size-full wp-image-6766" title="00004" src="http://yibin.us/wp-content/uploads/2010/02/00004.png" alt="" width="353" height="179" /></a></p>
<p>album4gae请到这里下载：</p>
<p><a href="http://code.google.com/p/album4gae/downloads/list" target="_blank">http://code.google.com/p/album4gae/downloads/list</a></p>
<p>imagespider下载：</p>
<p><a href="http://u.115.com/file/f7c8516c94" target="_blank">http://u.115.com/file/f7c8516c94</a><!--wsa:new-ads--></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6763/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>百度图片抓取工具</title>
		<link>http://yibin.us/archives/6682</link>
		<comments>http://yibin.us/archives/6682#comments</comments>
		<pubDate>Sun, 01 Nov 2009 09:17:14 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6682</guid>
		<description><![CDATA[有时我们需要从baidu上找一些图片，比如某一个美女或某一个帅哥，一张一张下载太麻烦，于是产生了下面这个工具 运行截图： 分析每一页的图片地址： 启动多个线程同时下载： 下载完成： 工具会在当前运行目录生成一个download目录用来存放图片，请确保程序文件不在含有中文字符的路径中。 运行压缩包中的.exe即可。 http://u.115.com/file/f7c8516c94 ps.在西塘逛了一天，好累，谢谢西厢阁客栈的沈姐，谢谢她的接送。相关影像： http://photo.yibin.us/album/5001/]]></description>
			<content:encoded><![CDATA[<p>有时我们需要从baidu上找一些图片，比如某一个美女或某一个帅哥，一张一张下载太麻烦，于是产生了下面这个工具</p>
<p>运行截图：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/11/0000002.png"><img class="alignnone size-full wp-image-6684" title="0000002" src="http://yibin.us/wp-content/uploads/2009/11/0000002.png" alt="0000002" width="413" height="126" /></a></p>
<p><span id="more-6682"></span></p>
<p>分析每一页的图片地址：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/11/0000003.png"><img class="alignnone size-full wp-image-6685" title="0000003" src="http://yibin.us/wp-content/uploads/2009/11/0000003.png" alt="0000003" width="646" height="173" /></a></p>
<p>启动多个线程同时下载：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/11/0000004.png"><img class="alignnone size-full wp-image-6686" title="0000004" src="http://yibin.us/wp-content/uploads/2009/11/0000004.png" alt="0000004" width="519" height="204" /></a></p>
<p>下载完成：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/11/0000005.png"><img class="alignnone size-full wp-image-6687" title="0000005" src="http://yibin.us/wp-content/uploads/2009/11/0000005.png" alt="0000005" width="560" height="401" /></a></p>
<p>工具会在当前运行目录生成一个download目录用来存放图片，请确保程序文件不在含有中文字符的路径中。</p>
<p>运行压缩包中的.exe即可。</p>
<p><a href="http://u.115.com/file/f7c8516c94" target="_blank">http://u.115.com/file/f7c8516c94</a></p>
<p>ps.在西塘逛了一天，好累，谢谢西厢阁客栈的沈姐，谢谢她的接送。相关影像：</p>
<p><a href="http://photo.yibin.us/album/5001/" target="_blank">http://photo.yibin.us/album/5001/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6682/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>开心网省份、城市、学校数据库版</title>
		<link>http://yibin.us/archives/6675</link>
		<comments>http://yibin.us/archives/6675#comments</comments>
		<pubDate>Fri, 30 Oct 2009 05:41:35 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6675</guid>
		<description><![CDATA[目前支持SqLite及MySql数据库，运行时会有选择数据库的选项 如果是MySql数据库，请确保目标库已建立，请确保登录用户有建立数据表的权限。 如果是SqLite数据库，库文件会自动在该文件所在的目录下生成，库名为kaixin001.db3 MySql入库结果： SqLite入库查看： 可执行文件打包下载，解压后直接运行kaixin001.exe即可 注：请确保可执行文件的绝对路径中没有中文字符，否则运行失败 下载已失效，提供MySql脚本 http://yibin.us/upload/kaixin001.sql.gz]]></description>
			<content:encoded><![CDATA[<p><!--wsa:new-ads--><br />
目前支持SqLite及MySql数据库，运行时会有选择数据库的选项</p>
<p>如果是MySql数据库，请确保目标库已建立，请确保登录用户有建立数据表的权限。</p>
<p>如果是SqLite数据库，库文件会自动在该文件所在的目录下生成，库名为kaixin001.db3</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00055.jpg"><img class="alignnone size-full wp-image-6676" title="ScreenShot00055" src="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00055.jpg" alt="ScreenShot00055" width="318" height="177" /></a></p>
<p><span id="more-6675"></span></p>
<p>MySql入库结果：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00056.jpg"><img class="alignnone size-full wp-image-6677" title="ScreenShot00056" src="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00056.jpg" alt="ScreenShot00056" width="411" height="403" /></a></p>
<p>SqLite入库查看：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00057.jpg"><img class="alignnone size-full wp-image-6678" title="ScreenShot00057" src="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00057.jpg" alt="ScreenShot00057" width="439" height="395" /></a></p>
<p><span style="text-decoration: line-through;">可执行文件打包下载，解压后直接运行kaixin001.exe即可</span></p>
<p><span style="color: #ff0000;"><strong>注：请确保可执行文件的绝对路径中没有中文字符，否则运行失败</strong></span></p>
<p>下载已失效，提供MySql脚本</p>
<p><a href="http://yibin.us/upload/kaixin001.sql.gz">http://yibin.us/upload/kaixin001.sql.gz</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6675/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>获取开心网的省份、城市及学校列表</title>
		<link>http://yibin.us/archives/6668</link>
		<comments>http://yibin.us/archives/6668#comments</comments>
		<pubDate>Wed, 28 Oct 2009 08:01:33 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6668</guid>
		<description><![CDATA[目前在做公司的一个SNS项目，其中有一个模块就是添加教育经历，整个流程与开心网基本一致。 开心网中的城市列表及学校列表返回格式是json的，c#中不大好处理json，不能“智能”地将json字符串转换为一个“对象”。 php处理json异常简单，直接用json_decode就行了。 这里是用python实现的，用到了第三方的一个json模块，调用后直接将json转成了list 运行时效果如下： web上]]></description>
			<content:encoded><![CDATA[<p>目前在做公司的一个SNS项目，其中有一个模块就是添加教育经历，整个流程与开心网基本一致。</p>
<p>开心网中的城市列表及学校列表返回格式是json的，c#中不大好处理json，不能“智能”地将json字符串转换为一个“对象”。</p>
<p>php处理json异常简单，直接用json_decode就行了。</p>
<p>这里是用python实现的，用到了第三方的一个json模块，调用后直接将json转成了list</p>
<p>运行时效果如下：</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00053.jpg"><img class="alignnone size-full wp-image-6669" title="ScreenShot00053" src="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00053.jpg" alt="ScreenShot00053" width="533" height="260" /></a></p>
<p><span id="more-6668"></span></p>
<p>web上</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00054.jpg"><img class="alignnone size-full wp-image-6670" title="ScreenShot00054" src="http://yibin.us/wp-content/uploads/2009/10/ScreenShot00054.jpg" alt="ScreenShot00054" width="611" height="474" /></a></p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
#coding=utf-8
import sys
import os
import json
import urllib2
import time

provinces = []
citys = []
areas = []

def GetProvince():
    '''获取省及直辖市'''
    url = 'http://www.kaixin001.com/interface/suggestlocation.php?type=0'
    request = urllib2.urlopen(url)
    jsonData = request.read()
    d = json.read(jsonData)
    global provinces

    for a in d:
        provinces.append(a['id'])
        print 'get:'+a['name']
        if int(a['type']) == -1: #获取直辖市
            GetArea(a['id'],3)
        else:
            GetCity(1,a['id'])

def GetCity(typeid,provinceid):
    '''获取城市'''
    url = 'http://www.kaixin001.com/interface/suggestlocation.php?type=%s&amp;id=%s' % (typeid,provinceid)
    global citys
    request = urllib2.urlopen(url)
    jsonData = request.read()
    d = json.read(jsonData)
    for a in d:
        citys.append(a['id'])
        print 'get:'+a['name']
        GetArea(a['id'])

def GetArea(cityid,typeid = 2):
    '''获取区级县级地名'''
    url = 'http://www.kaixin001.com/interface/suggestlocation.php?type=%s&amp;id=%s' % (typeid, cityid)
    global areas
    request = urllib2.urlopen(url)
    jsonData = request.read()
    d = json.read(jsonData)
    for a in d:
        areas.append(a['id'])
        print 'get:'+a['name']

def GetSchool(schooltype,cityid):
    '''获取学校'''
    url = 'http://www.kaixin001.com/interface/suggestlocation.php?type=%s&amp;id=%s' % (schooltype,cityid)
    request = urllib2.urlopen(url)
    jsonData = request.read()
    d = json.read(jsonData)
    for a in d:
        print 'get'+a['name']

if __name__=='__main__':

    GetProvince()
    print u'共有省份或直辖市: %d ' %  len(provinces)
    print u'共有城市: %d ' % len(citys)
    print u'共有区县: %d ' % len(areas)
    citys.extend(areas)

    #大学
    for pid in provinces:
        time.sleep(1)
        GetSchool(4,pid)
    #高中初中及技校
    for cid in citys:
        for i in (5,6,7):
            GetSchool(i,cid)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6668/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

