2014-02-26 23:22:53 +00:00
#!/usr/bin/env python2
2012-03-04 00:29:38 +00:00
# -*- coding: utf-8 -*-
# Copyright (C) 2011-2012 WikiTeam
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-03-04 12:35:02 +00:00
import os
import platform
2012-03-04 18:53:35 +00:00
import random
2012-03-04 12:35:02 +00:00
import re
from Tkinter import *
2012-03-04 00:29:38 +00:00
import ttk
2012-03-04 12:35:02 +00:00
import tkMessageBox
2012-03-05 00:19:20 +00:00
import thread
2012-03-04 12:35:02 +00:00
import time
import urllib
import webbrowser
2012-03-04 00:29:38 +00:00
2012-03-04 12:35:02 +00:00
import dumpgenerator
"""
TODO :
* basic : GUI to download just a wiki
* advanced : batch downloads , upload to Internet Archive or anywhere
"""
2012-03-04 13:09:58 +00:00
wikifarms = {
2012-03-05 12:29:04 +00:00
' gentoo_wikicom ' : ' Gentoo Wiki ' ,
2012-03-04 13:09:58 +00:00
' opensuseorg ' : ' OpenSuSE ' ,
' referatacom ' : ' Referata ' ,
2012-03-05 12:29:04 +00:00
' shoutwikicom ' : ' ShoutWiki ' ,
2012-03-04 13:12:40 +00:00
' Unknown ' : ' Unknown ' ,
2012-03-05 12:29:04 +00:00
' wikanda ' : ' Wikanda ' ,
' wikifur ' : ' WikiFur ' ,
2012-03-05 14:48:44 +00:00
' wikimedia ' : ' Wikimedia ' ,
2012-03-04 13:09:58 +00:00
' wikitravelorg ' : ' WikiTravel ' ,
2012-03-05 12:29:04 +00:00
' wikkii ' : ' Wikkii ' ,
2012-03-04 13:09:58 +00:00
}
2012-03-04 12:35:02 +00:00
NAME = ' WikiTeam tools '
VERSION = ' 0.1 '
HOMEPAGE = ' https://code.google.com/p/wikiteam/ '
LINUX = platform . system ( ) . lower ( ) == ' linux '
PATH = os . path . dirname ( __file__ )
if PATH : os . chdir ( PATH )
class App :
def __init__ ( self , master ) :
self . master = master
2012-03-04 13:31:11 +00:00
self . dumps = [ ]
2012-03-04 18:53:35 +00:00
self . downloadpath = ' downloads '
2012-03-05 14:48:44 +00:00
self . block = False
2012-03-04 13:31:11 +00:00
2012-03-04 12:35:02 +00:00
# interface elements
#progressbar
#self.value = 0
#self.progressbar = ttk.Progressbar(self.master, orient=HORIZONTAL, value=self.value, mode='determinate')
#self.progressbar.grid(row=0, column=0, columnspan=1, sticky=W+E)
#self.run()
#description
2012-03-05 00:25:14 +00:00
self . desc = Label ( self . master , text = " " , anchor = W , font = ( " Arial " , 10 ) )
2012-03-04 12:35:02 +00:00
self . desc . grid ( row = 0 , column = 0 , columnspan = 1 )
2012-03-04 21:04:41 +00:00
#self.footer = Label(self.master, text="%s (version %s). This program is free software (GPL v3 or higher)" % (NAME, VERSION), anchor=W, justify=LEFT, font=("Arial", 10))
#self.footer.grid(row=2, column=0, columnspan=1)
2012-03-04 12:35:02 +00:00
2012-03-04 14:35:36 +00:00
#begin tabs
2012-03-04 12:35:02 +00:00
self . notebook = ttk . Notebook ( self . master )
self . notebook . grid ( row = 1 , column = 0 , columnspan = 1 , sticky = W + E + N + S )
self . frame1 = ttk . Frame ( self . master )
self . notebook . add ( self . frame1 , text = ' Dump generator ' )
self . frame2 = ttk . Frame ( self . master )
self . notebook . add ( self . frame2 , text = ' Downloader ' )
self . frame3 = ttk . Frame ( self . master )
self . notebook . add ( self . frame3 , text = ' Uploader ' )
2012-03-04 14:35:36 +00:00
2012-03-04 15:05:21 +00:00
#dump generator tab (1)
2012-03-05 14:04:23 +00:00
self . labelframe11 = LabelFrame ( self . frame1 , text = " Single download " )
self . labelframe11 . grid ( row = 0 , column = 0 )
self . labelframe12 = LabelFrame ( self . frame1 , text = " Batch download " )
self . labelframe12 . grid ( row = 1 , column = 0 )
#single download labelframe
self . label11 = Label ( self . labelframe11 , text = " Wiki URL: " )
2012-03-04 17:38:44 +00:00
self . label11 . grid ( row = 0 , column = 0 )
2012-03-05 14:04:23 +00:00
self . entry11 = Entry ( self . labelframe11 , width = 40 )
2012-03-05 10:52:18 +00:00
self . entry11 . grid ( row = 0 , column = 1 )
2012-03-05 12:57:38 +00:00
self . entry11 . bind ( ' <Return> ' , ( lambda event : self . checkURL ( ) ) )
2012-03-05 14:04:23 +00:00
self . optionmenu11var = StringVar ( self . labelframe11 )
2012-03-05 12:31:06 +00:00
self . optionmenu11var . set ( " api.php " )
2012-03-05 14:04:23 +00:00
self . optionmenu11 = OptionMenu ( self . labelframe11 , self . optionmenu11var , self . optionmenu11var . get ( ) , " index.php " )
2012-03-05 11:08:03 +00:00
self . optionmenu11 . grid ( row = 0 , column = 2 )
2012-03-05 14:04:23 +00:00
self . button11 = Button ( self . labelframe11 , text = " Check " , command = lambda : thread . start_new_thread ( self . checkURL , ( ) ) , width = 5 )
2012-03-05 11:08:03 +00:00
self . button11 . grid ( row = 0 , column = 3 )
2012-03-05 14:04:23 +00:00
#batch download labelframe
self . label12 = Label ( self . labelframe12 , text = " Wiki URLs: " )
self . label12 . grid ( row = 0 , column = 0 )
self . text11 = Text ( self . labelframe12 , width = 70 , height = 20 )
self . text11 . grid ( row = 0 , column = 1 )
2012-03-04 15:05:21 +00:00
#downloader tab (2)
2012-03-04 19:48:24 +00:00
self . label25var = StringVar ( self . frame2 )
2012-03-04 20:00:21 +00:00
self . label25var . set ( " Available dumps: 0 (0.0 MB) " )
2012-03-04 21:04:41 +00:00
self . label25 = Label ( self . frame2 , textvariable = self . label25var , width = 27 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label25 . grid ( row = 0 , column = 0 , columnspan = 2 )
2012-03-04 19:48:24 +00:00
self . label26var = StringVar ( self . frame2 )
2012-03-04 20:00:21 +00:00
self . label26var . set ( " Downloaded: 0 (0.0 MB) " )
2012-03-04 21:04:41 +00:00
self . label26 = Label ( self . frame2 , textvariable = self . label26var , background = ' lightgreen ' , width = 27 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label26 . grid ( row = 0 , column = 2 , columnspan = 2 )
2012-03-04 19:48:24 +00:00
self . label27var = StringVar ( self . frame2 )
2012-03-05 10:33:56 +00:00
self . label27var . set ( " Not downloaded: 0 (0.0 MB) " )
2012-03-04 21:04:41 +00:00
self . label27 = Label ( self . frame2 , textvariable = self . label27var , background = ' white ' , width = 27 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label27 . grid ( row = 0 , column = 4 , columnspan = 2 )
2012-03-04 21:04:41 +00:00
self . label21 = Label ( self . frame2 , text = " Filter by wikifarm: " , width = 15 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label21 . grid ( row = 1 , column = 0 )
2012-03-04 15:46:53 +00:00
self . optionmenu21var = StringVar ( self . frame2 )
self . optionmenu21var . set ( " all " )
2012-03-05 14:48:44 +00:00
self . optionmenu21 = OptionMenu ( self . frame2 , self . optionmenu21var , self . optionmenu21var . get ( ) , " Gentoo Wiki " , " OpenSuSE " , " Referata " , " ShoutWiki " , " Unknown " , " Wikanda " , " WikiFur " , " Wikimedia " , " WikiTravel " , " Wikkii " )
2012-03-04 19:08:01 +00:00
self . optionmenu21 . grid ( row = 1 , column = 1 )
2012-03-04 15:05:21 +00:00
2012-03-04 21:04:41 +00:00
self . label22 = Label ( self . frame2 , text = " Filter by size: " , width = 15 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label22 . grid ( row = 1 , column = 2 )
2012-03-04 15:46:53 +00:00
self . optionmenu22var = StringVar ( self . frame2 )
self . optionmenu22var . set ( " all " )
2012-03-05 12:31:06 +00:00
self . optionmenu22 = OptionMenu ( self . frame2 , self . optionmenu22var , self . optionmenu22var . get ( ) , " KB " , " MB " , " GB " , " TB " )
2012-03-04 19:08:01 +00:00
self . optionmenu22 . grid ( row = 1 , column = 3 )
2012-03-04 15:05:21 +00:00
2012-03-04 21:04:41 +00:00
self . label23 = Label ( self . frame2 , text = " Filter by date: " , width = 15 , anchor = W )
2012-03-04 19:08:01 +00:00
self . label23 . grid ( row = 1 , column = 4 )
2012-03-04 15:46:53 +00:00
self . optionmenu23var = StringVar ( self . frame2 )
self . optionmenu23var . set ( " all " )
2012-03-05 12:31:06 +00:00
self . optionmenu23 = OptionMenu ( self . frame2 , self . optionmenu23var , self . optionmenu23var . get ( ) , " 2011 " , " 2012 " )
2012-03-04 19:08:01 +00:00
self . optionmenu23 . grid ( row = 1 , column = 5 )
2012-03-04 15:05:21 +00:00
self . label24 = Label ( self . frame2 , text = " Filter by mirror: " )
2012-03-04 19:08:01 +00:00
self . label24 . grid ( row = 1 , column = 6 )
2012-03-04 15:46:53 +00:00
self . optionmenu24var = StringVar ( self . frame2 )
self . optionmenu24var . set ( " all " )
2012-03-05 12:31:06 +00:00
self . optionmenu24 = OptionMenu ( self . frame2 , self . optionmenu24var , self . optionmenu24var . get ( ) , " Google Code " , " Internet Archive " , " ScottDB " )
2012-03-04 19:08:01 +00:00
self . optionmenu24 . grid ( row = 1 , column = 7 )
2012-03-04 13:31:11 +00:00
2012-03-04 21:04:41 +00:00
self . button23 = Button ( self . frame2 , text = " Filter! " , command = self . filterAvailableDumps , width = 7 )
2012-03-04 19:08:01 +00:00
self . button23 . grid ( row = 1 , column = 8 )
2012-03-04 15:46:53 +00:00
2012-03-04 14:20:44 +00:00
self . treescrollbar = Scrollbar ( self . frame2 )
2012-03-04 19:08:01 +00:00
self . treescrollbar . grid ( row = 2 , column = 9 , sticky = W + E + N + S )
2012-03-04 21:04:41 +00:00
columns = ( ' dump ' , ' wikifarm ' , ' size ' , ' date ' , ' mirror ' , ' status ' )
2012-03-04 17:38:44 +00:00
self . tree = ttk . Treeview ( self . frame2 , height = 20 , columns = columns , show = ' headings ' , yscrollcommand = self . treescrollbar . set )
2012-03-04 14:20:44 +00:00
self . treescrollbar . config ( command = self . tree . yview )
2012-03-05 00:25:14 +00:00
self . tree . column ( ' dump ' , width = 495 , minwidth = 200 , anchor = ' center ' )
2012-03-04 12:35:02 +00:00
self . tree . heading ( ' dump ' , text = ' Dump ' )
self . tree . column ( ' wikifarm ' , width = 100 , minwidth = 100 , anchor = ' center ' )
self . tree . heading ( ' wikifarm ' , text = ' Wikifarm ' )
self . tree . column ( ' size ' , width = 100 , minwidth = 100 , anchor = ' center ' )
self . tree . heading ( ' size ' , text = ' Size ' )
self . tree . column ( ' date ' , width = 100 , minwidth = 100 , anchor = ' center ' )
self . tree . heading ( ' date ' , text = ' Date ' )
2012-03-04 13:09:58 +00:00
self . tree . column ( ' mirror ' , width = 120 , minwidth = 120 , anchor = ' center ' )
self . tree . heading ( ' mirror ' , text = ' Mirror ' )
2012-03-04 21:04:41 +00:00
self . tree . column ( ' status ' , width = 120 , minwidth = 120 , anchor = ' center ' )
self . tree . heading ( ' status ' , text = ' Status ' )
2012-03-04 19:08:01 +00:00
self . tree . grid ( row = 2 , column = 0 , columnspan = 9 , sticky = W + E + N + S )
2012-03-04 17:38:44 +00:00
[ self . tree . heading ( column , text = column , command = lambda : self . treeSortColumn ( column = column , reverse = False ) ) for column in columns ]
2012-03-05 00:50:39 +00:00
#self.tree.bind("<Double-1>", (lambda: thread.start_new_thread(self.downloadDump, ())))
2012-03-04 19:08:01 +00:00
self . tree . tag_configure ( ' downloaded ' , background = ' lightgreen ' )
2012-03-04 18:53:35 +00:00
self . tree . tag_configure ( ' nodownloaded ' , background = ' white ' )
2012-03-05 00:19:20 +00:00
self . button21 = Button ( self . frame2 , text = " Load available dumps " , command = lambda : thread . start_new_thread ( self . loadAvailableDumps , ( ) ) , width = 15 )
2012-03-04 19:08:01 +00:00
self . button21 . grid ( row = 3 , column = 0 )
2012-03-05 00:19:20 +00:00
self . button23 = Button ( self . frame2 , text = " Download selection " , command = lambda : thread . start_new_thread ( self . downloadDump , ( ) ) , width = 15 )
2012-03-04 19:08:01 +00:00
self . button23 . grid ( row = 3 , column = 4 )
2012-03-04 15:46:53 +00:00
self . button22 = Button ( self . frame2 , text = " Clear list " , command = self . deleteAvailableDumps , width = 10 )
2012-03-04 19:08:01 +00:00
self . button22 . grid ( row = 3 , column = 8 , columnspan = 2 )
2012-03-04 14:35:36 +00:00
2012-03-04 15:05:21 +00:00
#uploader tab (3)
2012-03-04 17:38:44 +00:00
self . label31 = Label ( self . frame3 , text = " todo... " )
self . label31 . grid ( row = 0 , column = 0 )
2012-03-04 14:35:36 +00:00
#end tabs
2012-03-04 12:35:02 +00:00
2012-03-04 21:04:41 +00:00
#statusbar
2012-03-05 14:20:22 +00:00
self . status = Label ( self . master , text = " Welcome to WikiTeam tools. What wiki do you want to preserve today? " , bd = 1 , background = ' grey ' , justify = LEFT , relief = SUNKEN )
2012-03-04 21:04:41 +00:00
self . status . grid ( row = 4 , column = 0 , columnspan = 10 , sticky = W + E )
2012-03-04 14:35:36 +00:00
#begin menu
2012-03-04 12:35:02 +00:00
menu = Menu ( self . master )
master . config ( menu = menu )
2012-03-04 14:35:36 +00:00
#file menu
2012-03-04 12:35:02 +00:00
filemenu = Menu ( menu )
menu . add_cascade ( label = " File " , menu = filemenu )
filemenu . add_command ( label = " Preferences " , command = self . callback )
filemenu . add_separator ( )
filemenu . add_command ( label = " Exit " , command = askclose )
2012-03-04 14:35:36 +00:00
#help menu
2012-03-04 12:35:02 +00:00
helpmenu = Menu ( menu )
menu . add_cascade ( label = " Help " , menu = helpmenu )
helpmenu . add_command ( label = " About " , command = self . callback )
helpmenu . add_command ( label = " Help index " , command = self . callback )
helpmenu . add_command ( label = " WikiTeam homepage " , command = lambda : webbrowser . open_new_tab ( HOMEPAGE ) )
2012-03-04 14:35:36 +00:00
#end menu
2012-03-04 19:17:49 +00:00
2012-03-05 15:00:52 +00:00
def blocked ( self ) :
tkMessageBox . showerror ( " Error " , " There is a task in progress. Please, wait. " )
2012-03-05 12:57:38 +00:00
def checkURL ( self ) :
if re . search ( ur " (?im)^https?://[^/]+ \ .[^/]+/ " , self . entry11 . get ( ) ) : #well-constructed URL?, one dot at least, aaaaa.com, but bb.aaaaa.com is allowed too
if self . optionmenu11var . get ( ) == ' api.php ' :
self . msg ( ' Please wait... Checking api.php... ' )
if dumpgenerator . checkAPI ( self . entry11 . get ( ) ) :
self . entry11 . config ( background = ' lightgreen ' )
2012-03-05 13:44:58 +00:00
self . msg ( ' api.php is correct! ' , level = ' ok ' )
2012-03-05 12:57:38 +00:00
else :
self . entry11 . config ( background = ' red ' )
2012-03-05 13:44:58 +00:00
self . msg ( ' api.php is incorrect! ' , level = ' error ' )
2012-03-05 12:57:38 +00:00
elif self . optionmenu11var . get ( ) == ' index.php ' :
self . msg ( ' Please wait... Checking index.php... ' )
if dumpgenerator . checkIndexphp ( self . entry11 . get ( ) ) :
self . entry11 . config ( background = ' lightgreen ' )
2012-03-05 13:44:58 +00:00
self . msg ( ' index.php is OK! ' , level = ' ok ' )
2012-03-05 12:57:38 +00:00
else :
self . entry11 . config ( background = ' red ' )
2012-03-05 13:44:58 +00:00
self . msg ( ' index.php is incorrect! ' , level = ' error ' )
2012-03-05 12:57:38 +00:00
else :
tkMessageBox . showerror ( " Error " , " You have to write a correct api.php or index.php URL. " )
2012-03-04 19:17:49 +00:00
def sumSizes ( self , sizes ) :
total = 0
for size in sizes :
if size . endswith ( ' KB ' ) :
total + = float ( size . split ( ' ' ) [ 0 ] )
elif size . endswith ( ' MB ' ) :
total + = float ( size . split ( ' ' ) [ 0 ] ) * 1024
elif size . endswith ( ' GB ' ) :
total + = float ( size . split ( ' ' ) [ 0 ] ) * 1024 * 1024
elif size . endswith ( ' TB ' ) :
total + = float ( size . split ( ' ' ) [ 0 ] ) * 1024 * 1024 * 1024
2012-03-05 14:48:44 +00:00
elif not size or size . lower ( ) == ' unknown ' :
pass
2012-03-04 19:17:49 +00:00
else :
total + = size
return total / 1024 #MB
2012-03-04 13:31:11 +00:00
def run ( self ) :
for i in range ( 10 ) :
time . sleep ( 0.1 )
self . value + = 10
"""
#get parameters selected
params = [ ' --api=http://www.archiveteam.org/api.php ' , ' --xml ' ]
#launch dump
dumpgenerator . main ( params = params )
#check dump
"""
2012-03-05 13:44:58 +00:00
def msg ( self , msg = ' ' , level = ' ' ) :
levels = { ' ok ' : ' lightgreen ' , ' warning ' : ' yellow ' , ' error ' : ' red ' }
if levels . has_key ( level . lower ( ) ) :
print ' %s : %s ' % ( level . upper ( ) , msg )
2012-03-05 15:05:34 +00:00
self . status . config ( text = ' %s : %s ' % ( level . upper ( ) , msg ) , background = levels [ level . lower ( ) ] )
2012-03-05 13:44:58 +00:00
else :
print msg
self . status . config ( text = msg , background = ' grey ' )
2012-03-05 00:19:20 +00:00
2012-03-04 17:38:44 +00:00
def treeSortColumn ( self , column , reverse = False ) :
l = [ ( self . tree . set ( i , column ) , i ) for i in self . tree . get_children ( ' ' ) ]
l . sort ( reverse = reverse )
for index , ( val , i ) in enumerate ( l ) :
self . tree . move ( i , ' ' , index )
self . tree . heading ( column , command = lambda : self . treeSortColumn ( column = column , reverse = not reverse ) )
2012-03-04 18:53:35 +00:00
def downloadProgress ( self , block_count , block_size , total_size ) :
try :
total_mb = total_size / 1024 / 1024.0
downloaded = block_count * ( block_size / 1024 / 1024.0 )
percent = downloaded / ( total_mb / 100.0 )
if not random . randint ( 0 , 10 ) :
2012-03-05 00:19:20 +00:00
msg = " %.1f MB of %.1f MB downloaded ( %.1f %% ) " % ( downloaded , total_mb , percent < = 100 and percent or 100 )
2012-03-05 13:44:58 +00:00
self . msg ( msg , level = ' ok ' )
2012-03-04 18:53:35 +00:00
#sys.stdout.write("%.1f MB of %.1f MB downloaded (%.2f%%)" %(downloaded, total_mb, percent))
#sys.stdout.flush()
except :
pass
2012-03-04 16:06:35 +00:00
def downloadDump ( self , event = None ) :
2012-03-05 14:49:33 +00:00
if self . block :
2012-03-05 15:00:52 +00:00
self . blocked ( )
2012-03-05 14:49:33 +00:00
return
else :
self . block = True
2012-03-04 16:06:35 +00:00
items = self . tree . selection ( )
2012-03-04 17:38:44 +00:00
if items :
2012-03-04 18:53:35 +00:00
if not os . path . exists ( self . downloadpath ) :
os . makedirs ( self . downloadpath )
2012-03-05 13:44:58 +00:00
c = 0
d = 0
2012-03-04 17:38:44 +00:00
for item in items :
2012-03-04 18:53:35 +00:00
filepath = self . downloadpath and self . downloadpath + ' / ' + self . dumps [ int ( item ) ] [ 0 ] or self . dumps [ int ( item ) ] [ 0 ]
2012-03-04 20:00:21 +00:00
if os . path . exists ( filepath ) :
2012-03-05 13:44:58 +00:00
self . msg ( ' That dump was downloaded before ' , level = ' ok ' )
d + = 1
2012-03-04 20:00:21 +00:00
else :
2012-03-05 13:44:58 +00:00
self . msg ( " [ %d of %d ] Downloading %s from %s " % ( c + 1 , len ( items ) , self . tree . item ( item , " text " ) , self . dumps [ int ( item ) ] [ 5 ] ) )
2012-03-04 20:00:21 +00:00
f = urllib . urlretrieve ( self . dumps [ int ( item ) ] [ 5 ] , filepath , reporthook = self . downloadProgress )
2012-03-05 10:42:18 +00:00
msg = ' %s size is %s bytes large. Download successful! ' % ( self . dumps [ int ( item ) ] [ 0 ] , os . path . getsize ( filepath ) )
2012-03-05 13:44:58 +00:00
self . msg ( msg = msg , level = ' ok ' )
c + = 1
2012-03-04 18:53:35 +00:00
self . dumps [ int ( item ) ] = self . dumps [ int ( item ) ] [ : 6 ] + [ ' True ' ]
2012-03-05 13:44:58 +00:00
if c + d == len ( items ) :
2012-03-05 13:47:54 +00:00
self . msg ( ' Downloaded %d of %d %s . ' % ( c , len ( items ) , d and ' (and %d were previously downloaded) ' % ( d ) or ' ' ) , level = ' ok ' )
2012-03-05 13:44:58 +00:00
else :
self . msg ( ' Problems in %d dumps. Downloaded %d of %d (and %d were previously downloaded). ' % ( len ( items ) - ( c + d ) , c , len ( items ) , d ) , level = ' error ' )
2012-03-04 17:38:44 +00:00
else :
tkMessageBox . showerror ( " Error " , " You have to select some dumps to download. " )
2012-03-04 18:53:35 +00:00
self . clearAvailableDumps ( )
self . showAvailableDumps ( )
2012-03-04 20:00:21 +00:00
self . filterAvailableDumps ( )
2012-03-05 14:49:33 +00:00
self . block = False
2012-03-04 16:06:35 +00:00
2012-03-04 15:46:53 +00:00
def deleteAvailableDumps ( self ) :
2012-03-04 15:50:45 +00:00
#really delete dump list and clear tree
2012-03-04 15:46:53 +00:00
self . clearAvailableDumps ( )
self . dumps = [ ] #reset list
2012-03-04 14:35:36 +00:00
def clearAvailableDumps ( self ) :
2012-03-04 15:50:45 +00:00
#clear tree
2012-03-04 21:04:41 +00:00
for i in range ( len ( self . dumps ) ) :
self . tree . delete ( str ( i ) )
2012-03-04 14:35:36 +00:00
2012-03-04 15:46:53 +00:00
def showAvailableDumps ( self ) :
c = 0
2012-03-04 18:53:35 +00:00
for filename , wikifarm , size , date , mirror , url , downloaded in self . dumps :
2012-03-05 10:22:10 +00:00
self . tree . insert ( ' ' , ' end ' , str ( c ) , text = filename , values = ( filename , wikifarm , size , date , mirror , downloaded and ' Downloaded ' or ' Not downloaded ' ) , tags = ( downloaded and ' downloaded ' or ' nodownloaded ' , ) )
2012-03-04 15:46:53 +00:00
c + = 1
def filterAvailableDumps ( self ) :
2012-03-04 15:05:21 +00:00
self . clearAvailableDumps ( )
2012-03-04 15:46:53 +00:00
self . showAvailableDumps ( )
2012-03-04 19:48:24 +00:00
sizes = [ ]
downloadedsizes = [ ]
nodownloadedsizes = [ ]
2012-03-04 15:46:53 +00:00
for i in range ( len ( self . dumps ) ) :
2012-03-04 19:48:24 +00:00
if ( self . optionmenu21var . get ( ) == ' all ' and self . optionmenu22var . get ( ) == ' all ' and self . optionmenu23var . get ( ) == ' all ' and self . optionmenu24var . get ( ) == ' all ' ) :
sizes . append ( self . dumps [ i ] [ 2 ] )
if self . dumps [ i ] [ 6 ] :
downloadedsizes . append ( self . dumps [ i ] [ 2 ] )
else :
nodownloadedsizes . append ( self . dumps [ i ] [ 2 ] )
elif ( self . optionmenu21var . get ( ) != ' all ' and not self . optionmenu21var . get ( ) == self . dumps [ i ] [ 1 ] ) or \
2012-03-04 15:46:53 +00:00
( self . optionmenu22var . get ( ) != ' all ' and not self . optionmenu22var . get ( ) in self . dumps [ i ] [ 2 ] ) or \
( self . optionmenu23var . get ( ) != ' all ' and not self . optionmenu23var . get ( ) in self . dumps [ i ] [ 3 ] ) or \
( self . optionmenu24var . get ( ) != ' all ' and not self . optionmenu24var . get ( ) in self . dumps [ i ] [ 4 ] ) :
2012-03-04 21:04:41 +00:00
self . tree . detach ( str ( i ) ) #hide this item
2012-03-04 19:48:24 +00:00
sizes . append ( self . dumps [ i ] [ 2 ] )
if self . dumps [ i ] [ 6 ] :
downloadedsizes . append ( self . dumps [ i ] [ 2 ] )
else :
nodownloadedsizes . append ( self . dumps [ i ] [ 2 ] )
2012-03-04 20:00:21 +00:00
self . label25var . set ( " Available dumps: %d ( %.1f MB) " % ( len ( sizes ) , self . sumSizes ( sizes ) ) )
self . label26var . set ( " Downloaded: %d ( %.1f MB) " % ( len ( downloadedsizes ) , self . sumSizes ( downloadedsizes ) ) )
2012-03-05 10:33:56 +00:00
self . label27var . set ( " Not downloaded: %d ( %.1f MB) " % ( len ( nodownloadedsizes ) , self . sumSizes ( nodownloadedsizes ) ) )
2012-03-04 15:46:53 +00:00
2012-03-04 18:53:35 +00:00
def isDumpDownloaded ( self , filename ) :
#improve, size check or md5sum?
if filename :
filepath = self . downloadpath and self . downloadpath + ' / ' + filename or filename
if os . path . exists ( filepath ) :
return True
""" estsize = os.path.getsize(filepath)
c = 0
while int ( estsize ) > = 1024 :
estsize = estsize / 1024.0
c + = 1
estsize = ' %.1f %s ' % ( estsize , [ ' ' , ' KB ' , ' MB ' , ' GB ' , ' TB ' ] [ c ] ) """
return False
2012-03-04 15:46:53 +00:00
def loadAvailableDumps ( self ) :
2012-03-05 14:48:44 +00:00
if self . block :
2012-03-05 15:00:52 +00:00
self . blocked ( )
2012-03-05 14:48:44 +00:00
return
else :
self . block = True
2012-03-04 15:46:53 +00:00
if self . dumps :
self . deleteAvailableDumps ( )
2012-03-04 15:05:21 +00:00
iaregexp = ur ' /download/[^/]+/(?P<filename>[^>]+ \ .7z) " > \ s*(?P<size>[ \ d \ .]+ (?:KB|MB|GB|TB)) \ s*</a> '
2012-03-04 13:31:11 +00:00
self . urls = [
2012-03-04 15:05:21 +00:00
[ ' Google Code ' , ' https://code.google.com/p/wikiteam/downloads/list?num=5000&start=0 ' , ur ' (?im)detail \ ?name=(?P<filename>[^&]+ \ .7z)&can=2&q= " style= " white-space:nowrap " > \ s*(?P<size>[ \ d \ .]+ (?:KB|MB|GB|TB)) \ s*</a></td> ' ] ,
[ ' Internet Archive ' , ' http://www.archive.org/details/referata.com-20111204 ' , iaregexp ] ,
[ ' Internet Archive ' , ' http://www.archive.org/details/WikiTeamMirror ' , iaregexp ] ,
2012-03-05 10:37:29 +00:00
[ ' ScottDB ' , ' http://mirrors.sdboyd56.com/WikiTeam/ ' , ur ' <a href= " (?P<filename>[^>]+ \ .7z) " >(?P<size>[ \ d \ .]+ (?:KB|MB|GB|TB))</a> ' ] ,
2012-03-05 14:48:44 +00:00
[ ' Wikimedia ' , ' http://dumps.wikimedia.org/backup-index.html ' , ur ' (?P<size>)<a href= " (?P<filename>[^>]+) " >[^>]+</a>: <span class= \' done \' >Dump complete</span></li> ' ]
2012-03-04 13:09:58 +00:00
]
2012-03-05 12:29:04 +00:00
wikifarms_r = re . compile ( ur " ( %s ) " % ( ' | ' . join ( wikifarms . keys ( ) ) ) )
2012-03-04 13:31:11 +00:00
c = 0
for mirror , url , regexp in self . urls :
2012-03-04 13:09:58 +00:00
print ' Loading data from ' , mirror , url
2012-03-05 00:19:20 +00:00
self . msg ( msg = ' Please wait... Loading data from %s %s ' % ( mirror , url ) )
2012-03-04 13:09:58 +00:00
f = urllib . urlopen ( url )
m = re . compile ( regexp ) . finditer ( f . read ( ) )
for i in m :
filename = i . group ( ' filename ' )
2012-03-05 14:48:44 +00:00
if mirror == ' Wikimedia ' :
filename = ' %s -pages-meta-history.xml.7z ' % ( re . sub ( ' / ' , ' - ' , filename ) )
2012-03-04 13:09:58 +00:00
wikifarm = ' Unknown '
2012-03-05 12:29:04 +00:00
if re . search ( wikifarms_r , filename ) :
wikifarm = re . findall ( wikifarms_r , filename ) [ 0 ]
2012-03-04 13:12:40 +00:00
wikifarm = wikifarms [ wikifarm ]
2012-03-04 13:09:58 +00:00
size = i . group ( ' size ' )
2012-03-05 15:00:52 +00:00
if not size :
size = ' Unknown '
2012-03-04 13:09:58 +00:00
date = ' Unknown '
2012-03-05 10:45:02 +00:00
if re . search ( ur " \ -( \ d {8} )[ \ .-] " , filename ) :
date = re . findall ( ur " \ -( \ d {4} )( \ d {2} )( \ d {2} )[ \ .-] " , filename ) [ 0 ]
2012-03-04 13:09:58 +00:00
date = ' %s - %s - %s ' % ( date [ 0 ] , date [ 1 ] , date [ 2 ] )
2012-03-05 10:45:02 +00:00
elif re . search ( ur " \ -( \ d {4} \ - \ d {2} \ - \ d {2} )[ \ .-] " , filename ) :
date = re . findall ( ur " \ -( \ d {4} \ - \ d {2} \ - \ d {2} )[ \ .-] " , filename ) [ 0 ]
2012-03-04 18:53:35 +00:00
downloadurl = ' '
if mirror == ' Google Code ' :
downloadurl = ' https://wikiteam.googlecode.com/files/ ' + filename
elif mirror == ' Internet Archive ' :
2012-03-05 12:37:36 +00:00
downloadurl = re . sub ( ur ' /details/ ' , ur ' /download/ ' , url ) + ' / ' + filename
2012-03-05 10:37:29 +00:00
elif mirror == ' ScottDB ' :
downloadurl = url + ' / ' + filename
2012-03-05 14:48:44 +00:00
elif mirror == ' Wikimedia ' :
downloadurl = ' http://dumps.wikimedia.org/ ' + filename . split ( ' - ' ) [ 0 ] + ' / ' + re . sub ( ' - ' , ' ' , date ) + ' / ' + filename
2012-03-04 18:53:35 +00:00
downloaded = self . isDumpDownloaded ( filename )
self . dumps . append ( [ filename , wikifarm , size , date , mirror , downloadurl , downloaded ] )
2012-03-05 12:29:04 +00:00
self . dumps . sort ( )
2012-03-04 15:46:53 +00:00
self . showAvailableDumps ( )
self . filterAvailableDumps ( )
2012-03-05 15:00:52 +00:00
self . msg ( msg = ' Loaded %d available dumps! ' % ( len ( self . dumps ) ) , level = ' ok ' )
2012-03-05 14:48:44 +00:00
self . block = False
2012-03-04 13:31:11 +00:00
2012-03-04 12:35:02 +00:00
def callback ( self ) :
2012-03-05 13:44:58 +00:00
self . msg ( " Feature not implemented for the moment. Contributions are welcome. " , level = ' warning ' )
2012-03-04 12:35:02 +00:00
def askclose ( ) :
if tkMessageBox . askokcancel ( " Quit " , " Do you really wish to exit? " ) :
root . destroy ( )
if __name__ == " __main__ " :
root = Tk ( )
2012-03-05 00:25:14 +00:00
width = 1050
height = 560
2012-03-04 13:09:58 +00:00
# calculate position x, y
x = ( root . winfo_screenwidth ( ) / 2 ) - ( width / 2 )
y = ( root . winfo_screenheight ( ) / 2 ) - ( height / 2 )
root . geometry ( ' %d x %d + %d + %d ' % ( width , height , x , y ) )
2012-03-04 12:35:02 +00:00
root . title ( ' %s (version %s ) ' % ( NAME , VERSION ) )
root . protocol ( " WM_DELETE_WINDOW " , askclose )
#logo
#imagelogo = PhotoImage(file = 'logo.gif')
#labellogo = Label(root, image=imagelogo)
#labellogo.grid(row=0, column=0, rowspan=3, sticky=W)
app = App ( root )
root . mainloop ( )