Skip to content

Commit

Permalink
Merge branch 'verified-bot-edition-dev' into verified-bot-edition
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardozgz committed Dec 20, 2023
2 parents 8dad7ba + e2c2db6 commit 81a94c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/counters/YouTube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { YOUTUBE_API_KEY } = getEnv();

import Counter from "../typings/Counter";

const anyChannelMatch = /^((http|https):\/\/|)(www\.|m\.)?youtube\.com\/(channel|user|c)\//;
const usernameChannelMatch = /^((http|https):\/\/|)(www\.|m\.)?youtube\.com\/(user|c)\//;
const legacyUsernameChannelMatch = /^((http|https):\/\/|)(www\.|m\.)?youtube\.com\/(user|c)\//;
const handleUsernameChannelMatch = /^((http|https):\/\/|)(www\.|m\.)?youtube\.com\/\@/;
const idChannelMatch = /^((http|https):\/\/|)(www\.|m\.)?youtube\.com\/channel\//;

const YouTubeCounter: Counter = {
Expand All @@ -20,13 +20,20 @@ const YouTubeCounter: Counter = {
lifetime: 60 * 60 * 1000,
execute: async ({ unparsedArgs: channelUrl }) => {
if (!YOUTUBE_API_KEY) throw new Error("YOUTUBE_API_KEY not provided");
let channel = channelUrl.replace(anyChannelMatch, "");
let searchChannelBy = "";

if (usernameChannelMatch.test(channelUrl)) {
let channel = "";
let searchChannelBy = "id";

if (legacyUsernameChannelMatch.test(channelUrl)) {
searchChannelBy = "forUsername";
channel = channelUrl.replace(legacyUsernameChannelMatch, "");
} else if (handleUsernameChannelMatch.test(channelUrl)) {
const getId = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=${channelUrl.replace(handleUsernameChannelMatch, "")}&type=channel&key=${YOUTUBE_API_KEY}`
).then((response) => response.json());
channel = getId.items?.[0]?.id.channelId;
} else if (idChannelMatch.test(channelUrl)) {
searchChannelBy = "id";
channel = channelUrl.replace(idChannelMatch, "")
} else {
throw new Error(`Invalid youtube channel url: ${channelUrl}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lang/tr_TR.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@
"redditMembersOnline": {
"name": "redditMembersOnline:subreddit",
"description": "Bir subredditin aktif üye sayısını gösterir",
"detailedDescription": "Bir subredditin üye sayısını sayar, `subreddit` bölümünü istediğiniz subreddit ile değiştirin",
"detailedDescription": "Bir subredditin üye sayısını gösterir, `subreddit` bölümünü istediğiniz subreddit ile değiştirin",
"usage": [
"{redditMembersOnline:subreddit}"
],
Expand Down

0 comments on commit 81a94c2

Please sign in to comment.