refactor: Update author URL decoding in subscriptionService

This commit is contained in:
Peifan Li
2025-12-23 16:53:47 -05:00
parent c6b2417e5b
commit 9f2dd1af39
4 changed files with 278 additions and 30 deletions

149
.codacy/cli.sh Executable file
View File

@@ -0,0 +1,149 @@
#!/usr/bin/env bash
set -e +o pipefail
# Set up paths first
bin_name="codacy-cli-v2"
# Determine OS-specific paths
os_name=$(uname)
arch=$(uname -m)
case "$arch" in
"x86_64")
arch="amd64"
;;
"x86")
arch="386"
;;
"aarch64"|"arm64")
arch="arm64"
;;
esac
if [ -z "$CODACY_CLI_V2_TMP_FOLDER" ]; then
if [ "$(uname)" = "Linux" ]; then
CODACY_CLI_V2_TMP_FOLDER="$HOME/.cache/codacy/codacy-cli-v2"
elif [ "$(uname)" = "Darwin" ]; then
CODACY_CLI_V2_TMP_FOLDER="$HOME/Library/Caches/Codacy/codacy-cli-v2"
else
CODACY_CLI_V2_TMP_FOLDER=".codacy-cli-v2"
fi
fi
version_file="$CODACY_CLI_V2_TMP_FOLDER/version.yaml"
get_version_from_yaml() {
if [ -f "$version_file" ]; then
local version=$(grep -o 'version: *"[^"]*"' "$version_file" | cut -d'"' -f2)
if [ -n "$version" ]; then
echo "$version"
return 0
fi
fi
return 1
}
get_latest_version() {
local response
if [ -n "$GH_TOKEN" ]; then
response=$(curl -Lq --header "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null)
else
response=$(curl -Lq "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null)
fi
handle_rate_limit "$response"
local version=$(echo "$response" | grep -m 1 tag_name | cut -d'"' -f4)
echo "$version"
}
handle_rate_limit() {
local response="$1"
if echo "$response" | grep -q "API rate limit exceeded"; then
fatal "Error: GitHub API rate limit exceeded. Please try again later"
fi
}
download_file() {
local url="$1"
echo "Downloading from URL: ${url}"
if command -v curl > /dev/null 2>&1; then
curl -# -LS "$url" -O
elif command -v wget > /dev/null 2>&1; then
wget "$url"
else
fatal "Error: Could not find curl or wget, please install one."
fi
}
download() {
local url="$1"
local output_folder="$2"
( cd "$output_folder" && download_file "$url" )
}
download_cli() {
# OS name lower case
suffix=$(echo "$os_name" | tr '[:upper:]' '[:lower:]')
local bin_folder="$1"
local bin_path="$2"
local version="$3"
if [ ! -f "$bin_path" ]; then
echo "📥 Downloading CLI version $version..."
remote_file="codacy-cli-v2_${version}_${suffix}_${arch}.tar.gz"
url="https://github.com/codacy/codacy-cli-v2/releases/download/${version}/${remote_file}"
download "$url" "$bin_folder"
tar xzfv "${bin_folder}/${remote_file}" -C "${bin_folder}"
fi
}
# Warn if CODACY_CLI_V2_VERSION is set and update is requested
if [ -n "$CODACY_CLI_V2_VERSION" ] && [ "$1" = "update" ]; then
echo "⚠️ Warning: Performing update with forced version $CODACY_CLI_V2_VERSION"
echo " Unset CODACY_CLI_V2_VERSION to use the latest version"
fi
# Ensure version.yaml exists and is up to date
if [ ! -f "$version_file" ] || [ "$1" = "update" ]; then
echo " Fetching latest version..."
version=$(get_latest_version)
mkdir -p "$CODACY_CLI_V2_TMP_FOLDER"
echo "version: \"$version\"" > "$version_file"
fi
# Set the version to use
if [ -n "$CODACY_CLI_V2_VERSION" ]; then
version="$CODACY_CLI_V2_VERSION"
else
version=$(get_version_from_yaml)
fi
# Set up version-specific paths
bin_folder="${CODACY_CLI_V2_TMP_FOLDER}/${version}"
mkdir -p "$bin_folder"
bin_path="$bin_folder"/"$bin_name"
# Download the tool if not already installed
download_cli "$bin_folder" "$bin_path" "$version"
chmod +x "$bin_path"
run_command="$bin_path"
if [ -z "$run_command" ]; then
fatal "Codacy cli v2 binary could not be found."
fi
if [ "$#" -eq 1 ] && [ "$1" = "download" ]; then
echo "Codacy cli v2 download succeeded"
else
eval "$run_command $*"
fi

View File

@@ -143,7 +143,7 @@ export class SubscriptionService {
authorName === "Unknown Author" ||
authorName === providedAuthorName
) {
const match = authorUrl.match(/youtube\.com\/(@[^\/]+)/);
const match = decodeURI(authorUrl).match(/youtube\.com\/(@[^\/]+)/);
if (match && match[1]) {
authorName = match[1];
} else {
@@ -164,7 +164,7 @@ export class SubscriptionService {
} catch (error) {
logger.error("Error fetching YouTube channel info:", error);
// Fallback: try to extract from URL
const match = authorUrl.match(/youtube\.com\/(@[^\/]+)/);
const match = decodeURI(authorUrl).match(/youtube\.com\/(@[^\/]+)/);
if (match && match[1]) {
authorName = match[1];
} else {
@@ -303,7 +303,30 @@ export class SubscriptionService {
if (latestVideoUrl && latestVideoUrl !== sub.lastVideoLink) {
console.log(`New video found for ${sub.author}: ${latestVideoUrl}`);
// 2. Download the video based on platform
// 2. Update lastCheck *before* download to prevent concurrent processing
// Re-verify subscription exists before updating
const subscriptionStillExists = await db
.select()
.from(subscriptions)
.where(eq(subscriptions.id, sub.id))
.limit(1);
if (subscriptionStillExists.length === 0) {
logger.warn(
`Subscription ${sub.id} (${sub.author}) was deleted during processing, skipping update`
);
continue;
}
// Update lastCheck immediately to lock this subscription for this interval
await db
.update(subscriptions)
.set({
lastCheck: now,
})
.where(eq(subscriptions.id, sub.id));
// 3. Download the video
let downloadResult: any;
try {
if (sub.platform === "Bilibili") {
@@ -331,6 +354,40 @@ export class SubscriptionService {
thumbnailPath: videoData.thumbnailPath,
videoId: videoData.id,
});
// 4. Update subscription record with new video link and stats on success
// Re-verify subscription exists before final update (race condition protection)
const subscriptionStillExistsAfterDownload = await db
.select()
.from(subscriptions)
.where(eq(subscriptions.id, sub.id))
.limit(1);
if (subscriptionStillExistsAfterDownload.length === 0) {
logger.warn(
`Subscription ${sub.id} (${sub.author}) was deleted after download completed, skipping final update`
);
continue;
}
const updateResult = await db
.update(subscriptions)
.set({
lastVideoLink: latestVideoUrl,
downloadCount: (sub.downloadCount || 0) + 1,
})
.where(eq(subscriptions.id, sub.id))
.returning();
if (updateResult.length === 0) {
logger.error(
`Failed to update subscription ${sub.id} (${sub.author}) after successful download - no rows affected`
);
} else {
logger.debug(
`Successfully processed subscription ${sub.id} (${sub.author})`
);
}
} catch (downloadError: any) {
console.error(
`Error downloading subscription video for ${sub.author}:`,
@@ -348,29 +405,36 @@ export class SubscriptionService {
error: downloadError.message || "Download failed",
});
// Don't update lastVideoLink on failure so we retry next time
await db
.update(subscriptions)
.set({ lastCheck: now })
.where(eq(subscriptions.id, sub.id));
// Note: We already updated lastCheck, so we won't retry until next interval.
// This acts as a "backoff" preventing retry loops for broken downloads.
}
} else {
// Just update lastCheck
// Re-verify subscription exists before updating (race condition protection)
const subscriptionStillExists = await db
.select()
.from(subscriptions)
.where(eq(subscriptions.id, sub.id))
.limit(1);
if (subscriptionStillExists.length === 0) {
logger.debug(
`Subscription ${sub.id} (${sub.author}) was deleted during check, skipping update`
);
continue;
}
// 3. Update subscription record
await db
.update(subscriptions)
.set({
lastVideoLink: latestVideoUrl,
lastCheck: now,
downloadCount: (sub.downloadCount || 0) + 1,
})
.where(eq(subscriptions.id, sub.id));
} else {
// Just update lastCheck
await db
const updateResult = await db
.update(subscriptions)
.set({ lastCheck: now })
.where(eq(subscriptions.id, sub.id));
.where(eq(subscriptions.id, sub.id))
.returning();
if (updateResult.length === 0) {
logger.warn(
`Failed to update lastCheck for subscription ${sub.id} (${sub.author}) - no rows affected`
);
}
}
} catch (error) {
console.error(

View File

@@ -88,7 +88,7 @@ const SearchInput: React.FC<SearchInputProps> = ({
) : null,
endAdornment: (
<InputAdornment position="end">
{isSearchMode && searchTerm && (
{isSearchMode && searchTerm && videoUrl && (
<IconButton onClick={onResetSearch} edge="end" size="small" sx={{ mr: 0.5 }}>
<Clear />
</IconButton>

View File

@@ -184,20 +184,55 @@ const VideoPlayer: React.FC = () => {
// Check if author is subscribed
const isSubscribed = useMemo(() => {
if (!authorChannelUrl || !subscriptions || subscriptions.length === 0) {
if (!subscriptions || subscriptions.length === 0) {
return false;
}
return subscriptions.some((sub: any) => sub.authorUrl === authorChannelUrl);
}, [authorChannelUrl, subscriptions]);
// 1. Strict check by Channel URL (most accurate)
if (authorChannelUrl) {
const hasUrlMatch = subscriptions.some((sub: any) => sub.authorUrl === authorChannelUrl);
if (hasUrlMatch) return true;
}
// 2. Fallback check by Author Name and Platform matching
// This handles cases where we might have the same author but slightly different URLs (e.g. handle vs channel ID)
if (video) {
return subscriptions.some((sub: any) => {
const nameMatch = sub.author === video.author;
// sub.platform is typically "YouTube" or "Bilibili" (capitalized)
// video.source is typically "youtube" or "bilibili" (lowercase)
const platformMatch = sub.platform?.toLowerCase() === video.source?.toLowerCase();
return nameMatch && platformMatch;
});
}
return false;
}, [authorChannelUrl, subscriptions, video]);
// Get subscription ID if subscribed
const subscriptionId = useMemo(() => {
if (!authorChannelUrl || !subscriptions || subscriptions.length === 0) {
if (!subscriptions || subscriptions.length === 0) {
return null;
}
const subscription = subscriptions.find((sub: any) => sub.authorUrl === authorChannelUrl);
return subscription?.id || null;
}, [authorChannelUrl, subscriptions]);
// 1. Strict check by Channel URL
if (authorChannelUrl) {
const subscription = subscriptions.find((sub: any) => sub.authorUrl === authorChannelUrl);
if (subscription) return subscription.id;
}
// 2. Fallback check by Author Name and Platform matching
if (video) {
const subscription = subscriptions.find((sub: any) => {
const nameMatch = sub.author === video.author;
const platformMatch = sub.platform?.toLowerCase() === video.source?.toLowerCase();
return nameMatch && platformMatch;
});
if (subscription) return subscription.id;
}
return null;
}, [authorChannelUrl, subscriptions, video]);
// Handle navigation to author videos page or external channel
const handleAuthorClick = async () => {