Example #1
0
	def addRows(self, list_of_gallery, position=None,
				rows=1, index = QModelIndex()):
		"Adds new gallery data to model and to DB"
		if self.REMOVING_ROWS:
			return False
		self.ADD_MORE.emit()
		log_d('Adding {} rows'.format(rows))
		if not position:
			log_d('Add rows: No position specified')
			position = len(self._data)
		self._data_count += len(list_of_gallery)
		self.beginInsertRows(QModelIndex(), position, position + rows - 1)
		log_d('Add rows: Began inserting')
		gallerydb.GalleryDB.begin()
		for gallery in list_of_gallery:
			gallerydb.GalleryDB.add_gallery_return(gallery)
			#gallerydb.add_method_queue(gallerydb.GalleryDB.add_gallery_return, True, gallery)
			gallery.profile = gallerydb.PROFILE_TO_MODEL.get()
			self._data.insert(position, gallery)
		gallerydb.add_method_queue(gallerydb.GalleryDB.end, True)
		log_d('Add rows: Finished inserting')
		self.endInsertRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, True)
		self.CUSTOM_STATUS_MSG.emit("Added item(s)")
		self.ROWCOUNT_CHANGE.emit()
		self.ADDED_ROWS.emit()
		return True
Example #2
0
	def replace_edit_gallery(self, list_of_gallery, pos=None):
		"Replaces the view and DB with given list of gallery, at given position"
		assert isinstance(list_of_gallery, (list, gallerydb.Gallery)), "Please pass a gallery to replace with"
		if isinstance(list_of_gallery, gallerydb.Gallery):
			list_of_gallery = [list_of_gallery]
		log_d('Replacing {} galleries'.format(len(list_of_gallery)))
		for gallery in list_of_gallery:
			if not pos:
				index = self.find_index(gallery.id)
				if not index:
					log_e('Could not find index for gallery to edit: {}'.format(
						gallery.title.encode(errors='ignore')))
					continue
				pos = index.row()
			kwdict = {'title':gallery.title,
			 'profile':gallery.profile,
			 'artist':gallery.artist,
			 'info':gallery.info,
			 'type':gallery.type,
			 'language':gallery.language,
			 'status':gallery.status,
			 'pub_date':gallery.pub_date,
			 'tags':gallery.tags,
			 'link':gallery.link,
			 'series_path':gallery.path,
			 'chapters':gallery.chapters,
			 'exed':gallery.exed}

			gallerydb.add_method_queue(gallerydb.GalleryDB.modify_gallery,
							 True, gallery.id, **kwdict)
		assert isinstance(pos, int)
		self.gallery_model.replaceRows([gallery], pos, len(list_of_gallery))
Example #3
0
	def accept(self):
		name = self.name_edit.text()
		self.item.setText(name)
		self.gallery_list.name = name
		self.gallery_list.filter = self.filter_edit.text()
		gallerydb.add_method_queue(gallerydb.ListDB.modify_list, True, self.gallery_list, True, True)
		gallerydb.add_method_queue(self.gallery_list.scan, True)
		self.close()
Example #4
0
								def add_to_db(self):
									for y, x in enumerate(self.obj):
										gallerydb.add_method_queue(
											gallerydb.GalleryDB.add_gallery_return, False, x)
										self.galleries.append(x)
										y += 1
										self.prog.emit(y)
									append_to_model(self.galleries)
									self.done.emit()
Example #5
0
	def removeRows(self, position, rows=1, index=QModelIndex()):
		"Deletes gallery data from the model data list. OBS: doesn't touch DB!"
		self._data_count -= rows
		self.beginRemoveRows(QModelIndex(), position, position + rows - 1)
		for r in range(rows):
			del self._data[position]
		self.endRemoveRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, False)
		self.ROWCOUNT_CHANGE.emit()
		return True
Example #6
0
	def removeRows(self, position, rows=1, index=QModelIndex()):
		"Deletes gallery data from the model data list. OBS: doesn't touch DB!"
		self._data_count -= rows
		self.beginRemoveRows(QModelIndex(), position, position + rows - 1)
		for r in range(rows):
			del self._data[position]
		self.endRemoveRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, False)
		self.ROWCOUNT_CHANGE.emit()
		return True
Example #7
0
 def _item_double_clicked(self, item):
     if item:
         self._reset_selected()
         if item.item.filter:
             app_constants.NOTIF_BUBBLE.update_text(item.item.name,
                                                    "Updating list..", 5)
             gallerydb.add_method_queue(item.item.scan, True)
         self.GALLERY_LIST_CLICKED.emit(item.item)
         item.setFont(self._font_selected)
         self.current_selected = item
Example #8
0
	def _item_double_clicked(self, item):
		if item:
			if item.type() == self.CREATE_LIST_TYPE:
				self.create_new_list()
			else:
				self._reset_selected()
				if item.item.filter:
					gallerydb.add_method_queue(item.item.scan, True)
				self.GALLERY_LIST_CLICKED.emit(item.item)
				item.setFont(self._font_selected)
				self.current_selected = item
