]> git.parisson.com Git - tools.git/commitdiff
Using python API
authoryomguy <yomguy@457c0346-1240-4656-8a5a-9edca8063506>
Wed, 30 Jul 2008 12:39:33 +0000 (12:39 +0000)
committeryomguy <yomguy@457c0346-1240-4656-8a5a-9edca8063506>
Wed, 30 Jul 2008 12:39:33 +0000 (12:39 +0000)
git-svn-id: http://svn.parisson.org/svn/tools/trunk@44 457c0346-1240-4656-8a5a-9edca8063506

various/webthumb.py [new file with mode: 0644]
various/webthumb_list.py

diff --git a/various/webthumb.py b/various/webthumb.py
new file mode 100644 (file)
index 0000000..c90de01
--- /dev/null
@@ -0,0 +1,94 @@
+"""Python interface to Webthumb API (see http://bluga.net/webthumb/)
+
+By Ross Poulton - www.rossp.org
+
+License: Use this how you like, just don't claim it as your own because
+         that isn't cool. I'm not responsible for what this script does.
+
+Usage: Define WEBTHUMB_APIKEY with your API key, as per the above URL.
+
+Then, just call get_thumbnail(url, output_path). It will return true on
+success, false on anything else.
+
+An optional third parameter can be passed for the image size.
+"""
+
+import time
+import os
+import httplib
+
+import xml.dom.minidom
+from xml.dom.minidom import Node
+
+WEBTHUMB_APIKEY='e1716f8b48a6b9f4b27fa9d06fbc8579'
+
+WEBTHUMB_HOST='webthumb.bluga.net'
+WEBTHUMB_URI='/api.php'
+
+VALID_SIZES = (
+    'small',
+    'medium',
+    'medium2',
+    'large',
+)
+
+def get_thumbnail(url, output_path, size='medium2'):
+    if size not in VALID_SIZES:
+        return False
+
+    request = """
+<webthumb>
+    <apikey>%s</apikey>
+    <request>
+        <url>%s</url>
+    </request>
+</webthumb>
+    """ % (WEBTHUMB_APIKEY, url)
+
+    h = httplib.HTTPConnection(WEBTHUMB_HOST)
+    h.request("GET", WEBTHUMB_URI, request)
+    response = h.getresponse()
+
+    type = response.getheader('Content-Type', 'text/plain')
+    body = response.read()
+    h.close()
+    if type == 'text/xml':
+        # This is defined as 'success' by the API. text/plain is failure.
+        doc = xml.dom.minidom.parseString(body)
+
+        for node in doc.getElementsByTagName("job"):
+            wait = node.getAttribute('estimate')
+            key = ""
+            for node2 in node.childNodes:
+                if node2.nodeType == Node.TEXT_NODE:
+                    key = node2.data
+
+        # We're given an approx time by the webthumb server,
+        # we shouldn't request the thumbnail again within this
+        # time.
+        time.sleep(int(wait))
+
+        request = """
+    <webthumb>
+        <apikey>%s</apikey>
+        <fetch>
+            <job>%s</job>
+            <size>%s</size>
+        </fetch>
+    </webthumb>
+        """ % (WEBTHUMB_APIKEY, key, size)
+
+        h = httplib.HTTPConnection(WEBTHUMB_HOST)
+        h.request("GET", WEBTHUMB_URI, request)
+        response = h.getresponse()
+        try:
+            os.unlink(output_path)
+        except:
+            pass
+        img = file(output_path, "wb")
+        img.write(response.read())
+        img.close()
+        h.close()
+        return True
+    else:
+        return False
index ed6716839d7f6901da73705f51a32ef97d3b6ac4..01efe5a8d5aab3908a761475b681fe8b95b913d7 100755 (executable)
@@ -5,6 +5,7 @@
 
 import os
 import sys
+from webthumb import get_thumbnail
 
 img_dir = '/var/www/img/webthumbs'
 site_list_file = '/var/www/img/webthumbs/webthumb_list.txt'
@@ -13,10 +14,11 @@ def main(site_list_file, img_dir):
     site_list = open(site_list_file,'r')
     for site in site_list.readlines():
         site = site[0:len(site)-1]
-        command = 'webthumb http://'+ site +' | pnmscale -xysize 650 400 | ' + \
-                  'pnmtopng | convert -crop 510x275+5+60 - ' + img_dir + os.sep + site +'.png'
-        print command
-        os.system(command)
+        get_thumbnail('http://'+site, img_dir+os.sep+site+'.png','large')
+        #command = 'webthumb http://'+ site +' | pnmscale -xysize 650 400 | ' + \
+        #          'pnmtopng | convert -crop 510x275+5+60 - ' + img_dir + os.sep + site +'.png'
+        #print command
+        #os.system(command)
     site_list.close()
     print 'Webthumbs created !'