Commits

luoboiqingcai  committed e1f4ba9

bug fix

  • Participants
  • Parent commits 94dcb18

Comments (0)

Files changed (2)

File downloadlib.py

 
 duxiu_source_list = ["www2.zhengzhifl.cn","www.zirankxzl.cn","www.zhexuezj.cn","www.junshilei.cn",]
 
-DEBUG = True
+DEBUG = False
 TEM_FILE = "remote_webpage_content.html"
 
 class DownloadLib(object):
         if DEBUG:
             self.log.setLevel(logging.DEBUG)
         else:
-            self.log.setLevel(logging.INFO)                
+            self.log.setLevel(logging.INFO)
         if logfile:
             self.log.addHandler(logging.FileHandler(self.prex+logfile,mode='w'))
         else:
         try:
             img = urllib2.urlopen(s)
         except urllib2.URLError,e:
-            self.log.warn('%s'%e)
-            self.log.warn('firest time download failed: %s'%e)
+            self.log.warn('first time download failed: %s'%e)
             time.sleep(10)
             try:
                 img = urllib2.urlopen(s)
                 self.log.warn('try second time failed: %s'%e)
                 self.log.info("%s wasn't downloaded."%s)
             else:
+                self.log.info('second time succeed.')
                 return img
         else:
             return img
             content = self.get_content(url,remotep=False)
             for line in content:
                 imgurls = re.findall(p,line)
-        return [{'url':self.img_url_prex+imgurl,'localname':self.prex+self.get_localname(imgurl)} for imgurl in imgurls]
+        return [{'url':self.img_url_prex+imgurl,'localname':os.path.join(self.prex,self.get_localname(imgurl))} for imgurl in imgurls]
 
     def downloadit(self,
                    from_remote,
             except Exception, e:
                 raise e
             else:
-                self.log.debug("%s has been download and stored locally."%ul['localname'])
+                self.log.info("%s has been download and stored locally."%ul['localname'])
         self.log.info("%d images are downloaded."%count)
         return count
 
         with open(pair['localname'],'wb') as f:
             f.write(img.read())
     else:
-        sys.stderr.write('img empty')
+        log_to_stderr('img empty')
 
 multidownloadchaoxing = multidownloadduxiu
 
             usage()
             sys.exit(0)
         elif o == '--prex':
-            if a[-1] != '/':
-                prex = a + '/'
+            if a[-1] == '/' or a[-1] == '\\':
+                prex = prex[:-1]
         elif o == '--logfile':
             logfile = a
         elif o == '--local':
         instance = DownloadChaoXing(lnp=lnp,pattern=pattern_for_chaoxing,img_url_prex=chaoxing_img_url_prex,prex=prex,logfile=logfile)
         if resume:
             if prex[-1] == '/' or prex[-1] == '\\':
-                print('prex not end with slash')
+                print('prex should not end with slash')
                 exit(1)
             instance(from_remote,arg,sp=resume_point(os.path.join(prex,logfile)))
         if sp and ep:
 """
 
 from Tkinter import *
-from downloadduxiu import (Downloadduxiu,Downloadchaoxing)
+from downloadlib import (DownloadDuXiu,DownloadChaoXing)
 import threading
 import logging
 import os
                 lb['text'] = '文献传递查看地址'
             else:
                 lb['text'] = '超星全文下载后的本地网页地址'
-        Radiobutton(self,text="读秀",variable=self.type_,value=0,command=radio_change).grid(row=1,column=1)
-        Radiobutton(self,text="超星",variable=self.type_,value=1,command=radio_change).grid(row=1,column=2)
-        Entry(self,textvariable=self.url).grid(row=2,column=2)
+        self.dx = Radiobutton(self,text="读秀",variable=self.type_,value=0,command=radio_change)
+        self.dx.grid(row=1,column=1)
+        
+        self.cx = Radiobutton(self,text="超星",variable=self.type_,value=1,command=radio_change)
+        self.cx.grid(row=1,column=2)
+        
+        self.url_entry = Entry(self,textvariable=self.url)
+        self.url_entry.grid(row=2,column=2)
+        
         Label(self,text='相对程序目录的图片保存目录:').grid(row=3,column=1)
-        Entry(self,textvariable=self.path_to_save).grid(row=3,column=2)
+        
+        self.path_entry = Entry(self,textvariable=self.path_to_save)
+        self.path_entry.grid(row=3,column=2)
+        
         self.CANCEL = Button(self,text='Cancel')
         def q():
             self.quit()
         prex = self.path_to_save.get()
         if not prex:
             prex = 'xiazai'
-        prex += os.sep
         logfile = 'logfile'
+
+
         class StatusVar(StringIO):
             def __init__(self,statusarea):
                 StringIO.__init__(self)
                 self.closed = True
         if self.type_.get() == 0: # download duxiu
             def work():
+                self.OK['state'] = DISABLED
+                self.dx['state'] = DISABLED
+                self.cx['state'] = DISABLED
+                self.url_entry['state'] = DISABLED
+                self.path_entry['state'] = DISABLED
                 count = threading.active_count()
                 if count > 2:
                     return
                 try:
-                    duxiu = Downloadduxiu(lnp=lnp,pattern=pattern_for_duxiu_r,img_url_prex=img_url_prex,prex=prex,logfile=logfile)
+                    duxiu = DownloadDuXiu(lnp=lnp,pattern=pattern_for_duxiu_r,img_url_prex=img_url_prex,prex=prex,logfile=logfile)
                     duxiu.log.addHandler(logging.StreamHandler(StatusVar(self.statusarea)))
-                    duxiu.downloadduxiu(True,self.url.get())
+                    duxiu(True,self.url.get())
                 except Exception,e:
                     self.statusarea.insert(END,"%s"%e)
                 else:
+                    self.CANCEL.config(status = NORMAL)
                     self.url.set('Finished.')
                     self.statusarea.insert(END,"下载完成")
+                    self.statusarea.update()
+                    self.OK.config(status = NORMAL)
+                finally:
+                    return
         else: # download chaoxing
             print "download chaoxing.."
             def work():
                 if count > 2: # run in another thread to avoid interrupt the guid thread,and prevent user clicking download multiple times.
                     return
                 try:
-                    chaoxing = Downloadchaoxing(lnp=lnp,pattern=pattern_for_chaoxing,img_url_prex=chaoxing_img_url_prex,prex=prex,logfile=logfile)
+                    chaoxing = DownloadChaoXing(lnp=lnp,pattern=pattern_for_chaoxing,img_url_prex=chaoxing_img_url_prex,prex=prex,logfile=logfile)
                     chaoxing.log.addHandler(logging.StreamHandler(StatusVar(self.statusarea)))
-                    chaoxing.downloadchaoxing(False,self.url.get())
+                    chaoxing(False,self.url.get())
                 except Exception,e:
                     self.statusarea.insert(END,"%s"%e)
-                finally:
+                else:
                     self.url.set('Finished.')
                     self.statusarea.insert(END,"下载完成")
+                    self.OK['status'] = ACTIVE
+                finally:
+                    return
         th = threading.Thread(target=work)
         th.start()