【发布时间】:2014-08-26 05:37:32
【问题描述】:
我正在尝试将我的所有 Livejournal 帖子复制到我在 blogger.com 上的新博客。我通过使用gdata python client 附带的稍微修改的example 来做到这一点。我有一个 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)
【问题讨论】:
-
你能绕过并通过 python 独立脚本手动将每条记录从一个数据库写入另一个数据库吗?不熟悉 livejournal 或 blogger,但我不得不批量处理大量帖子,所以我有兴趣提供帮助。
-
@Joaq2Remember 对不起,我没有真正关注,请您澄清一下吗?谢谢。
-
可能建立到两个数据库的两个连接; livejournal和博客。从 live journal 中选择然后将副本写入 blogger 或建立 blogger db 连接并通过解析 Json 写入。
-
@Joaq2Remember 我希望我能做到,但博主只提供 REST API,所以我无法直接访问他们的数据库。
-
那么您的问题是对他们的 API 发布请求的硬性限制?除了 API 之外,还有其他发布项目吗?我可能会建议使用机器人通过 CMS 发布这些帖子,或者尝试与博客中的某个人取得联系,看看他们是否可以帮助您。
标签: python batch-processing blogger gdata-python-client