Example #9
0
	def favorite(self, index):
		assert isinstance(index, QModelIndex)
		gallery = index.data(Qt.UserRole+1)
		if gallery.fav == 1:
			gallery.fav = 0
			#self.model().replaceRows([gallery], index.row(), 1, index)
			gallerydb.add_method_queue(gallerydb.GalleryDB.modify_gallery, True, gallery.id, {'fav':0})
			self.gallery_model.CUSTOM_STATUS_MSG.emit("Unfavorited")
		else:
			gallery.fav = 1
			#self.model().replaceRows([gallery], index.row(), 1, index)
			gallerydb.add_method_queue(gallerydb.GalleryDB.modify_gallery, True, gallery.id, {'fav':1})
			self.gallery_model.CUSTOM_STATUS_MSG.emit("Favorited")
Example #10
0
 def accept(self):
     name = self.name_edit.text()
     self.item.setText(name)
     self.gallery_list.name = name
     self.gallery_list.filter = self.filter_edit.text()
     self.gallery_list.enforce = self.enforce.isChecked()
     self.gallery_list.regex = self.regex.isChecked()
     self.gallery_list.case = self.case.isChecked()
     self.gallery_list.strict = self.strict.isChecked()
     gallerydb.add_method_queue(gallerydb.ListDB.modify_list, True,
                                self.gallery_list)
     self.apply.emit()
     self.hide()
Example #11
0
	def favorite(self, index):
		assert isinstance(index, QModelIndex)
		gallery = index.data(Qt.UserRole+1)
		if gallery.fav == 1:
			gallery.fav = 0
			#self.model().replaceRows([gallery], index.row(), 1, index)
			gallerydb.add_method_queue(gallerydb.GalleryDB.modify_gallery, True, gallery.id, {'fav':0})
			self.gallery_model.CUSTOM_STATUS_MSG.emit("Unfavorited")
		else:
			gallery.fav = 1
			#self.model().replaceRows([gallery], index.row(), 1, index)
			gallerydb.add_method_queue(gallerydb.GalleryDB.modify_gallery, True, gallery.id, {'fav':1})
			self.gallery_model.CUSTOM_STATUS_MSG.emit("Favorited")
Example #12
0
	def del_chapter(self, index, chap_numb):
		gallery = index.data(Qt.UserRole+1)
		if len(gallery.chapters) < 2:
			CommonView.remove_gallery(self, [index])
		else:
			msgbox = QMessageBox(self)
			msgbox.setText('Are you sure you want to delete:')
			msgbox.setIcon(msgbox.Question)
			msgbox.setInformativeText('Chapter {} of {}'.format(chap_numb+1,
														  gallery.title))
			msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
			if msgbox.exec() == msgbox.Yes:
				gallery.chapters.pop(chap_numb, None)
				self.gallery_model.replaceRows([gallery], index.row())
				gallerydb.add_method_queue(gallerydb.ChapterDB.del_chapter, True, gallery.id, chap_numb)
Example #13
0
	def del_chapter(self, index, chap_numb):
		gallery = index.data(Qt.UserRole+1)
		if len(gallery.chapters) < 2:
			self.remove_gallery([index])
		else:
			msgbox = QMessageBox(self)
			msgbox.setText('Are you sure you want to delete:')
			msgbox.setIcon(msgbox.Question)
			msgbox.setInformativeText('Chapter {} of {}'.format(chap_numb+1,
														  gallery.title))
			msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
			if msgbox.exec() == msgbox.Yes:
				gallery.chapters.pop(chap_numb, None)
				self.gallery_model.replaceRows([gallery], index.row())
				gallerydb.add_method_queue(gallerydb.ChapterDB.del_chapter, True, gallery.id, chap_numb)
Example #14
0
	def gallery_to_model(self, gallery_list):
		try:
			d_item = self.fetch_instance.download_items.pop(0)
		except IndexError:
			return
		if gallery_list:
			gallery = gallery_list[0]
			if d_item.item.metadata:
				gallery = fetch.Fetch.apply_metadata(gallery, d_item.item.metadata)
			gallery.link = d_item.item.gallery_url
			gallerydb.add_method_queue(
				gallerydb.GalleryDB.add_gallery_return, False, gallery)
			self.gallery_model.insertRows([gallery], None, 1)
			self.gallery_model.init_search(self.gallery_model.current_term)
			d_item.status_item.setText('Added to library!')
		else:
			d_item.status_item.setText('Adding to library failed!')
Example #15
0
	def insertRows(self, list_of_gallery, position=None,
				rows=None, index = QModelIndex(), emit_statusbar=True, data_count=True):
		"Inserts new gallery data to the data list WITHOUT adding to DB"
		if self.REMOVING_ROWS:
			return False
		self.ADD_MORE.emit()
		position = len(self._data) if not position else position
		rows = len(list_of_gallery) if not rows else 0
		if data_count:
			self._data_count += len(list_of_gallery)
		self.beginInsertRows(QModelIndex(), position, position + rows - 1)
		self._data.extend(list_of_gallery)
		self.endInsertRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, True)
		if emit_statusbar:
			self.CUSTOM_STATUS_MSG.emit("Added item(s)")
		self.ROWCOUNT_CHANGE.emit()
		self.ADDED_ROWS.emit()
		return True
Example #16
0
	def insertRows(self, list_of_gallery, position=None,
				rows=None, index = QModelIndex(), emit_statusbar=True, data_count=True):
		"Inserts new gallery data to the data list WITHOUT adding to DB"
		if self.REMOVING_ROWS:
			return False
		self.ADD_MORE.emit()
		position = len(self._data) if not position else position
		rows = len(list_of_gallery) if not rows else 0
		if data_count:
			self._data_count += len(list_of_gallery)
		self.beginInsertRows(QModelIndex(), position, position + rows - 1)
		for pos, gallery in enumerate(list_of_gallery, 1):
			self._data.append(gallery)
		self.endInsertRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, True)
		if emit_statusbar:
			self.CUSTOM_STATUS_MSG.emit("Added item(s)")
		self.ROWCOUNT_CHANGE.emit()
		self.ADDED_ROWS.emit()
		return True
Example #17
0
	def _gallery_to_model(self, gallery_list):
		log_i("Adding downloaded gallery to library")
		try:
			d_item = self.fetch_instance.download_items.pop(0)
		except IndexError:
			return
		if gallery_list:
			gallery = gallery_list[0]
			if d_item.item.metadata:
				gallery = fetch.Fetch.apply_metadata(gallery, d_item.item.metadata)
			gallery.link = d_item.item.gallery_url
			gallerydb.add_method_queue(
				gallerydb.GalleryDB.add_gallery_return, False, gallery)
			self.gallery_model.insertRows([gallery], None, 1)
			self.gallery_model.init_search(self.gallery_model.current_term)
			d_item.status_item.setText('Added to library!')
			log_i("Added downloaded gallery to library")
		else:
			d_item.status_item.setText('Adding to library failed!')
			log_i("Could not add downloaded gallery to library")
Example #18
0
	def remove_gallery(view_cls, index_list, local=False):
		view_cls.sort_model.setDynamicSortFilter(False)
		msgbox = QMessageBox(view_cls)
		msgbox.setIcon(msgbox.Question)
		msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
		if len(index_list) > 1:
			if not local:
				msg = 'Are you sure you want to remove {} selected galleries?'.format(
				len(index_list))
			else:
				msg = 'Are you sure you want to remove {} selected galleries and their files/directories?'.format(
				len(index_list))

			msgbox.setText(msg)
		else:
			if not local:
				msg = 'Are you sure you want to remove this gallery?'
			else:
				msg = 'Are you sure you want to remove this gallery and its file/directory?'
			msgbox.setText(msg)

		if msgbox.exec() == msgbox.Yes:
			view_cls.setUpdatesEnabled(False)
			gallery_list = []
			log_i('Removing {} galleries'.format(len(index_list)))
			view_cls.gallery_model.REMOVING_ROWS = True
			for index in index_list:
				gallery = index.data(Qt.UserRole+1)
				gallery_list.append(gallery)
				log_i('Attempt to remove: {} by {}'.format(gallery.title.encode(),
											gallery.artist.encode()))
			gallerydb.add_method_queue(gallerydb.GalleryDB.del_gallery, True, gallery_list, local=local)
			rows = sorted([x.row() for x in index_list])

			for x in range(len(rows), 0, -1):
				view_cls.sort_model.removeRows(rows[x-1])
			view_cls.STATUS_BAR_MSG.emit('Gallery removed!')
			view_cls.gallery_model.REMOVING_ROWS = False
			view_cls.setUpdatesEnabled(True)
		view_cls.sort_model.setDynamicSortFilter(True)
Example #19
0
	def remove_gallery(self, index_list, local=False):
		self.sort_model.setDynamicSortFilter(False)
		msgbox = QMessageBox()
		msgbox.setIcon(msgbox.Question)
		msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
		if len(index_list) > 1:
			if not local:
				msg = 'Are you sure you want to remove {} selected galleries?'.format(
				len(index_list))
			else:
				msg = 'Are you sure you want to remove {} selected galleries and their files/directories?'.format(
				len(index_list))

			msgbox.setText(msg)
		else:
			if not local:
				msg = 'Are you sure you want to remove this gallery?'
			else:
				msg = 'Are you sure you want to remove this gallery and its file/directory?'
			msgbox.setText(msg)

		if msgbox.exec() == msgbox.Yes:
			gallery_list = []
			log_i('Removing {} galleries'.format(len(index_list)))
			self.gallery_model.REMOVING_ROWS = True
			for index in index_list:
				gallery = index.data(Qt.UserRole+1)
				gallery_list.append(gallery)
				log_i('Attempt to remove: {} by {}'.format(gallery.title.encode(),
											gallery.artist.encode()))
			gallerydb.add_method_queue(gallerydb.GalleryDB.del_gallery, True, gallery_list, local=local)
			rows = sorted([x.row() for x in index_list])

			for x in range(len(rows), 0, -1):
				self.sort_model.removeRows(rows[x-1])
			self.STATUS_BAR_MSG.emit('Gallery removed!')
			self.gallery_model.REMOVING_ROWS = False
		self.sort_model.setDynamicSortFilter(True)
Example #20
0
	def setup_tags(self):
		self.tags_tree.clear()
		tags = gallerydb.add_method_queue(gallerydb.TagDB.get_ns_tags, False)
		items = []
		for ns in tags:
			top_item = QTreeWidgetItem(self.tags_tree)
			if ns == 'default':
				top_item.setText(0, 'No namespace')
			else:
				top_item.setText(0, ns)
			for tag in tags[ns]:
				child_item = QTreeWidgetItem(top_item)
				child_item.setText(0, tag)
		self.tags_tree.sortItems(0, Qt.AscendingOrder)
Example #21
0
 def setup_tags(self):
     self.tags_tree.clear()
     tags = gallerydb.add_method_queue(gallerydb.TagDB.get_ns_tags, False)
     items = []
     for ns in tags:
         top_item = QTreeWidgetItem(self.tags_tree)
         if ns == 'default':
             top_item.setText(0, 'No namespace')
         else:
             top_item.setText(0, ns)
         for tag in tags[ns]:
             child_item = QTreeWidgetItem(top_item)
             child_item.setText(0, tag)
     self.tags_tree.sortItems(0, Qt.AscendingOrder)
Example #22
0
	def addRows(self, list_of_gallery, position=None,
				rows=1, index = QModelIndex()):
		"Adds new gallery data to model and to DB"
		if self.REMOVING_ROWS:
			return False
		self.ADD_MORE.emit()
		log_d('Adding {} rows'.format(rows))
		if not position:
			log_d('Add rows: No position specified')
			position = len(self._data)
		self._data_count += len(list_of_gallery)
		self.beginInsertRows(QModelIndex(), position, position + rows - 1)
		log_d('Add rows: Began inserting')
		for gallery in list_of_gallery:
			gallerydb.add_method_queue(gallerydb.GalleryDB.add_gallery_return, True, gallery)
			gallery.profile = gallerydb.PROFILE_TO_MODEL.get()
			self._data.insert(position, gallery)
		log_d('Add rows: Finished inserting')
		self.endInsertRows()
		gallerydb.add_method_queue(self.db_emitter.update_count, True)
		self.CUSTOM_STATUS_MSG.emit("Added item(s)")
		self.ROWCOUNT_CHANGE.emit()
		self.ADDED_ROWS.emit()
		return True
Example #23
0
	def _auto_metadata_process(self, galleries, hen, valid_url):
		hen.LAST_USED = time.time()
		self.AUTO_METADATA_PROGRESS.emit("Checking gallery urls...")

		fetched_galleries = []
		checked_pre_url_galleries = []
		multiple_hit_galleries = []
		for x, gallery in enumerate(galleries, 1):
			log_i("Checking gallery url")

			# coming from GalleryDialog
			if hasattr(gallery, "_g_dialog_url"):
				if gallery._g_dialog_url:
					gallery.temp_url = gallery._g_dialog_url
					checked_pre_url_galleries.append(gallery)
					# to process even if this gallery is last and fails
					if x == len(galleries):
						self.fetch_metadata(hen=hen)
					continue

			if gallery.link and app_constants.USE_GALLERY_LINK:
				check = self._website_checker(gallery.link)
				if check == valid_url:
					gallery.temp_url = gallery.link
					checked_pre_url_galleries.append(gallery)
					if x == len(galleries):
						self.fetch_metadata(hen=hen)
					continue

			self.AUTO_METADATA_PROGRESS.emit("({}/{}) Generating gallery hash: {}".format(x, len(galleries), gallery.title))
			log_i("Generating gallery hash: {}".format(gallery.title.encode(errors='ignore')))
			hash = None
			try:
				if not gallery.hashes:
					hash_dict = add_method_queue(HashDB.gen_gallery_hash, False, gallery, 0, 'mid')
					if hash_dict:
						hash = hash_dict['mid']
				else:
					hash = gallery.hashes[random.randint(0, len(gallery.hashes)-1)]
			except app_constants.CreateArchiveFail:
				pass
			if not hash:
				self.error_galleries.append((gallery, "Could not generate hash"))
				log_e("Could not generate hash for gallery: {}".format(gallery.title.encode(errors='ignore')))
				if x == len(galleries):
					self.fetch_metadata(hen=hen)
				continue
			gallery.hash = hash

			# dict -> hash:[list of title,url tuples] or None
			self.AUTO_METADATA_PROGRESS.emit("({}/{}) Finding url for gallery: {}".format(x, len(galleries), gallery.title))
			found_url = hen.search(gallery.hash)
			if found_url == 'error':
				app_constants.GLOBAL_EHEN_LOCK = False
				self.FINISHED.emit(True)
				return
			if not gallery.hash in found_url:
				self.error_galleries.append((gallery, "Could not find url for gallery"))
				self.AUTO_METADATA_PROGRESS.emit("Could not find url for gallery: {}".format(gallery.title))
				log_w('Could not find url for gallery: {}'.format(gallery.title.encode(errors='ignore')))
				if x == len(galleries):
					self.fetch_metadata(hen=hen)
				continue
			title_url_list = found_url[gallery.hash]

			if not len(title_url_list) > 1 or app_constants.ALWAYS_CHOOSE_FIRST_HIT:
				title = title_url_list[0][0]
				url = title_url_list[0][1]
			else:
				multiple_hit_galleries.append([gallery, title_url_list])
				if x == len(galleries):
					self.fetch_metadata(hen=hen)
				continue

			if not gallery.link:
				if isinstance(hen, (pewnet.EHen, pewnet.ExHen)):
					gallery.link = url
					self.GALLERY_EMITTER.emit(gallery, None, None)
			gallery.temp_url = url
			self.AUTO_METADATA_PROGRESS.emit("({}/{}) Adding to queue: {}".format(
				x, len(galleries), gallery.title))

			self.fetch_metadata(gallery, hen, x == len(galleries))

		if checked_pre_url_galleries:
			for x, gallery in enumerate(checked_pre_url_galleries, 1):
				self.AUTO_METADATA_PROGRESS.emit("({}/{}) Adding to queue: {}".format(
					x, len(checked_pre_url_galleries), gallery.title))
				self.fetch_metadata(gallery, hen, x == len(checked_pre_url_galleries))

		if multiple_hit_galleries:
			for x, g_data in enumerate(multiple_hit_galleries, 1):
				gallery = g_data[0]
				title_url_list = g_data[1]
				self.AUTO_METADATA_PROGRESS.emit("Multiple galleries found for gallery: {}".format(gallery.title))
				app_constants.SYSTEM_TRAY.showMessage('Happypanda', 'Multiple galleries found for gallery:\n{}'.format(gallery.title),
									minimized=True)
				log_w("Multiple galleries found for gallery: {}".format(gallery.title.encode(errors='ignore')))
				self.GALLERY_PICKER.emit(gallery, title_url_list, self.GALLERY_PICKER_QUEUE)
				user_choice = self.GALLERY_PICKER_QUEUE.get()

				if not user_choice:
					if x == len(multiple_hit_galleries):
						self.fetch_metadata(hen=hen)
					continue

				title = user_choice[0]
				url = user_choice[1]

				if not gallery.link:
					gallery.link = url
					if isinstance(hen, (pewnet.EHen, pewnet.ExHen)):
						self.GALLERY_EMITTER.emit(gallery, None, None)
				gallery.temp_url = url
				self.AUTO_METADATA_PROGRESS.emit("({}/{}) Adding to queue: {}".format(
					x, len(multiple_hit_galleries), gallery.title))
				self.fetch_metadata(gallery, hen, x == len(multiple_hit_galleries))
