emacs-blogger / blogger_bridge.py

from blogger import Blogger
from Pymacs import lisp,Let
import os
import filters
import picasa
interactions = {}

class LispInteractive(object):
    def __init__(self):
        self.authenticated = False

    def authenticate(self):
        '''Authenticate the user and return True if succeed else False
        '''
        
        username = lisp["blogger-username"].value()
        password = lisp["blogger-password"].value()
        
        try:
            self.blogger = Blogger(username, password)
            self.authenticated = True
        except:
            return False
        return True
    
    def refresh(self):
        '''refresh the cached list of blogs and entries
        '''
        
        self.blogs = list(self.blogger.get_blogs())

    @property
    def blog(self):
        if hasattr(self,"_blog"):
            return self._blog
        else:
            lisp.error("Current Blog not selected")
    
    def set_blog(self, blog_id):
        self._blog = self.get_blog(blog_id)

    blog.setter(set_blog)

    
    def get_blogs(self):
        return [[blog.id, blog.name] for blog in self.blogs]    
    
    def get_blog_id(self,name):
        '''get a blog by name
        '''
        try:
            return next(blog.id for blog in self.blogs if blog.name == name)
        except StopIteration:
            lisp.error("Blog name '%s' not correct"%name)
    
    def get_entries(self):
        '''get entries of the current blog selected (attr .blog)
        '''
        
        blog = self.blog
        
        entries = blog.get_entries()
        return [[entry.id ,entry.title, entry.updated.strftime("%d/%m/%Y"), entry.category] \
                    for entry in entries]
    
    def get_blog_names(self):
        '''return a list of the blog names
        '''
        
        return [bl.name for bl in self.blogs]

    def get_blog(self, blog_id):
        return next(blog for blog in self.blogs if blog.id == blog_id)
    
    def get_a_blog(self,blog_id=None):
        blog = self.blogs[0] if blog_id == None else self.get_blog(blog_id)
        return blog

    def post(self, title, content):
        '''Post this article to the current blog,
        return the article id
        '''
        entry = self.blog.publish_post(title, content)
        return entry.id
    
    def update_post(self, post_id, title=None, content=None):
        blog = self.blog
        blog.update_post(post_id, title, content)
    
    def delete_post(self, post_id, blog_id = None):
        blog = self.blog
        blog.delete_post(post_id)

    def upload_image(self, filename):
        c = picasa.Client()
        username = lisp["blogger-username"].value()
        password = lisp["blogger-password"].value()
        c.login(username, password)
        
        return c.add_photo(os.path.expanduser(filename))
    def tag_post(self, post_id, tags):
        blog = self.blog
        blog.add_tags(str(post_id), tags)
li = LispInteractive()

# Exposing functions
authenticate = li.authenticate
refresh = li.refresh

get_entries = li.get_entries
get_blog_names = li.get_blog_names
get_blog_id = li.get_blog_id
set_current_blog_by_name = lambda name: li.set_blog(li.get_blog_id(name))

post = li.post
rst2blogger = filters.pipe(filters.rst2html,
                           filters.extract_body,
                           filters.degrade_sections,
                           filters.remove_space_between_tags)
delete_post = li.delete_post
update_post = li.update_post
tag_post = li.tag_post

upload_image = li.upload_image

# Interactive functions
interactions = {}
interactions[upload_image] = "fSelect image to upload: "

# Local Variables:
# pymacs-auto-reload: t
# End:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.