使用gdata python客户端在博客上批量发布

 mobiledu2502876733 发布于 2022-12-25 17:51

我正在尝试将我的所有Livejournal帖子复制到blogger.com上的新博客.我通过使用gdata python客户端附带的略微修改的示例来实现.我有一个json文件,其中包含从Livejournal导入的所有帖子.问题是blogger.com每天发布新博客条目的每日限制 - 50,所以你可以想象我将在一个月内复制1300多个帖子,因为我无法在50次导入后以编程方式输入验证码.

我最近了解到gdata中还有批处理操作模式,但我无法弄清楚如何使用它.谷歌搜索并没有真正帮助.

任何建议或帮助将受到高度赞赏.

谢谢.

更新

为了以防万一,我使用以下代码

#!/usr/local/bin/python
import json
import requests

from gdata import service
import gdata
import atom
import getopt
import sys

from datetime import datetime as dt
from datetime import timedelta as td
from datetime import tzinfo as tz

import time

allEntries = json.load(open("todays_copy.json", "r"))

class TZ(tz):
    def utcoffset(self, dt): return td(hours=-6)

class BloggerExample:
    def __init__(self, email, password):
        # Authenticate using ClientLogin.
        self.service = service.GDataService(email, password)
        self.service.source = "Blogger_Python_Sample-1.0"
        self.service.service = "blogger"
        self.service.server = "www.blogger.com"
        self.service.ProgrammaticLogin()

        # Get the blog ID for the first blog.
        feed = self.service.Get("/feeds/default/blogs")
        self_link = feed.entry[0].GetSelfLink()
        if self_link:
            self.blog_id = self_link.href.split("/")[-1]

    def CreatePost(self, title, content, author_name, label, time):
        LABEL_SCHEME = "http://www.blogger.com/atom/ns#"
        # Create the entry to insert.
        entry = gdata.GDataEntry()
        entry.author.append(atom.Author(atom.Name(text=author_name)))
        entry.title = atom.Title(title_type="xhtml", text=title)
        entry.content = atom.Content(content_type="html", text=content)
        entry.published = atom.Published(time)
        entry.category.append(atom.Category(scheme=LABEL_SCHEME, term=label))

        # Ask the service to insert the new entry.
        return self.service.Post(entry, 
            "/feeds/" + self.blog_id + "/posts/default")

    def run(self, data):
        for year in allEntries:
            for month in year["yearlydata"]:
                for day in month["monthlydata"]:
                    for entry in day["daylydata"]:
                        # print year["year"], month["month"], day["day"], entry["title"].encode("utf-8")
                        atime = dt.strptime(entry["time"], "%I:%M %p")
                        hr = atime.hour
                        mn = atime.minute
                        ptime = dt(year["year"], int(month["month"]), int(day["day"]), hr, mn, 0, tzinfo=TZ()).isoformat("T")
                        public_post = self.CreatePost(entry["title"],
                            entry["content"],
                            "My name",
                            ",".join(entry["tags"]),
                            ptime)
                        print "%s, %s - published, Waiting 30 minutes" % (ptime, entry["title"].encode("utf-8"))
                        time.sleep(30*60)


def main(data):
    email = "my@email.com"
    password = "MyPassW0rd"

    sample = BloggerExample(email, password)
    sample.run(data)

if __name__ == "__main__":
    main(allEntries)

Prayag Verma.. 15

我建议改用Google博客转换器(https://code.google.com/archive/p/google-blog-converters-appengine/)

要开始,你必须经历

https://github.com/google/gdata-python-client/blob/master/INSTALL.txt - 设置Google GData API的步骤 https://github.com/pra85/google-blog-converters-appengine/blob /master/README.txt - 使用博客转换器的步骤

一旦完成所有设置,就必须运行以下命令(LiveJournal用户名和密码)

livejournal2blogger.sh -u  -p  [-s ]

将其输出重定向到.xml文件.现在可以通过访问Blogger信息中心,您的博客> 设置 > 其他 > 博客工具 > 导入博客,直接将此文件导入Blogger 博客

请记住检查" 自动发布所有导入的帖子和页面"选项.之前我曾经尝试过一次有超过400个帖子的博客,Blogger成功导入并发布了它们没有问题

如果您怀疑Blogger可能存在一些问题(因为帖子数量很高),或者您的帐户中有其他Blogger博客.然后,为了预防起见,请创建一个单独的Blogger(Google)帐户,然后尝试导入帖子.之后,您可以将管理控制转移到您真实的Blogger帐户(要转移,您首先必须发送作者邀请,然后将您真实的Blogger帐户提升到管理员级别,最后删除虚拟帐户.发送邀请的选项出现在设置>基本>权限>博客作者)

还要确保您使用的是Python 2.5,否则这些脚本将无法运行.在运行livejournal2blogger.sh之前,请更改以下行(感谢Michael Fleet提供此修复http://michael.f1337.us/2011/12/28/google-blog-converters-blogger2wordpress/)

PYTHONPATH=${PROJ_DIR}/lib python ${PROJ_DIR}/src/livejournal2blogger/lj2b.py $*

PYTHONPATH=${PROJ_DIR}/lib python2.5 ${PROJ_DIR}/src/livejournal2blogger/lj2b.py $*

PS我知道这不是你的问题的答案,但由于这个答案的目标与你的问题相同(要在一天内导入超过50个帖子),这就是我分享它的原因.我对Python或GData API知之甚少,我设置环境并按照这些步骤回答这个问题(我能够将LiveJournal中的帖子导入到Blogger中).

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有