Friday, October 6, 2017

Using XML file to restore posts to Blogger - by Python ElementTree API

Using XML file to restore posts to Blogger - by Python ElementTree API

Backup/Restore function of Blogger

The Blogger backup file is a xml file.
Please refer to Blogger Developer’s Guide for more information. The following is an example of a feed for a blog with only one post. In particular, a real Blogger feed contains actual IDs and URLs.
<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css"
  type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'
    gd:etag='W/"D08FQn8-eip7ImA9WxZbFEw."'>
  <id>tag:blogger.com,1999:blog-blogID</id>
  <updated>2008-04-17T00:03:33.152-07:00</updated>
  <title>Lizzy's Diary</title>
  <subtitle type='html'></subtitle>
  <link rel='http://schemas.google.com/g/2005#feed'
    type='application/atom+xml'
    href='http://blogName.blogspot.com/feeds/posts/default' />
  <link rel='self' type='application/atom+xml'
    href='http://www.blogger.com/feeds/blogID/posts/default' />
  <link rel='alternate' type='text/html'
    href='http://blogName.blogspot.com/' />
  <author>
    <name>Elizabeth Bennet</name>
    <uri>http://www.blogger.com/profile/profileID</uri>
    <email>noreply@blogger.com</email>
  </author>
  <generator version='7.00'
    uri='http://www2.blogger.com'>Blogger</generator>
  <entry gd:etag='W/"D0YHRn84eip7ImA9WxZUFk8."'>
    <id>tag:blogger.com,1999:blog-blogID.post-postID</id>
    <published>2008-04-07T20:25:00.005-07:00</published>
    <updated>2008-04-07T20:25:37.132-07:00</updated>
    <title>Quite disagreeable</title>
    <content type='html'>&lt;p&gt;I met Mr. Bingley's friend Mr. Darcy
      this evening. I found him quite disagreeable.&lt;/p&gt;</content>
    <link rel='edit' type='application/atom+xml'
      href='http://www.blogger.com/feeds/blogID/posts/default/postID' />
    <link rel='self' type='application/atom+xml'
      href='http://www.blogger.com/feeds/blogID/posts/default/postID' />
    <link rel='alternate' type='text/html'
      href='http://blogName.blogspot.com/2008/04/quite-disagreeable.html' />
    <author>
      <name>Elizabeth Bennet</name>
      <uri>http://www.blogger.com/profile/profileID</uri>
      <email>noreply@blogger.com</email>
    </author>
  </entry>
</feed>
Reference: Blogger APIs Client Library for Python

Using ElementTree to parse and insert posts to backup xml

ElementTree is a Python API for parsing and creating XML data. To utilize it, just include the following line in the program.
from lxml import etree
or
from lxml import etree as ET

Loading an xml file as an template

Takes an xml file as input. Outputs ElementTree and element.
def load_xml_template(self, name):
    parser = ET.XMLParser(encoding='utf-8')
    tree = ET.parse(name, parser)
    root = tree.getroot()
    return tree, root

Output to xml file using ‘Find’ function in ElementTree

def output_to_xml(self, post_list):
  # Change and write the new xml
  tree, root = self.load_xml_template('template.xml')

  entry = root.find(self.prepend_ns('entry'))

  entry.find(self.prepend_ns('id')).text        = post_list[0][1]
  entry.find(self.prepend_ns('published')).text = post_list[0][3]
  entry.find(self.prepend_ns('updated')).text   = post_list[0][3]
  entry.find(self.prepend_ns('title')).text     = post_list[0][2]
  entry.find(self.prepend_ns('content')).text   = post_list[0][4]

  # Ignore the first one
  for post in post_list[1:]:
    entry2 = copy.deepcopy(entry)
    entry2.find(self.prepend_ns('id')).text        = post[1]
    entry2.find(self.prepend_ns('published')).text = post[3]
    entry2.find(self.prepend_ns('updated')).text   = post[3]
    entry2.find(self.prepend_ns('title')).text     = post[2]
    entry2.find(self.prepend_ns('content')).text   = post[4]
    root.append(entry2)

  global xml_filename
  tree.write(xml_filename, encoding='utf-8', xml_declaration=True)

  self.log('Saved file %s' % xml_filename)

Tags with Namespace declared in ElementTree

Since the tags searching for are declared within a namespace, hence: “http://www.w3.org/2005/Atom” , we have to specify that namespace when searching for those tags. In order to simply the process, a function prepend_ns()is created.
def prepend_ns(self, s):
    return '{http://www.w3.org/2005/Atom}' + s

