Update xham.py#70
Conversation
API UPDATED !!
Reviewer's GuideSwitches the XhamResolver from the EasyDownloader POST API to the vidquickly GET API, simplifies canonical URL handling, and reworks link selection to choose the highest‑quality MP4 stream with a generated filename based on video metadata. Sequence diagram for XhamResolver resolve flow using vidquickly GET APIsequenceDiagram
actor Client
participant XhamResolver
participant vidquicklyAPI
Client->>XhamResolver: resolve(url)
XhamResolver->>XhamResolver: _normalize_to_canonical(url)
XhamResolver->>vidquicklyAPI: GET API_URL + canonical_url
alt HTTP status 200
vidquicklyAPI-->>XhamResolver: JSON links[], videoDetails
XhamResolver->>XhamResolver: _extract_quality(link.title) for each link
XhamResolver->>XhamResolver: select best quality link
XhamResolver->>XhamResolver: build filename from videoDetails.title or link_url
XhamResolver-->>Client: LinkResult
else Non 200 or parse error or no links
vidquicklyAPI-->>XhamResolver: error or invalid JSON
XhamResolver->>Client: raise ExtractionFailedException
end
Updated class diagram for XhamResolver vidquickly migrationclassDiagram
class BaseResolver
class ExtractionFailedException
class LinkResult {
+str url
+str filename
+str mime_type
+int? size
}
class FolderResult
class XhamResolver {
+list~str~ DOMAINS
+str API_URL
+str CANONICAL_HOST
+str _normalize_to_canonical(original_url)
+int _extract_quality(title)
+LinkResult|FolderResult resolve(url)
}
XhamResolver --|> BaseResolver
XhamResolver ..> ExtractionFailedException
XhamResolver ..> LinkResult
XhamResolver ..> FolderResult
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary of ChangesHello @PandazNetwork, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request updates the Xhamster resolver to adapt to a new API, ensuring continued functionality for extracting video links. The changes involve switching to a different API endpoint, refining the link selection process to prioritize higher quality, and improving how filenames are generated from the extracted content. This update is crucial for maintaining the resolver's ability to process xhamster variant URLs effectively. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Hey - I've found 4 issues, and left some high level feedback:
- When building
api_urlby simple string concatenation (self.API_URL + canonical_url), the original URL is not URL-encoded, which can lead to malformed requests if it contains?,&, or other reserved characters; consider usingurllib.parse.quoteorurlencodeto safely embed the URL in the query string. - The
_extract_qualityhelper currently grabs the first number in the title regardless of context, which may mis-rank links if the title contains other numbers (e.g., years or durations); consider tightening the pattern to something liker'(\d{3,4})p\b'or verifying the number is actually a resolution. - The broad
except Exception as eat the end ofresolvewill catch and wrap existingExtractionFailedExceptions, duplicating messages and obscuring their original meaning; consider either not catchingExtractionFailedExceptionor re-raising it unchanged and only wrapping unexpected exception types.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- When building `api_url` by simple string concatenation (`self.API_URL + canonical_url`), the original URL is not URL-encoded, which can lead to malformed requests if it contains `?`, `&`, or other reserved characters; consider using `urllib.parse.quote` or `urlencode` to safely embed the URL in the query string.
- The `_extract_quality` helper currently grabs the first number in the title regardless of context, which may mis-rank links if the title contains other numbers (e.g., years or durations); consider tightening the pattern to something like `r'(\d{3,4})p\b'` or verifying the number is actually a resolution.
- The broad `except Exception as e` at the end of `resolve` will catch and wrap existing `ExtractionFailedException`s, duplicating messages and obscuring their original meaning; consider either not catching `ExtractionFailedException` or re-raising it unchanged and only wrapping unexpected exception types.
## Individual Comments
### Comment 1
<location> `src/truelink/resolvers/xham.py:61` </location>
<code_context>
- }
+ canonical_url = self._normalize_to_canonical(url)
+
+ api_url = self.API_URL + canonical_url
try:
</code_context>
<issue_to_address>
**issue (bug_risk):** Canonical URL should be URL-encoded before being appended to the API endpoint.
If `canonical_url` contains `&`, `?`, or other reserved characters, the API call will be malformed and parts of the URL may be treated as extra query parameters. Build the URL with proper encoding instead (e.g., `params={'url': canonical_url}` with `urlencode` / `requests`, or `urllib.parse.quote`), rather than concatenating the string directly.
</issue_to_address>
### Comment 2
<location> `src/truelink/resolvers/xham.py:79-82` </location>
<code_context>
+ links = data.get("links", [])
</code_context>
<issue_to_address>
**suggestion:** Error message claims no MP4 links, but the code never filters by MIME/extension.
The exception message asserts MP4 links specifically, but the code only checks whether `links` is non-empty, without validating that they are MP4s. This can mislead debugging when non-MP4 formats are returned. Either enforce MP4 selection (e.g., via URL or a `type` field) or change the message to reflect the actual check (e.g., “No links found”).
</issue_to_address>
### Comment 3
<location> `src/truelink/resolvers/xham.py:53-56` </location>
<code_context>
+
+ return original_url
+
+ def _extract_quality(self, title: str) -> int:
+ """Extract numeric quality from title like 'Video 720p'."""
+ match = re.search(r"(\d+)", title or "")
+ return int(match.group(1)) if match else 0
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Quality extraction may pick unintended numbers (e.g. bitrates) and mis-rank links.
`re.search(r"(\d+)")` will return the first number in the title, which could be a duration, bitrate (e.g. `128kbps`), or other non-resolution value. That can cause incorrect ranking if a non-resolution number is larger than the actual resolution. If titles consistently encode resolution as `720p`, `1080p`, etc., consider a stricter pattern like `r"(\d{3,4})p"`, or otherwise choose the most likely resolution token (e.g. the last numeric token) instead of the first match.
```suggestion
def _extract_quality(self, title: str) -> int:
"""Extract numeric quality from title like 'Video 720p'.
Prefer explicit resolution patterns like '720p' or '1080p', and as a
fallback, choose the last 3–4 digit numeric token which is most likely
to represent the resolution rather than e.g. duration or bitrate.
"""
if not title:
return 0
lowered = title.lower()
# First, look for patterns like '720p', '1080p', etc.
match = re.search(r"(\d{3,4})p\b", lowered)
if match:
return int(match.group(1))
# Fallback: pick the last standalone 3–4 digit number token.
candidates = re.findall(r"\b(\d{3,4})\b", lowered)
if candidates:
return int(candidates[-1])
return 0
```
</issue_to_address>
### Comment 4
<location> `src/truelink/resolvers/xham.py:99-105` </location>
<code_context>
+ safe_title = re.sub(r"[^\w\-_. ]", "", title)
+ filename = f"{safe_title}.mp4"
+ else:
+ filename = basename(urlparse(link_url).path)
+
+ mime_type = "video/mp4"
</code_context>
<issue_to_address>
**suggestion:** Filename derivation from URL may yield an empty string for trailing-slash URLs.
For URLs with a trailing slash or no path, `basename(urlparse(link_url).path)` returns an empty string, which is then used as the filename. Consider falling back to `None` or a default name when this is empty so consumers don’t receive an invalid filename.
```suggestion
if title:
safe_title = re.sub(r"[^\w\-_. ]", "", title)
filename = f"{safe_title}.mp4"
else:
# Derive filename from URL path; fall back to None for empty or trailing-slash URLs
url_path = urlparse(link_url).path
derived_filename = basename(url_path)
filename = derived_filename or None
mime_type = "video/mp4"
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request updates the XhamResolver to use the new vidquickly API, which is a great improvement. The changes simplify the implementation, remove a hardcoded API key, and introduce a more robust method for selecting the best video quality. The code is cleaner and more efficient.
I've added a couple of suggestions to further improve the code's maintainability and error handling structure. Specifically, I've pointed out an opportunity to define a constant at the class level to avoid recreation, and a way to refine the exception handling to avoid re-wrapping exceptions and produce clearer error messages.
API UPDATED !!
Summary by Sourcery
Switch XhamResolver to use the vidquickly API with xhamster1.desi as the canonical host and update link selection and error handling accordingly.
Enhancements: