Skip to content

Commit b41b5fa

Browse files
committed
- fixed and refactored even more things
1 parent be938d3 commit b41b5fa

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
lines changed

main.py

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,11 @@ def __init__(self, iterator, search_limit, data_mode, is_reverse, is_checked, la
515515
self.delay = delay
516516
self.output_path = output_path
517517
self.directory_system = directory_system
518-
self.url = url
519-
520-
def process_video(self, index):
521-
video = check_video(self.url, delay=self.delay)
522518

519+
def process_video(self, video, index):
520+
print(f"Requesting processing of video: {video.url}")
521+
video = check_video(video, delay=self.delay)
522+
print(type(video))
523523
try:
524524
data = load_video_attributes(video, self.data_mode)
525525

@@ -624,7 +624,7 @@ def run(self):
624624
break # Respect search limit
625625

626626

627-
text_data = self.process_video(i)
627+
text_data = self.process_video(video, i) # Passes video and index object / int
628628
if text_data is False:
629629
break # Skip to the next video if processing failed
630630

@@ -656,7 +656,7 @@ def run(self):
656656
class DownloadThread(QRunnable):
657657
"""Refactored threading class to download videos with improved performance and logic."""
658658

659-
def __init__(self, video, quality, output_path, threading_mode, workers, timeout, skip_existing_files):
659+
def __init__(self, video, quality, output_path, threading_mode, workers, timeout, skip_existing_files, data):
660660
super().__init__()
661661
self.ffmpeg = None
662662
self.video = video
@@ -668,6 +668,7 @@ def __init__(self, video, quality, output_path, threading_mode, workers, timeout
668668
self.skip_existing_files = skip_existing_files
669669
self.workers = int(workers)
670670
self.timeout = int(timeout)
671+
self.data = data
671672
self.video_progress = {}
672673
self.last_update_time = 0
673674
self.progress_signals = {
@@ -803,7 +804,7 @@ def run(self):
803804
# ... other video types ...
804805

805806
finally:
806-
self.signals.download_completed.emit()
807+
self.signals.download_completed.emit(self.data)
807808

808809

809810
class PostProcessVideoThread(QRunnable):
@@ -824,25 +825,33 @@ def __init__(self, write_tags_, data, ffmpeg_path, video_format):
824825

825826

826827
def run(self):
827-
os.rename(f"{self.path}", f"{self.path}_.tmp")
828-
logger.debug(f"FFMPEG PATH: {self.ffmpeg_path}")
828+
if self.ffmpeg_path is None:
829+
logger.warning("FFmpeg couldn't be found during initialization. Video post processing will be skipped!")
830+
return
831+
832+
try:
833+
os.rename(f"{self.path}", f"{self.path}_.tmp")
834+
logger.debug(f"FFMPEG PATH: {self.ffmpeg_path}")
829835

830-
if self.format == "mp4":
831-
cmd = [self.ffmpeg_path, "-i", f"{self.path}_.tmp", "-c", "copy", self.path]
836+
if self.format == "mp4":
837+
cmd = [self.ffmpeg_path, "-i", f"{self.path}_.tmp", "-c", "copy", self.path]
832838

833-
else:
834-
self.path = str(self.path).replace(".mp4", f"{self.format}")
835-
cmd = [self.ffmpeg_path, '-i', f"{self.path}_.tmp", self.path]
839+
else:
840+
self.path = str(self.path).replace(".mp4", f"{self.format}")
841+
cmd = [self.ffmpeg_path, '-i', f"{self.path}_.tmp", self.path]
836842

837843

838-
ff = FfmpegProgress(cmd)
839-
for progress in ff.run_command_with_progress():
840-
self.signals.ffmpeg_converting_progress.emit(round(progress), 100)
844+
ff = FfmpegProgress(cmd)
845+
for progress in ff.run_command_with_progress():
846+
self.signals.ffmpeg_converting_progress.emit(round(progress), 100)
841847

842-
os.remove(f"{self.path}_.tmp")
848+
os.remove(f"{self.path}_.tmp")
843849

844-
if self.write_tags_:
845-
write_tags(path=self.path, data=self.data)
850+
if self.write_tags_:
851+
write_tags(path=self.path, data=self.data)
852+
853+
except Exception as e:
854+
self.signals.error_signal.emit(e)
846855

847856

848857
class QTreeWidgetDownloadThread(QRunnable):
@@ -859,12 +868,14 @@ def __init__(self, tree_widget, threading_mode, semaphore, quality):
859868
def run(self):
860869
self.signals.start_undefined_range.emit()
861870
video_objects = []
871+
data_objects = []
862872

863873
for i in range(self.treeWidget.topLevelItemCount()):
864874
item = self.treeWidget.topLevelItem(i)
865875
check_state = item.checkState(0)
866876
if check_state == Qt.CheckState.Checked:
867877
video_objects.append(item.data(0, Qt.ItemDataRole.UserRole))
878+
data_objects.append(item.data(2, Qt.ItemDataRole.UserRole))
868879

869880
if not self.threading_mode == "FFMPEG":
870881
logger.debug("Getting segments...")
@@ -890,12 +901,12 @@ def run(self):
890901
downloaded_segments = 0
891902
self.signals.stop_undefined_range.emit()
892903

893-
for video in video_objects:
904+
for idx, video in enumerate(video_objects):
894905
self.semaphore.acquire() # Trying to start the download if the thread isn't locked
895906
if stop_flag.is_set():
896907
return
897908
logger.debug("Semaphore Acquired")
898-
self.signals.progress_send_video.emit(video) # Now emits the video to the main class for further processing
909+
self.signals.progress_send_video.emit(video, data_objects[idx]) # Now emits the video to the main class for further processing
899910

900911

901912
class AddUrls(QRunnable):
@@ -962,6 +973,8 @@ def __init__(self, parent=None, start_installation=False, app_name="Porn Fetch")
962973
self.download_thread = None
963974
self.video_loader_thread = None
964975
self.video_converting_thread = None
976+
self.post_processing_thread = None
977+
self.download_tree_thread = None
965978

966979
# Button groups
967980
self.group_threading_mode = None
@@ -1715,17 +1728,20 @@ def download_tree_widget(self):
17151728
Starts the thread for downloading the tree widget (All selected videos)
17161729
"""
17171730
tree_widget = self.ui.treeWidget
1718-
download_tree_thread = QTreeWidgetDownloadThread(tree_widget=tree_widget, quality=self.quality,
1731+
self.download_tree_thread = QTreeWidgetDownloadThread(tree_widget=tree_widget, quality=self.quality,
17191732
semaphore=self.semaphore, threading_mode=self.threading_mode)
1720-
download_tree_thread.signals.start_undefined_range.connect(self.start_undefined_range)
1721-
download_tree_thread.signals.stop_undefined_range.connect(self.stop_undefined_range)
1722-
self.threadpool.start(download_tree_thread)
1733+
self.download_tree_thread.signals.start_undefined_range.connect(self.start_undefined_range)
1734+
self.download_tree_thread.signals.stop_undefined_range.connect(self.stop_undefined_range)
1735+
self.download_tree_thread.signals.progress_send_video.connect(self.process_video_thread)
1736+
1737+
self.threadpool.start(self.download_tree_thread)
17231738

17241739
def process_video_thread(self, video, data):
17251740
"""Checks which of the three types of threading the user selected and handles them."""
17261741
self.download_thread = DownloadThread(video=video, output_path=self.output_path, quality=self.quality,
17271742
threading_mode=self.threading_mode, workers=self.workers,
1728-
timeout=self.timeout, skip_existing_files=self.skip_existing_files)
1743+
timeout=self.timeout, skip_existing_files=self.skip_existing_files,
1744+
data=data)
17291745
self.download_thread.signals.progress_pornhub.connect(self.update_progressbar)
17301746
self.download_thread.signals.total_progress.connect(self.update_total_progressbar)
17311747
self.download_thread.signals.progress_hqporner.connect(self.update_progressbar_hqporner)
@@ -1750,9 +1766,23 @@ def download_completed(self, data):
17501766
self.ui.progressbar_hqporner.setValue(0)
17511767
self.ui.progressbar_eporner.setValue(0)
17521768

1769+
self.post_processing_thread = PostProcessVideoThread(ffmpeg_path=self.ffmpeg_path, write_tags_=self.write_metadata,
1770+
data=data, video_format=self.format)
1771+
self.post_processing_thread.signals.ffmpeg_converting_progress.connect(self.update_converting)
1772+
self.post_processing_thread.signals.error_signal.connect(self.show_error)
1773+
self.threadpool.start(self.post_processing_thread)
1774+
17531775
# TODO: Add the post processing into this method
17541776
self.semaphore.release()
17551777

1778+
def show_error(self, error):
1779+
err = self.tr(f"""
1780+
An error happened inside of Porn Fetch!
1781+
1782+
{error}""")
1783+
ui_popup(err)
1784+
1785+
17561786

17571787

17581788
def reindex(self):
@@ -2167,14 +2197,18 @@ def check_ffmpeg(self):
21672197

21682198
if ffmpeg_path is None:
21692199
# If ffmpeg is not in PATH, check the current directory for ffmpeg binaries
2170-
ffmpeg_binary = "ffmpeg.exe" if os.path.isfile("ffmpeg.exe") else "ffmpeg" if os.path.isfile(
2200+
ffmpeg_path = "ffmpeg.exe" if os.path.isfile("ffmpeg.exe") else "ffmpeg" if os.path.isfile(
21712201
"ffmpeg") else None
21722202

2173-
if ffmpeg_binary is None:
2174-
# If ffmpeg binaries are not found in the current directory, display warning and disable features
2175-
if self.conf.get("Performance", "ffmpeg_warning") == "true":
2176-
ffmpeg_warning_message = self.tr(
2177-
"""
2203+
if not ffmpeg_path is None:
2204+
ffmpeg_path = os.path.abspath(ffmpeg_path)
2205+
2206+
2207+
if ffmpeg_path is None:
2208+
# If ffmpeg binaries are not found in the current directory, display warning and disable features
2209+
if self.conf.get("Performance", "ffmpeg_warning") == "true":
2210+
ffmpeg_warning_message = self.tr(
2211+
"""
21782212
FFmpeg isn't installed on your system... Some features won't be available:
21792213
21802214
- The FFmpeg threading mode
@@ -2187,24 +2221,20 @@ def check_ffmpeg(self):
21872221
local PATH (e.g, through your linux package manager, or through the Windows PATH)
21882222
21892223
This warning won't be shown again.
2190-
""", None)
2191-
ui_popup(ffmpeg_warning_message)
2192-
self.conf.set("Performance", "ffmpeg_warning", "false")
2193-
with open("config.ini", "w") as config_file: # type: TextIOWrapper
2194-
self.conf.write(config_file)
2224+
""", None)
2225+
ui_popup(ffmpeg_warning_message)
2226+
self.conf.set("Performance", "ffmpeg_warning", "false")
2227+
with open("config.ini", "w") as config_file: # type: TextIOWrapper
2228+
self.conf.write(config_file)
21952229

2196-
self.ui.settings_radio_threading_mode_ffmpeg.setDisabled(True)
2197-
2198-
else:
2199-
# If ffmpeg binary is found in the current directory, set it as the ffmpeg path
2200-
ffmpeg_path = os.path.abspath(ffmpeg_binary)
2230+
self.ui.settings_radio_threading_mode_ffmpeg.setDisabled(True)
2231+
self.ffmpeg_path = None
22012232

22022233
else:
2203-
# If ffmpeg is found in system PATH, use it directly
2204-
ffmpeg_path = shutil.which("ffmpeg")
22052234
consts.FFMPEG_EXECUTABLE = ffmpeg_path
22062235
bs_consts.FFMPEG_PATH = ffmpeg_path
2207-
logger.debug(f"FFMPEG: {ffmpeg_path}")
2236+
self.ffmpeg_path = ffmpeg_path
2237+
logger.debug(f"FFmpeg found at: {ffmpeg_path}")
22082238

22092239
def download_ffmpeg(self):
22102240
if sys.platform == "linux":

src/backend/shared_functions.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
as they are indeed needed for the main applications!
3434
"""
3535

36+
# TODO: Implement logging
3637
sections = ["Setup", "Performance", "PostProcessing", "Video", "UI"]
3738

3839
options_setup = ["license_accepted", "install", "update_checks", "internet_checks", "anonymous_mode", "tor"]
@@ -115,7 +116,7 @@ def send_error_log(message):
115116

116117
def check_video(url, is_url=True, delay=False):
117118
if is_url:
118-
119+
print(type(url))
119120
if hqporner_pattern.search(str(url)):
120121
return hq_Client().get_video(url)
121122

@@ -287,14 +288,14 @@ def load_video_attributes(video, data_mode):
287288
return data
288289

289290

290-
def write_tags(path, data):
291+
def write_tags(path, data: dict):
291292
comment = "Downloaded with Porn Fetch (GPLv3)"
292293
genre = "Porn"
293294

294-
title = data[0]
295-
artist = data[1]
296-
date = data[3]
297-
thumbnail = data[5]
295+
title = data.get("title")
296+
artist = data.get("author")
297+
date = data.get("publish_date")
298+
thumbnail = data.get("thumbnail")
298299
logging.debug("Tags [1/3]")
299300

300301
audio = MP4(path)

0 commit comments

Comments
 (0)