Using ISO datetime

Blogger uses ISO datetime format. Here is the transformation function.
def iso_datetime(datetime_string):
  real_date  = datetime.strptime(datetime_string, '%d, %b %Y %H:%M')
  # ISO8601 '2017-14-07T20:25:00.005-07:00'
  iso_date = real_date.strftime('%Y-%d-%mT%H:%M:%S.000-08:00')
  return real_date, iso_date

61 comments:

  1. Excellent Post. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
    Ruby on Rails Online Training

    ReplyDelete
    Replies
    1. Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download Now

      >>>>> Download Full

      Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download LINK

      >>>>> Download Now

      Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download Full

      >>>>> Download LINK 21

      Delete
  2. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective.

    Python Training Institute in South Delhi

    ReplyDelete
  3. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective.

    Python Training Institute in Delhi

    ReplyDelete
  4. Your content is really good thanks for sharing this post thank you if anyone looking for Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses

    ReplyDelete
  5. thank you for sharing this post its really awesome apart from that if anyone looking for e accounting institute in delhi so Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/Tally/e-accounting-training-course

    ReplyDelete
  6. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

    No-1 Python Training Institute in Delhi with Placement Assistance

    ReplyDelete
  7. A big thanks for sharing this post by the way if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in hyderabad, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

    ReplyDelete
  8. Great Info, Your blog is very informative and interesting, your all post are amazing, keep sharing more interesting topics. Thanks for the blog. It really helps me a lot.

    Innovative Approaches to Improve Your Python Training Course
    Reasons Why You Should Learn Python Training Course

    ReplyDelete
  9. I want to leave a little comment to support and wish you the best of luck. We wish you the best of luck in all your blogging endeavors. Otherwise if any One Want to Learn Complete Python Training Course - No Coding Experience Required So Contact Us.

    Complete Python Training Course - No Coding Experience Required

    ReplyDelete
  10. Thanks for sharing this amazing post this is the content i really looking for, it's very helpful i hope you will continue your blogging anyway if anyone looking for Advance excel training institute in delhi contact us +91-9311002620 visit-https://www.htsindia.com/Courses/business-analytics/adv-excel-training-course

    ReplyDelete

  11. Pretty great post. I simply stumbled upon your blog and wanted to mention that I have really loved surfing around your blog posts. Great set of tips from the master himself. Excellent ideas. Thanks for Awesome tips Keep it
    restoro-crack
    norton-antivirus-crack
    allavsoft-video-downloader-converter-crack
    wondershare-pdfelement-pro-crack
    cubase-pro-crack
    ummy-video-downloader-crack



    ReplyDelete
  12. This site have particular software articles which emits an impression of being a significant and significant for you individual, able software installation.This is the spot you can get helps for any software installation, usage and cracked.
    crackandpatch.com

    ReplyDelete
  13. Amazing blog! I really like the way you explained such information about this post with us. And blog is really helpful for us this website
    restoro-crack

    ReplyDelete

  14. Really Appreciable Article, Honestly Said The Thing Actually I liked The most is the step-by-step explanation of everything needed to be known for a blogger or webmaster to comment, I am going show this to my other blogger friends too.
    restoro-crack
    autodesk-powermill-crack
    deep-freeze-crack
    little-snitch-crack
    sidify-music-converter-crack
    sony-vegas-pro-crack
    sparkol-videoscribe-pro-crack
    avocode-crack
    beecut-crack
    ninjagram-crack

    ReplyDelete
  15. Awesome article! You are providing us very valid information. This is worth reading. Keep sharing more such articles.
    Data Science and Finance
    Data Science Applications in Finance

    ReplyDelete
  16. Pretty great post. I simply stumbled upon your blog and wanted to mention that I have really loved surfing around your blog posts. Great set of tips from the master himself. Excellent ideas. Thanks for Awesome tips Keep it
    gilisoft-secure-disk-creator-crack

    ReplyDelete

  17. Pretty great post. I simply stumbled upon your blog and wanted to mention that I have really loved surfing around your blog posts. Great set of tips from the master himself. Excellent ideas. Thanks for Awesome tips Keep it
    wonders-are-pdfelement-crack

    ReplyDelete
  18. PC Software Download
    You make it look very easy with your presentation, but I think this is important to Be something that I think I would never understand
    It seems very complex and extremely broad to me. I look forward to your next post,
    FL Studio Crack
    Exact Audio Copy Crack
    Excel Merger Pro Crack
    KLS Backup Professional Crack
    Sidify Music Converter Crack
    Adobe XD CC Crack
    Anthemion Jutoh Crack

    ReplyDelete
  19. Wow, amazing block structure! How long
    Have you written a blog before? Working on a blog seems easy.
    Microsoft Office Crack
    NetLimiter Pro Crack
    Andy Android Emulator
    The overview of your website is pretty good, not to mention what it does.

    ReplyDelete
  20. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
    Very interesting blog.
    cracksfix.org
    Restoro Crack

    ReplyDelete
  21. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also how to send large files

    ReplyDelete
  22. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. crackbay.org I hope to have many more entries or so from you.
    Very interesting blog.
    FL Studio Crack

    ReplyDelete
  23. Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download Now

    >>>>> Download Full

    Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download LINK

    >>>>> Download Now

    Charles'S Notes For Code: Using Xml File To Restore Posts To Blogger - By Python Elementtree Api >>>>> Download Full

    >>>>> Download LINK 3F

    ReplyDelete
  24. Hello, this post gives very interesting information about off-season camping, really I like this information which is so much beneficial to us, keep sharing such kind of information, Thanks for sharing.
    vMix Crack

    ReplyDelete
  25. Download Software for PC & Mac
    You make it look very easy with your presentation, but I think this is important to Be something that I think I would never understand
    It seems very complex and extremely broad to me. I look forward to your next post,
    PRTG Network Monitor Crack
    Mullvad VPN Crack
    SysTools SSD Data Recovery Crack
    FL Studio Crack
    ASTER Crack
    Far Cry 5 Crack
    ARCHICAD Crack
    Exiland Backup Professional Crack

    ReplyDelete
  26. Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet! android emulators
    restoro-crack
    dxo-photolab-crack
    pixologic-zbrush-crack
    corel-draw-x7-crack
    imyfone-lockwiper-crack

    ReplyDelete

  27. After looking through a few blog articles on your website,
    we sincerely appreciate the way you blogged.
    We've added it to our list of bookmarked web pages and will be checking back in the near
    future. Please also visit my website and tell us what you think.
    gilisoft-secure-disk-creator-crack
    endnote-crack
    windows-10-pro-activator
    windows-11-download-iso-crack

    ReplyDelete
  28. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
    Very interesting blog.
    crackpur.info
    Python Crack

    ReplyDelete
  29. Download Software for PC & Mac
    You make it look very easy with your presentation, but I think this is important to Be something that I think I would never understand
    It seems very complex and extremely broad to me. I look forward to your next post,
    Mocha Pro Crack
    BeeCut Crack
    Blue-Cloner Crack
    Sublime Text Crack
    Video Copilot Element Crack
    PyCharm Pro Crack
    Resharper Crack
    Agisoft Metashape Pro Crack

    ReplyDelete
  30. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot.
    Little Snitch Crack
    UTorrent Pro Crack
    Stereo Tool Crack

    ReplyDelete
  31. Amazing blog! I really like the way you explained such information about this post to us. And a blog is really helpful for us this website.
    ProgDVB Crack
    NordVPN Crack
    Quick Heal Total Security Crack
    IVT BlueSoleil Crack
    SmadAV Pro Crack
    Klevgrand Complete Bundle Crack

    ReplyDelete
  32. I'm really impressed with your writing skills, as smart as the structure of your


    Latest Software Free Download



    weblog. Is this a paid topic



    Mediacoder crack



    do you change it yourself? However, stopping by with great quality writing, it's hard to see any good blog today.



    Push video wallpaper -crack


    Iobit start menu -crack


    Pinnacle pro crack


    Betternet vpn pro crack

    ReplyDelete
  33. It is very informative. Very easy to understand. Great work team. Keep me updated for all such articles
    Your work is great it provides me great knowledge.
    Restoro Crack
    Bartende Crack
    UAD Ultimate 10 Bundle Crack
    Nitro Pro Crack
    Typing Master Pro Crack
    WaveLab Pro Crack

    ReplyDelete
  34. Absolute AV Antivirus 2022 Crack is the world’s most helpful antivirus that offers ongoing security from different outer as well as inside dangers,https://crackweblinks.com/total-av-antivirus-crack-download/

    ReplyDelete
  35. It is very informative. Very easy to understand. Great work team. Keep me updated for all such articles
    Java Full Stack in KPHB

    ReplyDelete