Example #24
0
	def remove_list(self):
		self.parent_widget.takeItem(self.parent_widget.row(self.item))
		gallerydb.add_method_queue(gallerydb.ListDB.remove_list, True, self.gallery_list)
		self.parent_widget.GALLERY_LIST_REMOVED.emit()
Example #25
0
 def remove_list(self):
     self.parent_widget.takeItem(self.parent_widget.row(self.item))
     gallerydb.add_method_queue(gallerydb.ListDB.remove_list, True,
                                self.gallery_list)
     self.parent_widget.GALLERY_LIST_REMOVED.emit()
Example #26
0
	def auto_web_metadata(self):
		"""
		Auto fetches metadata for the provided list of galleries.
		Appends or replaces metadata with the new fetched metadata.
		"""
		log_i('Initiating auto metadata fetcher')
		if self.galleries and not app_constants.GLOBAL_EHEN_LOCK:
			log_i('Auto metadata fetcher is now running')
			app_constants.GLOBAL_EHEN_LOCK = True
			if 'exhentai' in self._default_ehen_url:
				try:
					exprops = settings.ExProperties()
					if exprops.ipb_id and exprops.ipb_pass:
						hen = pewnet.ExHen(exprops.ipb_id, exprops.ipb_pass)
						valid_url = 'exhen'
					else:
						raise ValueError
				except ValueError:
					hen = pewnet.EHen()
					valid_url = 'ehen'
			else:
				hen = pewnet.EHen()
				valid_url = 'ehen'
			hen.LAST_USED = time.time()
			self.AUTO_METADATA_PROGRESS.emit("Checking gallery urls...")

			fetched_galleries = []
			checked_pre_url_galleries = []
			for x, gallery in enumerate(self.galleries, 1):
				self.AUTO_METADATA_PROGRESS.emit("({}/{}) Generating gallery hash: {}".format(x, len(self.galleries), gallery.title))
				log_i("Generating gallery hash: {}".format(gallery.title.encode(errors='ignore')))
				hash = None
				try:
					if not gallery.hashes:
						hash_dict = add_method_queue(HashDB.gen_gallery_hash, False, gallery, 0, 'mid')
						hash = hash_dict['mid']
					else:
						hash = gallery.hashes[random.randint(0, len(gallery.hashes)-1)]
				except app_constants.app_constants.CreateArchiveFail:
					pass
				if not hash:
					self.error_galleries.append((gallery, "Could not generate hash"))
					log_e("Could not generate hash for gallery: {}".format(gallery.title.encode(errors='ignore')))
					continue
				gallery.hash = hash

				log_i("Checking gallery url")
				if gallery.link and app_constants.USE_GALLERY_LINK:
					check = self._website_checker(gallery.link)
					if check == valid_url:
						gallery.temp_url = gallery.link
						checked_pre_url_galleries.append(gallery)
						continue

				# dict -> hash:[list of title,url tuples] or None
				self.AUTO_METADATA_PROGRESS.emit("({}/{}) Finding url for gallery: {}".format(x, len(self.galleries), gallery.title))
				found_url = hen.eh_hash_search(gallery.hash)
				if found_url == 'error':
					app_constants.GLOBAL_EHEN_LOCK = False
					self.FINISHED.emit(True)
					return
				if not gallery.hash in found_url:
					self.error_galleries.append((gallery, "Could not find url for gallery"))
					self.AUTO_METADATA_PROGRESS.emit("Could not find url for gallery: {}".format(gallery.title))
					log_w('Could not find url for gallery: {}'.format(gallery.title.encode(errors='ignore')))
					continue
				title_url_list = found_url[gallery.hash]
				if app_constants.ALWAYS_CHOOSE_FIRST_HIT:
					title = title_url_list[0][0]
					url = title_url_list[0][1]
				else:
					if len(title_url_list) > 1:
						self.AUTO_METADATA_PROGRESS.emit("Multiple galleries found for gallery: {}".format(gallery.title))
						app_constants.SYSTEM_TRAY.showMessage('Happypanda', 'Multiple galleries found for gallery:\n{}'.format(gallery.title),
											minimized=True)
						log_w("Multiple galleries found for gallery: {}".format(gallery.title.encode(errors='ignore')))
						self.GALLERY_PICKER.emit(gallery, title_url_list, self.GALLERY_PICKER_QUEUE)
						user_choice = self.GALLERY_PICKER_QUEUE.get()
					else:
						user_choice = title_url_list[0]

					if not user_choice:
						continue

					title = user_choice[0]
					url = user_choice[1]

				if not gallery.link:
					gallery.link = url
					self.GALLERY_EMITTER.emit(gallery)
				gallery.temp_url = url
				self.AUTO_METADATA_PROGRESS.emit("({}/{}) Adding to queue: {}".format(
					x, len(self.galleries), gallery.title))
				if x == len(self.galleries):
					self.fetch_metadata(gallery, hen, True)
				else:
					self.fetch_metadata(gallery, hen)

			if checked_pre_url_galleries:
				for x, gallery in enumerate(checked_pre_url_galleries, 1):
					self.AUTO_METADATA_PROGRESS.emit("({}/{}) Adding to queue: {}".format(
						x, len(checked_pre_url_galleries), gallery.title))
					if x == len(checked_pre_url_galleries):
						self.fetch_metadata(gallery, hen, True)
					else:
						self.fetch_metadata(gallery, hen)

			log_d('Auto metadata fetcher is done')
			app_constants.GLOBAL_EHEN_LOCK = False
			if not self.error_galleries:
				self.AUTO_METADATA_PROGRESS.emit('Done! Went through {} galleries successfully!'.format(len(self.galleries)))
				app_constants.SYSTEM_TRAY.showMessage('Done', 'Went through {} galleries successfully!', minimized=True)
				self.FINISHED.emit(True)
			else:
				self.AUTO_METADATA_PROGRESS.emit('Done! Could not fetch metadata for {} galleries. Check happypanda.log for more details!'.format(len(self.error_galleries)))
				app_constants.SYSTEM_TRAY.showMessage('Done!',
										  'Could not fetch metadata for {} galleries. Check happypanda.log for more details!'.format(len(self.error_galleries)),
										  minimized=True)
				for tup in self.error_galleries:
					log_e("{}: {}".format(tup[1], tup[0].title.encode(errors='ignore')))
				self.FINISHED.emit(self.error_galleries)
		else:
			log_e('Auto metadata fetcher is already running')
			self.AUTO_METADATA_PROGRESS.emit('Auto metadata fetcher is already running!')
			self.FINISHED.emit(False)
Example #27
0
    def auto_web_metadata(self):
        """
		Auto fetches metadata for the provided list of galleries.
		Appends or replaces metadata with the new fetched metadata.
		"""
        log_i('Initiating auto metadata fetcher')
        if self.galleries and not app_constants.GLOBAL_EHEN_LOCK:
            log_i('Auto metadata fetcher is now running')
            app_constants.GLOBAL_EHEN_LOCK = True
            if 'exhentai' in self._default_ehen_url:
                try:
                    exprops = settings.ExProperties()
                    if exprops.ipb_id and exprops.ipb_pass:
                        hen = pewnet.ExHen(exprops.ipb_id, exprops.ipb_pass)
                        valid_url = 'exhen'
                    else:
                        raise ValueError
                except ValueError:
                    hen = pewnet.EHen()
                    valid_url = 'ehen'
            else:
                hen = pewnet.EHen()
                valid_url = 'ehen'
            hen.LAST_USED = time.time()
            self.AUTO_METADATA_PROGRESS.emit("Checking gallery urls...")

            fetched_galleries = []
            checked_pre_url_galleries = []
            for x, gallery in enumerate(self.galleries, 1):
                self.AUTO_METADATA_PROGRESS.emit(
                    "({}/{}) Generating gallery hash: {}".format(
                        x, len(self.galleries), gallery.title))
                log_i("Generating gallery hash: {}".format(
                    gallery.title.encode(errors='ignore')))
                hash = None
                try:
                    if not gallery.hashes:
                        hash_dict = add_method_queue(HashDB.gen_gallery_hash,
                                                     False, gallery, 0, 'mid')
                        hash = hash_dict['mid']
                    else:
                        hash = gallery.hashes[random.randint(
                            0,
                            len(gallery.hashes) - 1)]
                except app_constants.app_constants.CreateArchiveFail:
                    pass
                if not hash:
                    self.error_galleries.append(
                        (gallery, "Could not generate hash"))
                    log_e("Could not generate hash for gallery: {}".format(
                        gallery.title.encode(errors='ignore')))
                    continue
                gallery.hash = hash

                log_i("Checking gallery url")
                if gallery.link and app_constants.USE_GALLERY_LINK:
                    check = self._website_checker(gallery.link)
                    if check == valid_url:
                        gallery.temp_url = gallery.link
                        checked_pre_url_galleries.append(gallery)
                        continue

                # dict -> hash:[list of title,url tuples] or None
                self.AUTO_METADATA_PROGRESS.emit(
                    "({}/{}) Finding url for gallery: {}".format(
                        x, len(self.galleries), gallery.title))
                found_url = hen.eh_hash_search(gallery.hash)
                if found_url == 'error':
                    app_constants.GLOBAL_EHEN_LOCK = False
                    self.FINISHED.emit(True)
                    return
                if not gallery.hash in found_url:
                    self.error_galleries.append(
                        (gallery, "Could not find url for gallery"))
                    self.AUTO_METADATA_PROGRESS.emit(
                        "Could not find url for gallery: {}".format(
                            gallery.title))
                    log_w('Could not find url for gallery: {}'.format(
                        gallery.title.encode(errors='ignore')))
                    continue
                title_url_list = found_url[gallery.hash]
                if app_constants.ALWAYS_CHOOSE_FIRST_HIT:
                    title = title_url_list[0][0]
                    url = title_url_list[0][1]
                else:
                    if len(title_url_list) > 1:
                        self.AUTO_METADATA_PROGRESS.emit(
                            "Multiple galleries found for gallery: {}".format(
                                gallery.title))
                        app_constants.SYSTEM_TRAY.showMessage(
                            'Happypanda',
                            'Multiple galleries found for gallery:\n{}'.format(
                                gallery.title),
                            minimized=True)
                        log_w(
                            "Multiple galleries found for gallery: {}".format(
                                gallery.title.encode(errors='ignore')))
                        self.GALLERY_PICKER.emit(gallery, title_url_list,
                                                 self.GALLERY_PICKER_QUEUE)
                        user_choice = self.GALLERY_PICKER_QUEUE.get()
                    else:
                        user_choice = title_url_list[0]

                    if not user_choice:
                        continue

                    title = user_choice[0]
                    url = user_choice[1]

                if not gallery.link:
                    gallery.link = url
                    self.GALLERY_EMITTER.emit(gallery)
                gallery.temp_url = url
                self.AUTO_METADATA_PROGRESS.emit(
                    "({}/{}) Adding to queue: {}".format(
                        x, len(self.galleries), gallery.title))
                if x == len(self.galleries):
                    self.fetch_metadata(gallery, hen, True)
                else:
                    self.fetch_metadata(gallery, hen)

            if checked_pre_url_galleries:
                for x, gallery in enumerate(checked_pre_url_galleries, 1):
                    self.AUTO_METADATA_PROGRESS.emit(
                        "({}/{}) Adding to queue: {}".format(
                            x, len(checked_pre_url_galleries), gallery.title))
                    if x == len(checked_pre_url_galleries):
                        self.fetch_metadata(gallery, hen, True)
                    else:
                        self.fetch_metadata(gallery, hen)

            log_d('Auto metadata fetcher is done')
            app_constants.GLOBAL_EHEN_LOCK = False
            if not self.error_galleries:
                self.AUTO_METADATA_PROGRESS.emit(
                    'Done! Went through {} galleries successfully!'.format(
                        len(self.galleries)))
                app_constants.SYSTEM_TRAY.showMessage(
                    'Done',
                    'Went through {} galleries successfully!',
                    minimized=True)
                self.FINISHED.emit(True)
            else:
                self.AUTO_METADATA_PROGRESS.emit(
                    'Done! Could not fetch metadata for {} galleries. Check happypanda.log for more details!'
                    .format(len(self.error_galleries)))
                app_constants.SYSTEM_TRAY.showMessage(
                    'Done!',
                    'Could not fetch metadata for {} galleries. Check happypanda.log for more details!'
                    .format(len(self.error_galleries)),
                    minimized=True)
                for tup in self.error_galleries:
                    log_e("{}: {}".format(
                        tup[1], tup[0].title.encode(errors='ignore')))
                self.FINISHED.emit(self.error_galleries)
        else:
            log_e('Auto metadata fetcher is already running')
            self.AUTO_METADATA_PROGRESS.emit(
                'Auto metadata fetcher is already running!')
            self.FINISHED.emit(False)
