Search This Site

Custom Search

Thursday, January 13, 2011

Re: [bloggerDev] posting html content

Heya Arun,

I tidied up your code and it works now. I've attached it so that you can review the changes I made.

The mistakes you are making indicate that your time would be best spent learning python, and one of the best resources I know is Mark Pilgrim's Dive into Python, available in both book form, and free online at: http://diveintopython.org/

brett

On Thu, Jan 13, 2011 at 10:24 PM, arun <arunkumar413@gmail.com> wrote:
Hi, i'm trying to experiment with blogger api using puthon.So wrote a
simple test code. I created the blog post content with open office and
saved it as a html document. In the code i used the open().read()
method to read the content of the html file and  used the client to
post it to blogger. But i'm getting these errors:

program code:
#!/usr/bin/python

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

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 PrintUserBlogTitles(self):
           """Prints a list of all the user's blogs."""

           # Request the feed.
           query = service.Query()
           query.feed = '/feeds/default/blogs'
           feed = self.service.Get(query.ToUri())

           # Print the results.
           print feed.title.text
           for entry in feed.entry:
                print "\t" + entry.title.text
           print


       def CreatePost(self, title, content, author_name, is_draft):
           # 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)
           if is_draft:
                control = atom.Control()
                control.draft = atom.Draft(text='yes')
                entry.control = control

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

def main():
       email=" "
       password=" "
       sample = BloggerExample(email, password)
       priblog=BloggerExample.PrintUserBlogTitles(sample)
       body=open("justblog.html", "r").readlines()
       postit=BloggerExample.CreatePost(sample,'testpost',body,"Post
author", False)
       print "success"

if __name__ == '__main__':
 main()
________________________________________________________________________
--------------------------------------------------------------------------------------------------------------------
arun@arun-desktop:~/Desktop$ python blogit.py
arunkumar's Blogs
       ClicknScroll
       testblog
       CircuitPlanet
       arunkumar413.blogspot.com

Traceback (most recent call last):
 File "blogit.py", line 87, in <module>
   main()
 File "blogit.py", line 81, in main
   postit=BloggerExample.CreatePost(sample,'testpost',body,"Post
author", False)
 File "blogit.py", line 71, in CreatePost
   '/feeds/' + self.blog_id + '/posts/default')
 File "/usr/lib/pymodules/python2.6/gdata/service.py", line 1146, in
Post
   media_source=media_source, converter=converter)
 File "/usr/lib/pymodules/python2.6/gdata/service.py", line 1231, in
PostOrPut
   headers=extra_headers)
 File "/usr/lib/pymodules/python2.6/atom/service.py", line 165, in
request
   content_length = CalculateDataLength(data)
 File "/usr/lib/pymodules/python2.6/atom/service.py", line 721, in
CalculateDataLength
   return len(str(data))
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 333, in
__str__
   return self.ToString()
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 330, in
ToString
   return ElementTree.tostring(self._ToElementTree(),
encoding=string_encoding)
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 325, in
_ToElementTree
   self._AddMembersToElementTree(new_tree)
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 287, in
_AddMembersToElementTree
   member._BecomeChildElement(tree)
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 313, in
_BecomeChildElement
   self._AddMembersToElementTree(new_child)
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 298, in
_AddMembersToElementTree
   ExtensionContainer._AddMembersToElementTree(self, tree)
 File "/usr/lib/pymodules/python2.6/atom/__init__.py", line 184, in
_AddMembersToElementTree
   tree.text = self.text.decode(MEMBER_STRING_ENCODING)
AttributeError: 'list' object has no attribute 'decode'
arun@arun-desktop:~/Desktop$

--
You received this message because you are subscribed to the Google Groups "Blogger Developer Group" group.
To post to this group, send email to bloggerdev@googlegroups.com.
To unsubscribe from this group, send email to bloggerdev+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bloggerdev?hl=en.




--

Brett Morgan

Developer Programs Engineer, Blogger

--
You received this message because you are subscribed to the Google Groups "Blogger Developer Group" group.
To post to this group, send email to bloggerdev@googlegroups.com.
To unsubscribe from this group, send email to bloggerdev+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bloggerdev?hl=en.

No comments:

Post a Comment