#!/usr/bin/env python
# -*- coding:utf-8 -*-
import cPickle
import os
import hashlib
import threading
from image import load
import Queue
queue = Queue.Queue()
class MutilThread(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
#按照顺序执行,先put所有的的queue然后再逐步处理;
#while queue.qsize() != 0:
while True:
url = self.queue.get()
size = queue.qsize()
print "Thread's Size: %s" % size
SaveImage(url)
self.queue.task_done()
def DownloadImage():
Urllist = []
url_li = getMedicineImage(Urllist)
#先将所有的item,put到queue;
#for url in url_li:
#queue.put(url)
for i in range(4):
t = MutilThread(queue)
t.setDaemon(True)
t.start()
for url in url_li:
queue.put(url)
#size = queue.qsize()
#print "Queue's Size: %s" % size
queue.join()
def openFile(pickfile):
f = open(pickfile,'rb')
dct = cPickle.load(f)
f.close()
return dct
def SaveImage(url):
tail = url.split('.')[-1]
fl = hashlib.md5(url).hexdigest()
dirname = "images/"
f = open(dirname+fl+'.'+tail,'w')
f.write(load(url))
f.close()
def getMedicineImage(li):
pickfile = "yaopin.pkl"
hd = openFile(pickfile)
for trunk in hd['yaopin']:
print trunk
for sub in hd['yaopin'][trunk]:
for medicine in hd['yaopin'][trunk][sub]:
url = medicine['image_url']
if url != "http://img.39.net/ypk/images/spic_bg.gif":
li.append(url)
return li
def main():
# getMedicineImage()
DownloadImage()
if __name__=='__main__':
main()