Skip to content

Commit

Permalink
Now all methods use video objects. Updated to HQPorner V1.1, model fu…
Browse files Browse the repository at this point in the history
…nction now also works for HQPorner

A lot of the time I reinitialized a Client / Video object again and again because I was to lazy to write a good API for HQPorner. Now where this is done, I can handle video objects a lot better across the UI which gives a good impact for requests usage and makes it more stable.
  • Loading branch information
EchterAlsFake committed Dec 30, 2023
1 parent 7503f78 commit 5ad6527
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 102 deletions.
189 changes: 99 additions & 90 deletions Porn_Fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Version 3.0
"""

import re
import sys
import os.path
import time
Expand All @@ -18,10 +18,10 @@
from colorama import Fore
from requests.exceptions import SSLError
from datetime import datetime
from hqporner_api.api import Client as hq_Client, Quality as hq_Quality
from hqporner_api.api import Client as hq_Client, Quality as hq_Quality, Video as hq_Video
from configparser import ConfigParser
from hue_shift import return_color, reset
from phub import Quality, Client, errors, download, Video, HTMLQuery
from phub import Quality, Client, errors, download, Video

from src.backend.shared_functions import (strip_title, check_video, check_if_video_exists, setup_config_file,
logger_debug, correct_output_path)
Expand All @@ -37,7 +37,7 @@
total_segments = 0
downloaded_segments = 0

__version__ = "beta-3.0"
__version__ = "3.0"


def ui_popup(text):
Expand Down Expand Up @@ -72,7 +72,7 @@ class DownloadProgressSignal(QObject):

class QTreeWidgetSignal(QObject):
"""Signals needed across the QTreeWidget"""
progress = Signal(str)
progress = Signal(object)
get_total = Signal(str, Quality)
start_undefined_range = Signal()
stop_undefined_range = Signal()
Expand Down Expand Up @@ -110,27 +110,30 @@ def run(self):
try:
total = len(self.iterator)

except IndexError:
except Exception:
logger_debug("Can't get length of the iterator. Progress won't be available!")
total = None

for i, video in enumerate(self.iterator, start=1):
if str(video).endswith(".html"):
title = hq_Client().get_video(str(video)).video_title
if isinstance(video, hq_Video):
title = video.video_title
if self.data_mode == 1:
duration = hq_Client().get_video(str(video)).video_length
author = hq_Client().get_video(str(video)).pornstars
author = "".join(author)
duration = int(video.video_length) / 60
pornstars = video.pornstars
if len(pornstars) == 0:
author = "author_not_found"

else:
author = pornstars[0]

else:
duration = disabled
author = disabled

text_data = [str(title), str(author), str(duration), str(i), str(video)]
logger_debug(f"EMITTED VIDEO TYPE: {type(video)}")
text_data = [str(title), str(author), str(duration), str(i), video]

else:
title = video.title
url = video.url
if self.data_mode == 1:
duration = round(video.duration.seconds / 60)
author = video.author.name
Expand All @@ -139,7 +142,8 @@ def run(self):
duration = disabled
author = disabled

text_data = [str(title), str(author), str(duration), str(i), str(url)]
logger_debug(f"EMITTED VIDEO TYPE: {type(video)}")
text_data = [str(title), str(author), str(duration), str(i), video]

self.signals.progress.emit(total, i)
self.signals.text_data.emit(text_data)
Expand Down Expand Up @@ -193,8 +197,8 @@ def run(self):
display=self.callback)

else:
hq_Client().get_video(url=str(self.video)).download(quality=hq_Quality.BEST, output_path=self.output_path, callback=self.callback_hqporner,
no_title=True)
self.video.download(quality=hq_Quality.BEST, output_path=self.output_path,
callback=self.callback_hqporner, no_title=True)

finally:
self.signals_completed.completed.emit()
Expand All @@ -212,35 +216,33 @@ def __init__(self, treeWidget, semaphore, quality):

def run(self):
self.signals.start_undefined_range.emit()
video_urls = []
video_objects = []
video_urls_hqporner = []
video_objects_hqporner = []
video_objects_pornhub = []

for i in range(self.treeWidget.topLevelItemCount()):
item = self.treeWidget.topLevelItem(i)
checkState = item.checkState(0)
if checkState == Qt.Checked:
video_urls.append(item.data(0, Qt.UserRole))
object_ = item.data(0, Qt.UserRole)
print(f"Object Type: ---- : {type(object_)}")
if isinstance(object_, hq_Video):
video_objects_hqporner.append(object_)

global total_segments, downloaded_segments
for url in video_urls:
if str(url).endswith(".html"):
video_urls_hqporner.append(url)

else:
video_objects.append(
check_video(url, language="en")) # Not used for downloading, so language doesn't matter
elif isinstance(object_, Video):
video_objects_pornhub.append(object_)

global total_segments, downloaded_segments
total_segments = sum(
[len(list(video.get_segments(quality=self.quality))) for video in video_objects])
[len(list(video.get_segments(quality=self.quality))) for video in video_objects_pornhub])

downloaded_segments = 0

self.signals.stop_undefined_range.emit()
for video_url in video_urls:
logger_debug(f"Downloading: {video_url}")
videos = video_objects_pornhub + video_objects_hqporner
for video in videos:
self.semaphore.acquire()
logger_debug("Semaphore Acquired")
self.signals.progress.emit(video_url)
self.signals.progress.emit(video)


class MetadataVideos(QRunnable):
Expand Down Expand Up @@ -845,12 +847,13 @@ def add_to_tree_widget_signal(self, data):
duration = data[2]
index = data[3]
video = data[4]
print(f"Received Video: {type(video)}")

item = QTreeWidgetItem(self.ui.treeWidget)
item.setText(0, f"{index}) {title}")
item.setText(1, author)
item.setText(2, str(duration))
item.setData(0, Qt.UserRole, str(video))
item.setData(0, Qt.UserRole, video)
item.setCheckState(0, Qt.Unchecked) # Adds a checkbox

def download_tree_widget(self):
Expand All @@ -863,9 +866,8 @@ def download_tree_widget(self):
download_tree_thread.signals.stop_undefined_range.connect(self.stop_undefined_range)
self.threadpool.start(download_tree_thread)

def tree_widget_completed(self, url):
print("Connected")
self.load_video(url)
def tree_widget_completed(self, video):
self.load_video(video)

def unselect_all_items(self):
root = self.ui.treeWidget.invisibleRootItem()
Expand Down Expand Up @@ -933,25 +935,42 @@ def start_single_video(self):
api_language = self.api_language
one_time_iterator = []
if url.endswith(".html"):
one_time_iterator.append(url)
one_time_iterator.append(check_video(url=url, language=api_language))

else:
one_time_iterator.append(check_video(url=url, language=api_language))

self.add_to_tree_widget_thread(iterator=one_time_iterator, search_limit=self.search_limit)

def start_model(self):
model = self.ui.lineedit_model_url.text()
api_language = self.api_language
search_limit = self.search_limit
if not isinstance(self.client, Client):
client = Client(language=api_language)
model = self.ui.lineedit_model_url.text()

actress_pattern = re.compile(r"https://hqporner\.com/actress/(.+)")
pornhub_pattern = re.compile(r"(.*?)pornhub(.*?)")

match = actress_pattern.match(model)
if match:
# Extract the actress name from the URL
model = match.group(1)
videos = hq_Client().get_videos_by_actress(name=model)

elif pornhub_pattern.match(model):
api_language = self.api_language
if not isinstance(self.client, Client):
client = Client(language=api_language)
else:
client = self.client

model_object = client.get_user(model)
videos = model_object.videos

elif not model.startswith("https://"):
videos = hq_Client().get_videos_by_actress(name=model)

else:
client = self.client
return

model_object = client.get_user(model)
videos = model_object.videos
self.add_to_tree_widget_thread(videos, search_limit=search_limit)

def load_video(self, url):
Expand All @@ -961,60 +980,50 @@ def load_video(self, url):
threading_mode = self.threading_mode
directory_system = self.directory_system
quality = self.quality

if str(url).endswith(".html"):
video = url

if not isinstance(url, Video):
video = check_video(url, language=api_language)

if not isinstance(url, Video) and not isinstance(url, str):
text = QCoreApplication.tr("Video is not from PornHub or HQPorner!")
ui_popup(text)

else:
output_path = correct_output_path(output_path)

if str(url).endswith(".html"):
title = hq_Client().get_video(str(url)).video_title
video = check_video(url, language=api_language)
if isinstance(video, hq_Video):
title = video.video_title
pornstars = video.pornstars
if len(pornstars) == 0:
author = "no_author_found"

else:
title = video.title

stripped_title = strip_title(title)
logger_debug(f"Loading Video: {stripped_title}")
author = pornstars[0]

if directory_system:
if str(url).endswith(".html"):
author = hq_Client().get_video(str(url)).pornstars[0]
elif isinstance(video, Video):
video = check_video(url, language=api_language)
title = video.title
author = video.author.name

else:
author = video.author.name
output_path = correct_output_path(output_path)
stripped_title = strip_title(title)
logger_debug(f"Loading Video: {stripped_title}")

if not os.path.exists(f"{output_path}{author}"):
os.mkdir(output_path + author)
if directory_system:
if not os.path.exists(f"{output_path}{author}"):
os.mkdir(output_path + author)

output_path = f"{output_path}{author}{os.sep}{stripped_title}.mp4"
output_path.strip("'")
output_path = f"{output_path}{author}{os.sep}{stripped_title}.mp4"
output_path.strip("'")

else:
output_path = f"{output_path}{stripped_title}.mp4"
output_path.strip("'")
else:
output_path = f"{output_path}{stripped_title}.mp4"
output_path.strip("'")

if not check_if_video_exists(video, output_path):
if self.threading:
logger_debug("Processing Thread")
self.process_video_thread(output_path=output_path, video=video, threading_mode=threading_mode,
quality=quality)
if not check_if_video_exists(video, output_path):
if self.threading:
logger_debug("Processing Thread")
self.process_video_thread(output_path=output_path, video=video, threading_mode=threading_mode,
quality=quality)

elif not self.threading:
self.process_video_without_thread(output_path, video, quality)
elif not self.threading:
self.process_video_without_thread(output_path, video, quality)

else:
self.semaphore.release()
if not isinstance(video, str):
global downloaded_segments
downloaded_segments += len(list(video.get_segments(quality=quality)))
else:
self.semaphore.release()
if not isinstance(video, hq_Video):
global downloaded_segments
downloaded_segments += len(list(video.get_segments(quality=quality)))

def return_client(self):
if isinstance(self.client, Client):
Expand Down Expand Up @@ -1164,7 +1173,7 @@ def basic_search(self):
language = self.api_language
search_limit = self.search_limit
client = Client(language=language)
search = client.search(query, feature=HTMLQuery)
search = client.search(query, use_hubtraffic=True)
self.add_to_tree_widget_thread(search, search_limit=search_limit)

def search_pornstars(self):
Expand Down
11 changes: 7 additions & 4 deletions src/backend/shared_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import datetime
from pymediainfo import MediaInfo
from configparser import ConfigParser
from hqporner_api.api import Client
from hqporner_api.api import Client as hq_Client, Video as hq_Video

"""
The following are the sections and options for the configuration file. Please don't change anything here,
Expand Down Expand Up @@ -78,13 +78,16 @@ def logger_debug(e):

def check_video(url, language):
if str(url).endswith(".html"):
return url
return hq_Client().get_video(url)

else:
if isinstance(url, Video):
return url

if isinstance(url, str):
if isinstance(url, hq_Video):
return url

if isinstance(url, str) and not str(url).endswith(".html"):
try:
return Client(language=language).get(url)

Expand Down Expand Up @@ -139,7 +142,7 @@ def check_if_video_exists(video, output_path):
media_info = MediaInfo.parse(output_path)

if str(video).endswith(".html"):
video_duration = Client().get_video(str(video)).video_length
video_duration = hq_Client().get_video(str(video)).video_length
video_duration = convert_to_seconds(video_duration)

else:
Expand Down
Loading

0 comments on commit 5ad6527

Please sign in to comment.