remove the auto-loading feature for poster pictures from MyAnimeList.net links

This commit is contained in:
Bernd 2025-07-20 13:29:23 +05:00
parent f65a42cdce
commit 50b90bcf9c

View File

@ -2,7 +2,6 @@ import sys
import os
import random
import re
import hashlib
import html
import logging
from datetime import datetime
@ -12,9 +11,8 @@ from PyQt5.QtWidgets import (
QComboBox, QTextEdit, QDialogButtonBox, QAction, QFileDialog, QMessageBox,
QInputDialog, QApplication, QAbstractItemView, QSizePolicy, QHeaderView
)
from PyQt5.QtCore import Qt, QUrl, QEvent
from PyQt5.QtGui import QColor, QIcon, QKeySequence
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QIcon
from backend import AnimeBackend
# Set up logging
@ -108,30 +106,6 @@ Table-specific Shortcuts (apply to selected entry):
buttons.accepted.connect(self.accept)
layout.addWidget(buttons)
class HoverLabel(QLabel):
def __init__(self, main_window, name, url=None):
super().__init__()
self.main_window = main_window
self.url = url
self.fetched = False
self.image_file = None
if url:
self.image_file = os.path.join(main_window.image_cache_dir, hashlib.md5(url.encode()).hexdigest() + '.jpg')
name_escaped = html.escape(name)
self.setText(f'<a href="{url}">{name_escaped}</a>')
self.setOpenExternalLinks(True)
if os.path.exists(self.image_file):
self.setToolTip(f'<img src="{self.image_file}" width="200">')
self.fetched = True
else:
self.setText(html.escape(name))
def enterEvent(self, event):
if self.url and not self.fetched:
self.main_window.fetch_poster(self.url, self)
self.fetched = True
super().enterEvent(event)
class CustomTableWidget(QTableWidget):
def __init__(self, parent, is_pre):
super().__init__()
@ -165,12 +139,6 @@ class AnimeTracker(QMainWindow):
self.setWindowTitle("Anime Backlog Tracker")
self.resize(800, 600)
self.backend = AnimeBackend()
self.image_cache_dir = 'images'
try:
os.makedirs(self.image_cache_dir, exist_ok=True)
except Exception as e:
logging.error(f"Error creating image cache directory: {e}")
self.network_manager = QNetworkAccessManager(self)
self.tab_widget = QTabWidget()
self.setCentralWidget(self.tab_widget)
self.create_menu()
@ -275,7 +243,13 @@ class AnimeTracker(QMainWindow):
# Name
name = entry[1]
url = entry[7]
name_label = HoverLabel(self, name, url)
name_label = QLabel()
if url:
name_escaped = html.escape(name)
name_label.setText(f'<a href="{url}">{name_escaped}</a>')
name_label.setOpenExternalLinks(True)
else:
name_label.setText(html.escape(name))
table.setCellWidget(row, col, name_label)
col += 1
# Type
@ -378,7 +352,13 @@ class AnimeTracker(QMainWindow):
# Name
name = entry[1]
url = entry[7]
name_label = HoverLabel(self, name, url)
name_label = QLabel()
if url:
name_escaped = html.escape(name)
name_label.setText(f'<a href="{url}">{name_escaped}</a>')
name_label.setOpenExternalLinks(True)
else:
name_label.setText(html.escape(name))
table.setCellWidget(row, col, name_label)
col += 1
# Type
@ -464,49 +444,6 @@ class AnimeTracker(QMainWindow):
layout.addWidget(btn)
return widget
def fetch_poster(self, url, label):
image_file = label.image_file
if os.path.exists(image_file):
label.setToolTip(f'<img src="{image_file}" width="200">')
return
try:
request = QNetworkRequest(QUrl(url))
reply = self.network_manager.get(request)
reply.finished.connect(lambda: self.handle_html_reply(reply, image_file, label))
except Exception as e:
logging.error(f"Error fetching poster from {url}: {e}")
label.setToolTip('Failed to load poster')
def handle_html_reply(self, reply, image_file, label):
if reply.error() != QNetworkReply.NoError:
label.setToolTip('Failed to load poster')
return
try:
html = reply.readAll().data().decode('utf-8', errors='ignore')
match = re.search(r'<meta property="og:image" content="([^"]+)"', html)
if match:
image_url = match.group(1)
img_request = QNetworkRequest(QUrl(image_url))
img_reply = self.network_manager.get(img_request)
img_reply.finished.connect(lambda: self.handle_image_reply(img_reply, image_file, label))
else:
label.setToolTip('No poster found')
except Exception as e:
logging.error(f"Error handling HTML reply: {e}")
label.setToolTip('Failed to load poster')
def handle_image_reply(self, reply, image_file, label):
if reply.error() == QNetworkReply.NoError:
try:
with open(image_file, 'wb') as f:
f.write(reply.readAll().data())
label.setToolTip(f'<img src="{image_file}" width="200">')
except Exception as e:
logging.error(f"Error saving image to {image_file}: {e}")
label.setToolTip('Failed to load poster')
else:
label.setToolTip('Failed to load poster')
def add_anime(self):
tab_index = self.tab_widget.currentIndex()
if tab_index == -1: