class Gocomics(object): def __init__(self, comic=None, path=None, archive=None, full=None, useComix=None, url=None): """ Init variables""" self.comic = comic.replace(' ', '_') self.path = path self.archive = archive self.full = full self.useComix = useComix self.url = url self.comic_url = self.url.replace('http://www.gocomics.com/', '') self.headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' } """ Look for the comic html page and get informations""" self.parse_comic() if self.full == "False": self.single_dl() else: self.sqlite = Sqlite() self.init_dl_rule() self.full_dl() if self.archive == "True": self.control_path(self.path+"/archives/"+self.comic) self.create_archive() def init_dl_rule(self): """ Look for last comic downloaded""" self.sqlite.connect() self.sqlite.c.execute("select * from dl_rule where comic='%s'" % self.comic) row = self.sqlite.c.fetchall() if not row: self.sqlite.c.execute("insert into dl_rule values(?,?)", (self.comic, self.first[0])) self.sqlite.conn.commit() self.start = self.first[0] else: self.start = row[0][1] self.unpack_archive(self.start) self.sqlite.c.close def control_path(self, path): """ Check if download path exists""" if not os.path.exists(path): try: os.makedirs(path, mode=0755) except OSError, e: print e.errno, e.strerror, e.filename
class Preferences(QDialog, preferences_ui.Ui_Dialog): def __init__(self): QDialog.__init__(self) preferences_ui.Ui_Dialog.__init__(self) Dialog_Preferences = QDialog() self.setupUi(Dialog_Preferences) self.sqlite = Sqlite() self.initialise() """ Configuration SLOT""" self.connect(self.radioButton, SIGNAL("clicked()"), self.ajouter_comic) self.connect(self.radioButton_2, SIGNAL("clicked()"), self.ajouter_manga) self.connect(self.pushButton_3, SIGNAL("clicked()"), self.ajouter_sqlite) self.connect(self.radioButton_3, SIGNAL("clicked()"), self.supprimer_comic) self.connect(self.radioButton_4, SIGNAL("clicked()"), self.supprimer_manga) self.connect(self.pushButton_4, SIGNAL("clicked()"), self.supprimer_sqlite) self.connect(self.pushButton_5, SIGNAL("clicked()"), self.update_prefs) self.connect(self.pushButton, SIGNAL("clicked()"), self.choix_path) self.connect(self.pushButton_2, SIGNAL("clicked()"), Dialog_Preferences.accept) Dialog_Preferences.exec_() def initialise(self): """ Look for preferences in database, and check CheckBox with good options""" self.sqlite.connect() self.sqlite.c.execute('''select value from glob_prefs where \ param="full"''') row = self.sqlite.c.fetchall() if row: if row[0][0] == "True": self.checkBox.setChecked(True) self.full = 1 else: self.full = 0 self.sqlite.c.execute('''select value from glob_prefs where \ param="archive"''') row = self.sqlite.c.fetchall() if row: if row[0][0] == "True": self.checkBox_2.setChecked(True) self.archive = 1 else: self.archive = 0 self.sqlite.c.execute('''select value from glob_prefs where \ param="optimise"''') row = self.sqlite.c.fetchall() if row: if row[0][0] == "True": self.checkBox_3.setChecked(True) self.optimise = 1 else: self.optimise = 0 self.sqlite.c.execute('''select value from glob_prefs where \ param="pdf"''') row = self.sqlite.c.fetchall() if row: if row[0][0] == "True": self.checkBox_4.setChecked(True) self.pdf = 1 else: self.pdf = 0 self.sqlite.c.close() def ajouter_comic(self): """ Put prefered comics in comboBox""" i = 0 self.ajout = "comics" self.comboBox.clear() self.sqlite.connect() self.sqlite.c.execute('''select name from comics order by name''') row = self.sqlite.c.fetchall() lenght = len(row)-1 while i <= lenght: item = row[i][0] self.sqlite.c.execute('''select name from preferences where name="%s"''' % item) row_2 = self.sqlite.c.fetchall() if not row_2: self.comboBox.addItem(row[i][0]) i += 1 self.sqlite.c.close() def ajouter_manga(self): """ Put prefered mangas in comboBox""" i= 0 self.ajout = "mangas" self.comboBox.clear() self.sqlite.connect() self.sqlite.c.execute('''select name from mangas order by name''') row = self.sqlite.c.fetchall() lenght = len(row)-1 while i <= lenght: self.sqlite.c.execute("""select name from preferences where name='%s'""" % (row[i][0])) row_2 = self.sqlite.c.fetchall() if not row_2: self.comboBox.addItem(row[i][0]) i += 1 self.sqlite.c.close() def ajouter_sqlite(self): """ Add a comic/manga in preferences table""" self.sqlite.connect() name = (self.comboBox.currentText()) self.sqlite.c.execute("""insert into preferences values(?,?)""", (self.ajout,str(name))) self.sqlite.conn.commit() self.sqlite.c.close() if self.ajout == "mangas": self.ajouter_manga() else: self.ajouter_comic() def supprimer_comic(self): """ Delete a comic in preferences table in database""" i = 0 self.comboBox_2.clear() self.suppression = "comics" self.sqlite.connect() self.sqlite.c.execute("""select name from preferences where \ table_ref='comics'""") row = self.sqlite.c.fetchall() lenght = len(row) -1 while i<= lenght: self.comboBox_2.addItem(row[i][0]) i += 1 self.sqlite.c.close() def supprimer_manga(self): """ Delete a manga in preferences table""" i = 0 self.comboBox_2.clear() self.suppression = "mangas" self.sqlite.connect() self.sqlite.c.execute('''select name from preferences where \ table_ref="mangas"''') row = self.sqlite.c.fetchall() lenght = len(row) -1 while i<= lenght: self.comboBox_2.addItem(row[i][0]) i += 1 self.sqlite.c.close() def supprimer_sqlite(self): """ Delete a comic/manga in preference table in database""" self.sqlite.connect() item = str(self.comboBox_2.currentText()) self.sqlite.c.execute('''delete from preferences where name="%s"''' % item) self.sqlite.conn.commit() if self.suppression == "mangas": self.supprimer_manga() else: self.supprimer_comic() def update_prefs(self): """ Update preferences in database""" self.sqlite.connect() if self.checkBox.isChecked(): if self.full == 0: self.sqlite.c.execute('''update glob_prefs set value="True" where \ param="full"''') self.full = 1 else: if self.full == 1: self.sqlite.c.execute('''update glob_prefs set value="False" where \ param="full"''') self.full = 0 if self.checkBox_2.isChecked(): if self.archive == 0: self.sqlite.c.execute('''update glob_prefs set value="True" where \ param="archive"''') self.archive = 1 else: if self.archive == 1: self.sqlite.c.execute('''update glob_prefs set value="False" where \ param="archive"''') self.archive = 0 if self.checkBox_3.isChecked(): if self.optimise == 0: self.sqlite.c.execute('''update glob_prefs set value="True" where \ param="optimise"''') self.optimise = 1 else: if self.optimise == 1: self.sqlite.c.execute('''update glob_prefs set value="False" where \ param="optimise"''') self.optimise = 0 if self.checkBox_4.isChecked(): if self.pdf == 0: self.sqlite.c.execute('''update glob_prefs set value="True" where \ param="pdf"''') self.pdf = 1 else: if self.pdf == 1: self.sqlite.c.execute('''update glob_prefs set value="False" where \ param="pdf"''') self.pdf = 0 self.sqlite.conn.commit() self.sqlite.c.close() def choix_path(self): """ Choose download path""" self.sqlite.connect() self.sqlite.c.execute('''select value from glob_prefs where \ param="path"''') row = self.sqlite.c.fetchall() if row: path = row[0][0] choix_path = QFileDialog.getExistingDirectory(self) if row: print str(choix_path) self.sqlite.c.execute('''update glob_prefs set value="%s" \ where param="path"''' % choix_path) self.sqlite.conn.commit() self.sqlite.c.close()
class Manga(object): def __init__(self, manga=None, path=None, archive=None, full=None, useComix=None, url=None, pdf=None): """ Init variables""" self.manga = manga.replace(' ', '_') self.path = path self.archive = archive self.full = full self.useComix = useComix self.url = url self.pdf= pdf self.headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' } if self.archive == "True": self.control_path(self.path+"/archives/"+self.manga) if self.pdf == "True": self.control_path(self.path+"/pdf/"+self.manga) self.parseManga = self.parse_manga() """ Look if manga folder contains Chapters""" if not self.parseManga[1]: print "Le manga ne contient aucun chapitre" print "Téléchargement arrêté" else: self.sqlite = Sqlite() if self.full == "False": self.prepare_download() self.single_dl() else: self.prepare_download() self.full_dl() def init_dl_rule(self): """ Look for last chapter downloaded in database""" self.sqlite.connect() self.sqlite.c.execute("select * from dl_rule where comic='%s'" % self.manga) row = self.sqlite.c.fetchall() if not row: self.sqlite.c.execute("insert into dl_rule values(?,?)",(self.manga, '0')) self.sqlite.conn.commit() self.start_i = 0 self.start = self.parseManga[1][0] return True else: self.start_i = int(row[0][1]) nbr_l = len(self.parseManga[1]) - 1 if self.start_i <= nbr_l : self.start = self.parseManga[1][self.start_i] return True else: return False self.sqlite.c.close def prepare_download(self): """ define local paths for download""" test_dl = self.init_dl_rule() if test_dl is not False : self.normalize_chapter() self.urlDl = self.url+self.chapter+"/" self.pathDl = self.path+"/download/"+self.manga+"/"+self.chapter self.control_path(self.pathDl) def single_dl(self): """ Single chapter download""" self.download() self.start_i += 1 self.sqlite.connect() self.sqlite.c.execute("update dl_rule set data=(?) where comic=(?)", (self.start_i, self.manga)) self.sqlite.conn.commit() self.sqlite.c.close() if self.pdf == "True": self.make_pdf() if self.archive == "True": self.make_archive() def full_dl(self): """All chapter dowload """ while self.start_i <= self.parseManga[0] - 1: self.single_dl() self.prepare_download() def control_path(self, path): """ Check if download path exists""" if not os.path.exists(path): try: os.makedirs(path, mode=0755) except OSError, e: print e.errno, e.strerror, e.filename
class DLComix(QMainWindow, dlcomix_ui.Ui_DLComix): def __init__(self): QMainWindow.__init__(self) dlcomix_ui.Ui_DLComix.__init__(self) self.headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' } self.sqlite = Sqlite() self.setupUi(self) self.init_options() self.statusbar.showMessage(self.trUtf8("Prêt")) """ Configuration SLOT""" self.connect(self.pushButton, SIGNAL("clicked()"), self.update_database) self.connect(self.actionQuitter, SIGNAL("triggered()"), qApp, SLOT("quit()")) self.connect(self.actionPreferences, SIGNAL("triggered()"), self.preferences) self.connect(self.actionA_Propos, SIGNAL("triggered()"), self.apropos) self.connect(self.radioButton, SIGNAL("clicked()"), self.combo_comic) self.connect(self.radioButton_2, SIGNAL("clicked()"), self.combo_manga) self.connect(self.pushButton_2, SIGNAL("clicked()"), self.telecharger_prefs) self.connect(self.pushButton_3, SIGNAL("clicked()"), self.telecharger) self.connect(self.pushButton_4, SIGNAL("clicked()"), self.init_options) def init_options(self): """ Look for preferences in database and check the checkBox with available options """ self.check_preferences() if self.full == "True": self.checkBox.setChecked(True) if self.archive == "True": self.checkBox_2.setChecked(True) if self.optimise == "True": self.checkBox_3.setChecked(True) if self.pdf == "True": self.checkBox_4.setChecked(True) def update_database(self): """ Update database with available comics and mangas""" self.initialise_comic() self.initialise_manga() self.sqlite.c.close() def telecharger(self): """ Make a download of the comic/manga selected in the main window """ """ Define preferences""" if self.checkBox.isChecked(): self.full = "True" else: self.full = "False" if self.checkBox_2.isChecked(): self.archive = "True" else: self.archive = "False" if self.checkBox_3.isChecked(): self.optimise = "True" else: self.optimise = "False" if self.checkBox_4.isChecked(): self.pdf = "True" self.sqlite.connect() """ Define if target is Manga or Comic and launch appropriate Class""" if self.radioButton_2.isChecked(): self.sqlite.c.execute("select * from mangas where name='%s'" % self.comboBox.currentText()) for row in self.sqlite.c: manga = Manga(row[0], self.path, self.archive, self.full, self.optimise, row[1], self.pdf) elif self.radioButton.isChecked(): self.sqlite.c.execute("select * from comics where name='%s'" % self.comboBox.currentText()) for row in self.sqlite.c: comic = Gocomics(row[0], self.path, self.archive, self.full, self.optimise, row[1]) elif not self.radioButton.isChecked() and not self.radioButton_2.isChecked(): QMessageBox.warning(None, self.trUtf8("Téléchargement"), self.trUtf8("""Veuillez sélectionner un manga ou comic avant de lancer le téléchargement""")) self.sqlite.c.close() def telecharger_prefs(self): """ Launch download of manga/comic recorded in preferences""" i = 0 self.check_preferences() self.sqlite.connect() self.sqlite.c.execute('''select * from preferences''') row = self.sqlite.c.fetchall() lenght = len(row) -1 while i <= lenght: if row[i][0] == "mangas": self.sqlite.c.execute("select * from mangas where name='%s'" % row[i][1]) row_2 = self.sqlite.c.fetchall() manga = Manga(row_2[0][0], self.path, self.archive, self.full, self.optimise, row_2[0][1], self.pdf ) if row[i][0] == "comics": self.sqlite.c.execute("select * from comics where name='%s'" % row[i][1]) row_2 = self.sqlite.c.fetchall() comic = Gocomics(row_2[0][0], self.path, self.archive, self.full, self.optimise, row_2[0][1]) i += 1 if not row: msgPreferences = QMessageBox.question(None, self.trUtf8("Téléchargement"), self.trUtf8("""Vous n'avez par encore ajouté de comics ou mangas dans les préférences Souhaitez vous le faire maintenant ?"""), QMessageBox.StandardButtons(\ QMessageBox.No | \ QMessageBox.Yes)) if (msgPreferences == QMessageBox.Yes): self.preferences() self.sqlite.c.close() def combo_comic(self): """ Generate the Comic List in the comboBox""" i= 0 self.checkBox_3.setEnabled(False) self.checkBox_4.setEnabled(False) self.comboBox.clear() self.sqlite.connect() self.sqlite.c.execute('''select name from comics order by name''') row = self.sqlite.c.fetchall() lenght = len(row)-1 while i <= lenght: self.comboBox.addItem(row[i][0]) i += 1 self.sqlite.c.close() def combo_manga(self): """ Generate the Manga list in ComboBox""" i = 0 self.checkBox_3.setEnabled(True) self.checkBox_4.setEnabled(True) self.comboBox.clear() self.sqlite.connect() self.sqlite.c.execute('''select name from mangas order by name''') row = self.sqlite.c.fetchall() lenght = len(row)-1 while i <= lenght: self.comboBox.addItem(row[i][0]) i += 1 self.sqlite.c.close() """ Fonctions tierces""" def check_preferences(self): """ Select Preferences in database""" """ Look if database exist. If not, initialise it""" sqliteFile = os.path.expanduser ('~')+'/.dlcomix/dlcomix.sqlite' if not os.path.exists(sqliteFile): QMessageBox.information(None, self.trUtf8("Information"), self.trUtf8("""DLComix effectue une mise a jour necessaire de la base \ de données avant de démarrer. Cela peut prendre un peu de temps""")) self.sqlite.initialise_sqlite() self.update_database() self.sqlite.connect() self.sqlite.c.execute('''select * from glob_prefs''') row = self.sqlite.c.fetchall() if not row: QMessageBox.information(None, self.trUtf8("Information"), self.trUtf8("""DLComix effectue une mise a jour necessaire de la base \ de données avant de démarrer. Cela peut prendre un peu de temps""")) self.update_database() self.check_preferences() else: self.full = row[0][1] self.archive = row[1][1] self.optimise = row[2][1] self.path = row[3][1] if len(row)<5: self.sqlite.c.execute('''insert into glob_prefs values (?,?)''', ("pdf","false")) self.sqlite.conn.commit() self.pdf = "False" else: self.pdf = row[4][1] self.sqlite.c.close() def initialise_manga(self): """ Initialise the manga list in the database""" i = 0 self.sqlite.connect() print ("Initialisation de la liste de manga").decode('utf-8') print ("Cela peu prendre un peu de temps").decode('utf-8') print ("Connection au serveur").decode('utf-8') req = urllib2.Request('http://99.198.113.68/manga', headers = self.headers) response = urllib2.urlopen(req) source = response.read() link = re.findall('<li><a href="(.*?)">',source) link.remove(link[0]) n = len(link)-1 while i <= n: if link[i].count('.jpg')>0: link.remove(link[i]) n -= 1 i -= 1 i += 1 mangaList = re.findall('"> (.*?)/</a></li>',source) n = len(mangaList)-1 self.sqlite.c.execute('delete from mangas') i= 0 while i <= n: if mangaList[i].count('_') > 0: mangaList[i] = mangaList[i].replace('_',' ') tmpManga = str(mangaList[i]) linkManga = "http://99.198.113.68/manga/"+link[i] self.sqlite.c.execute("""insert into mangas values(?,?)""", (tmpManga,linkManga)) i += 1 """ TODO : make an update an not a complete insertion to purpose a new manga window""" self.sqlite.c.execute('delete from mangas where name like "Chapter-%"') self.sqlite.conn.commit() self.sqlite.c.close() print ("Fin de l'initialisation de la liste de manga").decode('utf-8') def initialise_comic(self): """ Initialise comic list in database""" i = 0 self.sqlite.connect() """ TODO : make an update an not a complete insertion to purpose a new comic window""" self.sqlite.c.execute('delete from comics') print ("Initialisation de la liste de comic").decode('utf-8') print ("Connection au serveur").decode('utf-8') req = urllib2.Request('http://www.gocomics.com/features', headers = self.headers) response = urllib2.urlopen(req) self.comicList = self.parse_comic(response) self.comic_db() req = urllib2.Request('http://www.gocomics.com/explore/editorial_lists', headers = self.headers) response = urllib2.urlopen(req) self.comicList = self.parse_comic(response) self.comic_db() req = urllib2.Request('http://www.gocomics.com/explore/sherpa_list', headers = self.headers) response = urllib2.urlopen(req) self.comicList = self.parse_comic(response) self.comic_db() self.sqlite.c.close() print ("Fin de l'initialisation de la liste de comic").decode('utf-8') def parse_comic(self, response): """ Parse gocomics html page to get necessary informations""" source = response.read() test = re.findall('<a href="/(.*?)" class="alpha_list updated">(.*?)</a>', source) name = ['']*len(test) link = ['']*len(test) for i in range (0, len(test)): name[i] = test[i][1].decode('utf-8').capitalize() lk = (test[i][0]).replace(' ','') link[i] = ("http://www.gocomics.com/"+lk).decode('utf-8') return name, link def comic_db(self): """ Insert comic list in database""" n = len(self.comicList[0])-1 i = 0 while i <= n: self.sqlite.c.execute("""insert into comics values(?,?)""", (self.comicList[0][i],self.comicList[1][i])) i += 1 self.sqlite.conn.commit() """Appel de fenetres""" def apropos(self): """ Call About window""" apropos.Apropos() def preferences(self): """ Call preferences window""" preferences.Preferences()