From 896a3faa065a9f038ce8477e2a542bbb650b506e Mon Sep 17 00:00:00 2001 From: yomguy Date: Wed, 30 Jul 2008 12:39:33 +0000 Subject: [PATCH] Using python API git-svn-id: http://svn.parisson.org/svn/tools/trunk@44 457c0346-1240-4656-8a5a-9edca8063506 --- various/webthumb.py | 94 ++++++++++++++++++++++++++++++++++++++++ various/webthumb_list.py | 10 +++-- 2 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 various/webthumb.py diff --git a/various/webthumb.py b/various/webthumb.py new file mode 100644 index 0000000..c90de01 --- /dev/null +++ b/various/webthumb.py @@ -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 = """ + + %s + + %s + + + """ % (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 = """ + + %s + + %s + %s + + + """ % (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 diff --git a/various/webthumb_list.py b/various/webthumb_list.py index ed67168..01efe5a 100755 --- a/various/webthumb_list.py +++ b/various/webthumb_list.py @@ -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 !' -- 2.39.5