Example #28
0
    def _auto_metadata_process(self, galleries, hen, valid_url, **kwargs):
        hen.LAST_USED = time.time()
        self.AUTO_METADATA_PROGRESS.emit("Checking gallery urls...")

        fetched_galleries = []
        checked_pre_url_galleries = []
        multiple_hit_galleries = []
        for x, gallery in enumerate(galleries, 1):
            custom_args = {}  # send to hen class
            log_i("Checking gallery url")

            # coming from GalleryDialog
            if hasattr(gallery, "_g_dialog_url"):
                if gallery._g_dialog_url:
                    gallery.temp_url = gallery._g_dialog_url
                    checked_pre_url_galleries.append(gallery)
                    # to process even if this gallery is last and fails
                    if x == len(galleries):
                        self.fetch_metadata(hen=hen)
                    continue

            if gallery.link and app_constants.USE_GALLERY_LINK:
                check = self._website_checker(gallery.link)
                if check == valid_url:
                    gallery.temp_url = gallery.link
                    checked_pre_url_galleries.append(gallery)
                    if x == len(galleries):
                        self.fetch_metadata(hen=hen)
                    continue

            self.AUTO_METADATA_PROGRESS.emit(
                "({}/{}) Generating gallery hash: {}".format(
                    x, len(galleries), gallery.title))
            log_i("Generating gallery hash: {}".format(
                gallery.title.encode(errors='ignore')))
            hash = None
            try:
                if not gallery.hashes:
                    color_img = kwargs[
                        'color'] if 'color' in kwargs else False  # used for similarity search on EH
                    hash_dict = add_method_queue(HashDB.gen_gallery_hash,
                                                 False, gallery, 0, 'mid',
                                                 color_img)
                    if color_img and 'color' in hash_dict:
                        custom_args['color'] = hash_dict[
                            'color']  # will be path to filename
                        hash = hash_dict['color']
                    elif hash_dict:
                        hash = hash_dict['mid']
                else:
                    hash = gallery.hashes[random.randint(
                        0,
                        len(gallery.hashes) - 1)]
            except app_constants.CreateArchiveFail:
                pass
            if not hash:
                self.error_galleries.append(
                    (gallery, "Could not generate hash"))
                log_e("Could not generate hash for gallery: {}".format(
                    gallery.title.encode(errors='ignore')))
                if x == len(galleries):
                    self.fetch_metadata(hen=hen)
                continue
            gallery.hash = hash

            # dict -> hash:[list of title,url tuples] or None
            self.AUTO_METADATA_PROGRESS.emit(
                "({}/{}) Finding url for gallery: {}".format(
                    x, len(galleries), gallery.title))
            found_url = hen.search(gallery.hash, **custom_args)
            if found_url == 'error':
                app_constants.GLOBAL_EHEN_LOCK = False
                self.FINISHED.emit(True)
                return
            if not gallery.hash in found_url:
                self.error_galleries.append(
                    (gallery, "Could not find url for gallery"))
                self.AUTO_METADATA_PROGRESS.emit(
                    "Could not find url for gallery: {}".format(gallery.title))
                log_w('Could not find url for gallery: {}'.format(
                    gallery.title.encode(errors='ignore')))
                if x == len(galleries):
                    self.fetch_metadata(hen=hen)
                continue
            title_url_list = found_url[gallery.hash]

            if not len(title_url_list
                       ) > 1 or app_constants.ALWAYS_CHOOSE_FIRST_HIT:
                title = title_url_list[0][0]
                url = title_url_list[0][1]
            else:
                multiple_hit_galleries.append([gallery, title_url_list])
                if x == len(galleries):
                    self.fetch_metadata(hen=hen)
                continue

            if not gallery.link:
                if isinstance(hen, (pewnet.EHen, pewnet.ExHen)):
                    gallery.link = url
                    self.GALLERY_EMITTER.emit(gallery, None, None)
            gallery.temp_url = url
            self.AUTO_METADATA_PROGRESS.emit(
                "({}/{}) Adding to queue: {}".format(x, len(galleries),
                                                     gallery.title))

            self.fetch_metadata(gallery, hen, x == len(galleries))

        if checked_pre_url_galleries:
            for x, gallery in enumerate(checked_pre_url_galleries, 1):
                self.AUTO_METADATA_PROGRESS.emit(
                    "({}/{}) Adding to queue: {}".format(
                        x, len(checked_pre_url_galleries), gallery.title))
                self.fetch_metadata(gallery, hen,
                                    x == len(checked_pre_url_galleries))

        if multiple_hit_galleries:
            for x, g_data in enumerate(multiple_hit_galleries, 1):
                gallery = g_data[0]
                title_url_list = g_data[1]

                self.AUTO_METADATA_PROGRESS.emit(
                    "Multiple galleries found for gallery: {}".format(
                        gallery.title))
                app_constants.SYSTEM_TRAY.showMessage(
                    'Happypanda',
                    'Multiple galleries found for gallery:\n{}'.format(
                        gallery.title),
                    minimized=True)
                log_w("Multiple galleries found for gallery: {}".format(
                    gallery.title.encode(errors='ignore')))
                self.GALLERY_PICKER.emit(gallery, title_url_list,
                                         self.GALLERY_PICKER_QUEUE)
                user_choice = self.GALLERY_PICKER_QUEUE.get()

                if not user_choice:
                    if x == len(multiple_hit_galleries):
                        self.fetch_metadata(hen=hen)
                    continue

                title = user_choice[0]
                url = user_choice[1]

                if not gallery.link:
                    gallery.link = url
                    if isinstance(hen, (pewnet.EHen, pewnet.ExHen)):
                        self.GALLERY_EMITTER.emit(gallery, None, None)
                gallery.temp_url = url
                self.AUTO_METADATA_PROGRESS.emit(
                    "({}/{}) Adding to queue: {}".format(
                        x, len(multiple_hit_galleries), gallery.title))
                self.fetch_metadata(gallery, hen,
                                    x == len(multiple_hit_galleries))