Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch posting on blogger using gdata python client

I'm trying to copy all my Livejournal posts to my new blog on blogger.com. I do so by using slightly modified example that ships with the gdata python client. I have a json file with all of my posts imported from Livejournal. Issue is that blogger.com has a daily limit for posting new blog entries per day — 50, so you can imagine that 1300+ posts I have will be copied in a month, since I can't programmatically enter captcha after 50 imports.

I recently learned that there's also batch operation mode somewhere in gdata, but I couldn't figure how to use it. Googling didn't really help.

Any advice or help will be highly appreciated.

Thanks.

Update

Just in case, I use the following code

#!/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 = "[email protected]"
    password = "MyPassW0rd"

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

if __name__ == "__main__":
    main(allEntries)
like image 424
Kaster Avatar asked Aug 26 '14 05:08

Kaster


1 Answers

I would recommend using Google Blog converters instead ( https://code.google.com/archive/p/google-blog-converters-appengine/ )

To get started you will have to go through

https://github.com/google/gdata-python-client/blob/master/INSTALL.txt - Steps for setting up Google GData API https://github.com/pra85/google-blog-converters-appengine/blob/master/README.txt - Steps for using Blog Convertors

Once you have everything setup , you would have to run the following command (its the LiveJournal Username and password)

livejournal2blogger.sh -u <username> -p <password> [-s <server>]

Redirect its output into a .xml file. This file can now be imported into a Blogger blog directly by going to Blogger Dashboard , your blog > Settings > Other > Blog tools > Import Blog

Here remember to check the Automatically publish all imported posts and pages option. I have tried this once before with a blog with over 400 posts and Blogger did successfully import & published them without issue

Incase you have doubts the Blogger might have some issues (because the number of posts is quite high) or you have other Blogger blogs in your account. Then just for precaution sake , create a separate Blogger (Google) account and then try importing the posts. After that you can transfer the admin controls to your real Blogger account (To transfer , you will first have to send an author invite , then raise your real Blogger account to admin level and lastly remove the dummy account. Option for sending invite is present at Settings > Basic > Permissions > Blog Authors )

Also make sure that you are using Python 2.5 otherwise these scripts will not run. Before running livejournal2blogger.sh , change the following line (Thanks for Michael Fleet for this fix http://michael.f1337.us/2011/12/28/google-blog-converters-blogger2wordpress/ )

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

to

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

P.S. I am aware this is not the answer to your question but as the objective of this answer is same as your question (To import more than 50 posts in a day) , Thats why I shared it. I don't have much knowledge of Python or GData API , I setup the environment & followed these steps to answer this question (And I was able to import posts from LiveJournal to Blogger with it ).

like image 77
Prayag Verma Avatar answered Oct 01 '22 16:10

Prayag Verma