Posts Tagged ‘python’

metaWeblog同步博客遇到的问题

最近有一个迫切的需求,需要同步几个博客。目前我有的博客是本站,还有hesicong.cnblogs.com和www.csdn.net/hesicong共三个。其后两个由于各种问题长久以来没有得到更新。我的需求就是写一个软件能够将wordpress的文章同步到cnblogs和csdn(成为次博客)。对于主次博客都有的文章修改次博客为主博客内容,对于次博客没有的文章按照时间新建一个。从而实现博客同步。

技术上使用metaWeblog就可以实现上述目的,选用python的xmlrpclib可以方便的进行xmlrpc操作。做一个控制台的小程序足够了我使用了。

然后开始技术实验,发现:

  1. wordpress支持metaWeblog很好,可以实现所有的功能。从wordpress可以通过metaWeblog.getRecentPosts函数得到所有的文章。
  2. cnblogs也支持metaWeblog,也支持的很好。cnblogs也支持我的语法高亮。但遗憾的是:第一:metaWeblog.getRecentPosts函数最多能够返回100个文章。而我的cnblogs目前有230篇文章,很显然,cnblogs限制了文章数量;第二:metaWeblog.newPost函数即便Post结构中有dateCreated,但cnblogs的主界面中依然按照当前时间计算,造成文章时间对不上号,顺序混乱。
  3. csdn就是个垃圾,metaWeblog表面上支持,暗地里出问题。metaWeblog.getRecentPosts,metaWeblog.editPost都无法用,提示User not exist。仅有metaWeblog.newPost可以用,但csdn的blog的语法高亮无法用,页面很难看。

所以,想实现我的目的通过metaWeblog看来是没希望了。除非cnblogs调整文章数量限制,csdn希望从垃圾变成战斗机了。

附我写的一个测试代码,不完善,仅作为参考:

import xmlrpclib

class Metablog:
    def __init__(self, url, username, password):
        self.username=username
        self.password=password
        self.url=url
        self.server=xmlrpclib.ServerProxy(url)
        self.posts=None

    def getAllPosts(self):
        print "Getting all posts from "+self.url
        self.posts=self.server.metaWeblog.getRecentPosts('', self.username, self.password, 9999999)
        print "found "+ str(len(self.posts)) +" posts"
        return self.posts;

    def getAllPostTitle(self):
        if self.posts==None:
            self.getAllPosts()

        ret=dict()
        for post in self.posts:
            ret[post["postid"]]=post["title"]

        return ret

    def getPost(self, id):
        for post in self.posts:
            if post["postid"]==id:
                return post

        return None

    def newPost(self, post):
        self.server.metaWeblog.newPost('', self.username, self.password, post, True)

    def editPost(self,postid, post):
        self.server.metaWeblog.editPost(postid, self.username, self.password, post, True)

    def delPost(self, postid):
        self.server.metaWeblog.deletePost('',postid, self.username, self.password, True)

def syncBlog(b1, b2):
    b1Titles=b1.getAllPostTitle()
    b2Titles=b2.getAllPostTitle()
    for key1,value1 in b1Titles.iteritems():
        print "Blog1 title: "+value1
        for key2,value2 in b2Titles.iteritems():
            print "\tBlog2 title: "+value2
            if value1==value2:
                print "Syncing, blog 2 postid="+key2
                b2.editPost(keys2, b1.getPost(key1))
                break

        print "Blog2 has no article equal to title :"+value1
        print "Add new "
        b2.newPost(b1.getPost(key1))

    print "Done sync"                    

wpBlog=Metablog("主站地址", "用户名", "密码")
cnBlog=Metablog("从站地址", "用户名", "密码")
syncBlog(wpBlog, cnBlog)

Grammer Girl podcast downloader

Same as ESL podcast downloader, a simple tool to fetch all the podcast and descriptions from Grammer Girl. Use at your own risk!


  GGDownloader.py (1.6 KiB, 366 hits)

ESL podcast downloader

When I first met ESL podcast, I found it was very useful for your listening and it expanded your vocabulary and your communicate skills. On the website, you can download all of the podcasts and read the scripts online by free. There are now 670-odd podcasts on the website, so it is a huge treasure for English learners.

I was wondering if I could create a tool and download them all at once including the audio index and the script and then convert them to a plain text form in order that I can listen to the podcast and read the script on my phone. To realize my idea, I wrote a small program by python under Linux.

Download it:

  eslDownloader.py (2.8 KiB, 486 hits)

How to steps:

  1. Be sure you are using Linux or Unix system or Cygwin system.
  2. Make sure you have installed Python 2.x
  3. Copy the script to any folder you like.
  4. Make sure you have right to create a sub-directory under this folder. My script will create a folder called "eslpod" to store the content.
  5. Run this script.

You will see the script start to fetch the pages and download the podcasts. Downloaded files will store under "eslpod" folder and grouping by the tag. Under every tag, you could see the script with .html suffix and podcast with .mp3 suffix.

You can interrupt the downloading process by kill the process.

DISCLAIMER:

  • Use this script at your own risk.
  • This program may fail if the site was changed.
  • Respect the authors' work and do not distribute without their permission.

And at last, be happy and confidence to study English!

python:整数转换为任意进制(<=36)

javascript提供了一个number.toString(baseNum)的函数,能够将number转换为36进制以下的字符串。
python里面仅提供了将字符串转换为整数的函数,并没有提供相应的函数将整数转换为任意进制的字符串的函数(如果有的话,请告诉我一声)。在网上找到了答案Read more

在Linux下开发Python S60程序初步——连上手机

今天突然想起来了,以前在Windows里面可以用超级终端链接通过蓝牙连接手机,但在Linux下面怎么做呢?做个笔记:
sdptool add --channel=10 SP
while true; do rfcomm listen /dev/rfcomm0 10; done

# On the cell phone, Open "Bluetooth Console" in Python menu and
# choose the Linux PC as the machine to connect to
#
# Once the phone is connected, open another terminal and type

screen /dev/rfcomm0

输入后会出现以下字样: Read more

1
Return top