View Single Post
Old 12-16-2015, 01:33 PM   #8
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 8,893
Karma: 6120478
Join Date: Nov 2009
Device: many
Hi,

Here is a little snippet of code to give the general idea. I put this together quickly to give you some idea of how to go about this. I am sure a simpler/tighter version is possible but this works in my testing for both python and python3

Code:
from __future__ import unicode_literals, division, absolute_import, print_function

import sys
import os
from quickparser import QuickXHTMLParser

try:
    import urllib.request as url_request
except ImportError:
    import urllib2 as url_request


def check_for_updates(site_url, plugin_path):
    plugin_name = None
    version = None
    ps = QuickXHTMLParser()

    try:
        # get the most up to date release version info
        # looks for the name of the current Mobileread Attachment
        # You could easily add current version info anyplace on the site
        req = url_request.Request(site_url)
        response = url_request.urlopen(req)
        the_page = response.read()

        # you could replace all of the parsing with simple regular 
        # expressions here as well
        ps.setContent(the_page)
        get_plugin_name = False
        for (text, tagprefix, tname, ttype, tattr) in ps.parse_iter():
            if text is not None:
                if get_plugin_name:
                    plugin_name = text
                    break
            elif tname == "a" and "href" in tattr:
                href = tattr["href"]
                if href.startswith("attachment.php?"):
                    get_plugin_name = True;
    except:
        pass

    # now get the current version of the installed plugin
    try:
        ppath = os.path.join(plugin_path, "plugin.xml")
        get_version = False
        with open(ppath,'rb') as f:
            data = f.read()
            ps.setContent(data)
            for (text, tagprefix, tname, ttype, tattr) in ps.parse_iter():
                if text is not None:
                    if get_version:
                        version = text
                        break
                elif tname == "version"and ttype == "begin":
                    get_version = True
    except:
        pass
    return plugin_name, version


SITE_URL = "https://www.mobileread.com/forums/showpost.php?p=2973066&postcount=1"
PLUGIN_PATH = "/Users/kbhend/Desktop"
plugin_name, version = check_for_updates(SITE_URL, PLUGIN_PATH)
print(plugin_name, version)
The path to the plugin directory would have to be properly determined inside the plugin (not my hard coded path here) and if I had bothered to put the current version someplace on the MR website I would not have had to parse to find the attachment name.

Using regular expression would probably make the code shorter. As I said, I just threw this together to illustrate how things could be done in a way that works on both python 2.7 and python 3.4.

After I get more time I will clean this up and add a specific current version info to the first post on the ePub3-itizer MR site to make this even simpler.

KevinH

Last edited by KevinH; 12-16-2015 at 01:46 PM.
KevinH is offline   Reply With Quote