બ્લોગ / ડેવલપર ગાઇડ
સોશિયલ મીડિયા પ્રોફાઇલ URL કેવી રીતે parse અને નૉર્મલાઇઝ કરવાં (Instagram, TikTok, YouTube, X)
ક્રિએટર પોતાની પ્રોફાઇલ લિંક દરેક કલ્પી શકાય તેવા રૂપમાં પેસ્ટ કરે છે: www સાથે અને વિના, ટ્રૅકિંગ પેરામીટરવાળી મોબાઇલ એપમાંથી, short link તરીકે, જૂના twitter.com URL તરીકે, કે પ્રોફાઇલને બદલે પોસ્ટની લિંક તરીકે. જો તમારી પ્રોડક્ટ આ strings જેવી આવે તેવી જ રાખે, તો એક જ ક્રિએટર ત્રણ વાર દેખાય છે અને તમારા joins ચૂપચાપ ચૂકી જાય છે. આ માર્ગદર્શિકા બતાવે છે કે કોઈ પણ Instagram, TikTok, YouTube કે X પ્રોફાઇલ URL ને પ્લેટફોર્મ, હેન્ડલ કે channel ID, અને એક canonical URL માં કેવી રીતે ફેરવવું, JavaScript અને Python માં ચકાસાયેલા કોડ સાથે.
પ્રોફાઇલ URL નૉર્મલાઇઝ કરવાં કેમ મહત્વનાં છે
પ્રોફાઇલ URL એક identifier છે, અને identifier ત્યારે જ કામ કરે જ્યારે તે એક જ રીતે લખાય. https://www.instagram.com/natgeo/, instagram.com/NatGeo?igsh=MWQ1ZGUxMzBkMA== અને https://m.instagram.com/natgeo બધાં એક જ એકાઉન્ટ તરફ જાય છે, પણ strings તરીકે તે ત્રણ જુદાં મૂલ્યો છે.
- Deduplication. જ્યારે કોઈ ક્રિએટર બે વાર sign up કરે, કે ટીમના બે સભ્યો એક જ ઇન્ફ્લુએન્સરને કૅમ્પેનમાં ઉમેરે, ત્યારે તમને બે નહીં, એક રેકોર્ડ જોઈએ.
- Joins. તમારા CRM ને બીજા ટૂલના export સાથે મેળવવાનું ત્યારે જ કામ કરે જ્યારે બંને બાજુ એક જ key વાપરે.
- ખરાબ ઇનપુટ વહેલા નકારવું. Reel, search પેજ કે login screen ની લિંક પ્રોફાઇલ નથી. ઇનપુટ વખતે જ પકડવું પછીથી સાફ કરવા કરતાં સસ્તું છે.
- API calls. સત્તાવાર API ને હેન્ડલ કે ID જોઈએ, ટ્રૅકિંગ પેરામીટર જોડેલું URL નહીં.
લક્ષ્ય છે એક નાનું, કડક function: તે યુઝર ખરેખર જે પેસ્ટ કરે તે સ્વીકારે, સાચાં પ્રોફાઇલ URL માટે નૉર્મલાઇઝ્ડ પરિણામ આપે, અને બાકી બધા માટે null આપે.
તમને જોવા મળશે તેવાં URL રૂપ
નીચેનાં રૂપ October 2026 માં live સાઇટ્સ સામે તપાસ્યાં હતાં. પ્લેટફોર્મ જાણ કર્યા વિના routing બદલે છે, તેથી આને એક snapshot ગણો અને તમારા test cases અપડેટ રાખો.
- પ્રોફાઇલ
https://www.instagram.com/<handle>/પર હોય છે. ખાલીinstagram.comઅનેm.instagram.comhostswww.instagram.comપર redirect થાય છે. - એપમાંથી શેર થયેલી લિંકમાં ઘણી વાર
?igsh=પેરામીટર હોય છે. આખી query string અને fragment કાઢી નાખો. - પોસ્ટ
/p/<code>/, reels/reel/<code>/, અને stories/stories/<handle>/<id>/છે. Path માં હેન્ડલ દેખાય ત્યારે પણ આમાંથી કોઈ પ્રોફાઇલ URL નથી. /explore/,/accounts/અને/direct/જેવા app routes પહેલો path segment હેન્ડલ સાથે વહેંચે છે, તેથી તમારે reserved શબ્દોની યાદી જોઈએ. નીચેના કોડમાંની યાદી સંપૂર્ણ નથી.
TikTok
- પ્રોફાઇલ
https://www.tiktok.com/@<handle>છે. ખાલીtiktok.comhostwwwપર redirect થાય છે. - વીડિયો
/@<handle>/video/<id>છે. હેન્ડલ લેખકનું છે, પણ URL વીડિયો લિંક છે, તેથી નીચેનો parser તેને નકારે છે. vm.tiktok.com,vt.tiktok.comઅનેwww.tiktok.com/t/<code>/પરની short links માં કોઈ હેન્ડલ નથી હોતું. તે ક્યાં લઈ જાય છે તે જોવા HTTP redirect અનુસરવું પડે, પછી અંતિમ URL parse કરો.@વિનાનો path (ઉદાહરણ તરીકેtiktok.com/khaby.lame) પ્રોફાઇલ URL નથી.
YouTube
- હેન્ડલ URL:
https://www.youtube.com/@<handle>. YouTube નું Help Center કહે છે કે હેન્ડલ આપમેળે ચેનલનું URL બને છે. - Channel ID URL:
https://www.youtube.com/channel/UC.... આ સ્થિર રૂપ છે. વ્યવહારમાં channel IDUCથી શરૂ થતા 24 અક્ષરનાં હોય છે. Google ફોર્મેટ દસ્તાવેજિત નથી કરતું, તેથી નીચેની તપાસ એક વાજબી સીમા છે, નિયમ નહીં. - જૂનાં રૂપ:
/user/<username>અને/c/<customname>. બંને આજે પણ ખૂલે છે, અને Help Center કહે છે કે જૂનાં વ્યક્તિગત URL કામ કરતાં રહે છે. Lookup વિના બેમાંથી કોઈ channel ID નથી કહેતું. - ખાલી
youtube.com/<name>પણ કેટલીક ચેનલ માટે હજી ખૂલે છે, પણ તે site routes સાથે namespace વહેંચે છે, તેથી નીચેનો parser તેને સ્વીકારતો નથી. /@handle/videosઅને/channel/UC.../featuredજેવાં ટૅબ ચેનલનાં જ છે અને સ્વીકારવાં સુરક્ષિત છે.m.youtube.comwww.youtube.comપર redirect થાય છે. વીડિયો લિંક (/watch?v=,/shorts/,youtu.be/<id>) ચેનલ નથી.
X (પહેલાં Twitter)
- પ્રોફાઇલ
https://x.com/<handle>છે.twitter.com,www.twitter.com,www.x.com,mobile.twitter.comઅનેmobile.x.comબધાંx.comકેtwitter.comપરની પ્રોફાઇલ પર redirect થાય છે, તેથી તેમને એક જ પ્લેટફોર્મ ગણો. - પોસ્ટ
/<handle>/status/<id>છે. TikTok વીડિયોની જેમ તે લેખકનું નામ આપે છે પણ પ્રોફાઇલ URL નથી. /home,/explore,/search,/settings,/notifications,/messagesજેવા app routes અને/i/હેઠળનું કંઈ પણ હેન્ડલ નથી.- શેર થયેલી લિંક ઘણી વાર
?s=અને?t=પેરામીટર ઉમેરે છે. તે કાઢી નાખો.
દરેક પ્લેટફોર્મ માટે હેન્ડલના અક્ષરોના નિયમો
હેન્ડલ validate કરવાથી typos અને મોટાભાગના બિન-પ્રોફાઇલ paths પકડાય છે. પ્લેટફોર્મ પોતાના નિયમો પ્રકાશિત કરે ત્યાં તે વાપરો, અને ન કરે ત્યાં તપાસ ઢીલી રાખો, જેથી તમે ક્યારેય સાચું એકાઉન્ટ નકારો નહીં.
| પ્લેટફોર્મ | મંજૂર અક્ષરો | લંબાઈ | Case | સ્રોત |
|---|---|---|---|---|
| અક્ષરો, અંકો, periods, underscores | 30 નો વ્યાપક ઉલ્લેખ થાય છે; Instagram નાં help pages માં મળ્યું નથી | Case-insensitive ગણો; નાના અક્ષરમાં રાખો | Instagram Help Center periods, અંકો અને underscores નો ઉલ્લેખ કરે છે | |
| TikTok | અક્ષરો, અંકો, underscores, periods; period થી અંત ન થઈ શકે | 2 થી 24 | URL માં case-insensitive (@TikTok tiktok પર ખૂલે છે) | TikTok Support, username બદલવા વિશે |
| YouTube | Support થતી ભાષાઓના અક્ષરો કે અંકો, અને underscores, hyphens, periods અને Latin middle dots (પહેલા કે છેલ્લા નહીં) | મોટાભાગની લિપિ માટે 3 થી 30; Han, Hangul અને કેટલીક બીજી લિપિ માટે ટૂંકી મર્યાદા | Case-sensitive નથી | YouTube Help, હેન્ડલ માર્ગદર્શિકા |
| X | અક્ષરો, અંકો અને underscores | 15 સુધી; નવાં usernames 4 થી લાંબાં હોવાં જોઈએ, પણ જૂનાં ટૂંકાં હેન્ડલ અસ્તિત્વમાં છે | Case-sensitive નથી (x.com/JACK canonical રૂપે x.com/jack બને છે) | X Help Center, username ના નિયમો |
નૉર્મલાઇઝેશનનો algorithm
- Step 1Trim કરો અને scheme ઉમેરો
Whitespace અને છૂટાં angle brackets કાઢો. જો ઇનપુટમાં scheme ન હોય, તો આગળ
https://લગાવો જેથીinstagram.com/natgeoparse થાય. - Step 2સાચા URL parser થી parse કરો
JavaScript માં
new URL()કે Python માંurllib.parse.urlsplitવાપરો, આખી string પર regex ક્યારેય નહીં. Parser ports, user info અને encoded અક્ષરો સંભાળે છે, અનેhttps://instagram.com@evil.com/natgeoને Instagram તરીકે પસાર થતું અટકાવે છે. - Step 3Host નૉર્મલાઇઝ કરો
તેને નાના અક્ષરમાં ફેરવો અને આગળનો એક
www.,m.કેmobile.કાઢો. પછી તેને hosts ની ચોક્કસ allow-list માં શોધો.includes("instagram.com")ક્યારેય ન વાપરો, જેinstagram.com.evil.comસાથે પણ મેળ ખાય છે. - Step 4Query string અને fragment કાઢો
igsh,s,tઅનેlangજેવા ટ્રૅકિંગ પેરામીટર URL કઈ પ્રોફાઇલ તરફ જાય છે તે ક્યારેય બદલતા નથી. - Step 5Path વિભાજિત કરો અને decode કરો
/પર વિભાજિત કરો, ખાલી segments કાઢો (આથી છેલ્લા slashes દૂર થાય છે) અને દરેકને percent-decode કરો. ખોટા encoding ને અમાન્ય ઇનપુટ ગણો. - Step 6પ્લેટફોર્મ મુજબ route કરો
દરેક પ્લેટફોર્મનું રૂપ લાગુ કરો: Instagram અને X પર એક segment, TikTok પર
@segment, અને YouTube પર@handle,channel/UC...,user/nameકેc/name. Reserved શબ્દો નકારો. - Step 7હેન્ડલ validate કરો અને નાના અક્ષરમાં ફેરવો
પ્લેટફોર્મના અક્ષર નિયમો સામે હેન્ડલ તપાસો, પછી તેને નાના અક્ષરમાં ફેરવો. YouTube channel ID ને નાના અક્ષરમાં ન ફેરવો: તે case-sensitive છે.
- Step 8Canonical URL બનાવો
એક નક્કી host પર ભાગો પરથી URL ફરી બનાવો, જેથી દરેક સમાન ઇનપુટ બરાબર એક જ string આપે.
- Step 9Short links અલગથી સંભાળો
TikTok short links ઓળખો, HTTP request થી redirect અનુસરો, અને અંતિમ URL ફરી parser માંથી પસાર કરો. આને pure function ની બહાર રાખો જેથી parsing ઝડપી અને test કરી શકાય તેવું રહે.
JavaScript અને Python માં કોડ
બંને functions એક જ ઇનપુટ માટે એક જ objects આપે છે: હેન્ડલ URL માટે { platform, handle, canonicalUrl }, YouTube channel ID માટે { platform, channelId, canonicalUrl }, YouTube નાં બે જૂનાં રૂપ માટે username કે customName, અને પ્રોફાઇલ URL ન હોય તેવી દરેક વસ્તુ માટે null. બેમાંથી કોઈને dependency જોઈતી નથી.
JavaScript (Node 18+)
const HOSTS = {
"instagram.com": "instagram",
"tiktok.com": "tiktok",
"youtube.com": "youtube",
"x.com": "x",
"twitter.com": "x",
};
const IG_RESERVED = new Set(["p", "reel", "reels", "tv", "stories", "explore", "accounts", "direct"]);
const X_RESERVED = new Set([
"home", "explore", "search", "i", "settings", "notifications", "messages",
"compose", "intent", "hashtag", "login", "signup", "logout", "tos", "privacy",
]);
const IG_HANDLE = /^[a-z0-9._]{1,30}$/;
const TIKTOK_HANDLE = /^[a-z0-9._]{2,24}$/;
const X_HANDLE = /^[a-z0-9_]{1,15}$/;
const YT_HANDLE = /^[\p{L}\p{M}\p{N}._\-·]{1,30}$/u;
const YT_CHANNEL_ID = /^UC[A-Za-z0-9_-]{22}$/;
const YT_LEGACY_NAME = /^[A-Za-z0-9._-]{1,100}$/;
export function parseProfileUrl(input) {
if (typeof input !== "string") return null;
let raw = input.trim().replace(/^<|>$/g, "");
if (!raw) return null;
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) raw = "https://" + raw;
let url;
try {
url = new URL(raw);
} catch {
return null;
}
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
const host = url.hostname.toLowerCase().replace(/^(www|m|mobile)\./, "");
const platform = HOSTS[host];
if (!platform) return null;
let segments;
try {
segments = url.pathname.split("/").filter(Boolean).map(decodeURIComponent);
} catch {
return null; // malformed percent-encoding
}
const [first = "", second] = segments;
if (platform === "instagram") {
const handle = first.toLowerCase();
if (segments.length !== 1 || IG_RESERVED.has(handle) || !IG_HANDLE.test(handle)) return null;
return { platform, handle, canonicalUrl: `https://www.instagram.com/${handle}/` };
}
if (platform === "tiktok") {
if (segments.length !== 1 || !first.startsWith("@")) return null;
const handle = first.slice(1).toLowerCase();
if (!TIKTOK_HANDLE.test(handle) || handle.endsWith(".")) return null;
return { platform, handle, canonicalUrl: `https://www.tiktok.com/@${handle}` };
}
if (platform === "x") {
const handle = first.toLowerCase();
if (segments.length !== 1 || X_RESERVED.has(handle) || !X_HANDLE.test(handle)) return null;
return { platform, handle, canonicalUrl: `https://x.com/${handle}` };
}
// YouTube: /@handle[/tab], /channel/UC...[/tab], /user/name, /c/name
if (first.startsWith("@")) {
const handle = first.slice(1).toLowerCase();
if (!YT_HANDLE.test(handle) || /^[._\-·]|[._\-·]$/.test(handle)) return null;
return { platform, handle, canonicalUrl: `https://www.youtube.com/@${encodeURIComponent(handle)}` };
}
if (first === "channel" && second && YT_CHANNEL_ID.test(second)) {
return { platform, channelId: second, canonicalUrl: `https://www.youtube.com/channel/${second}` };
}
if ((first === "user" || first === "c") && second && segments.length === 2 && YT_LEGACY_NAME.test(second)) {
const key = first === "user" ? "username" : "customName";
return { platform, [key]: second, canonicalUrl: `https://www.youtube.com/${first}/${second}` };
}
return null; // videos, shorts, playlists, search and anything else
}
const SHORT_HOSTS = new Set(["vm.tiktok.com", "vt.tiktok.com"]);
export function isShortLink(input) {
try {
const url = new URL(/^https?:\/\//i.test(input) ? input : "https://" + input);
const host = url.hostname.toLowerCase();
return SHORT_HOSTS.has(host) || (host.replace(/^www\./, "") === "tiktok.com" && url.pathname.startsWith("/t/"));
} catch {
return false;
}
}
export async function resolveShortLink(input) {
const res = await fetch(/^https?:\/\//i.test(input) ? input : "https://" + input, {
redirect: "follow",
signal: AbortSignal.timeout(10_000),
});
await res.body?.cancel();
return res.url; // the final URL after every redirect
}resolveShortLink બિલ્ટ-ઇન fetch વાપરે છે, જે redirects અનુસરે છે અને અંતિમ સરનામું res.url તરીકે આપે છે. જ્યારે isShortLink(link) true હોય ત્યારે parseProfileUrl(await resolveShortLink(link)) call કરો. Short link પ્રોફાઇલને બદલે વીડિયો તરફ પણ લઈ જઈ શકે, અને ત્યારે parser યોગ્ય રીતે null આપે છે.
Python 3
import re
import unicodedata
import urllib.request
from urllib.parse import quote, unquote, urlsplit
HOSTS = {
"instagram.com": "instagram",
"tiktok.com": "tiktok",
"youtube.com": "youtube",
"x.com": "x",
"twitter.com": "x",
}
IG_RESERVED = {"p", "reel", "reels", "tv", "stories", "explore", "accounts", "direct"}
X_RESERVED = {
"home", "explore", "search", "i", "settings", "notifications", "messages",
"compose", "intent", "hashtag", "login", "signup", "logout", "tos", "privacy",
}
IG_HANDLE = re.compile(r"[a-z0-9._]{1,30}")
TIKTOK_HANDLE = re.compile(r"[a-z0-9._]{2,24}")
X_HANDLE = re.compile(r"[a-z0-9_]{1,15}")
YT_CHANNEL_ID = re.compile(r"UC[A-Za-z0-9_-]{22}")
YT_LEGACY_NAME = re.compile(r"[A-Za-z0-9._-]{1,100}")
YT_PUNCT = "._-·"
def _is_youtube_handle(handle):
if not 1 <= len(handle) <= 30 or handle[0] in YT_PUNCT or handle[-1] in YT_PUNCT:
return False
return all(
ch.isalnum() or ch in YT_PUNCT or unicodedata.category(ch).startswith("M")
for ch in handle
)
def parse_profile_url(value):
if not isinstance(value, str):
return None
raw = value.strip().strip("<>")
if not raw:
return None
if not re.match(r"^[a-z][a-z0-9+.-]*://", raw, re.I):
raw = "https://" + raw
try:
url = urlsplit(raw)
host = (url.hostname or "").lower()
except ValueError:
return None
if url.scheme.lower() not in ("http", "https"):
return None
host = re.sub(r"^(www|m|mobile)\.", "", host)
platform = HOSTS.get(host)
if not platform:
return None
try:
segments = [unquote(s, errors="strict") for s in url.path.split("/") if s]
except UnicodeDecodeError:
return None # malformed percent-encoding
first = segments[0] if segments else ""
second = segments[1] if len(segments) > 1 else None
if platform == "instagram":
handle = first.lower()
if len(segments) != 1 or handle in IG_RESERVED or not IG_HANDLE.fullmatch(handle):
return None
return {"platform": platform, "handle": handle,
"canonicalUrl": f"https://www.instagram.com/{handle}/"}
if platform == "tiktok":
if len(segments) != 1 or not first.startswith("@"):
return None
handle = first[1:].lower()
if not TIKTOK_HANDLE.fullmatch(handle) or handle.endswith("."):
return None
return {"platform": platform, "handle": handle,
"canonicalUrl": f"https://www.tiktok.com/@{handle}"}
if platform == "x":
handle = first.lower()
if len(segments) != 1 or handle in X_RESERVED or not X_HANDLE.fullmatch(handle):
return None
return {"platform": platform, "handle": handle, "canonicalUrl": f"https://x.com/{handle}"}
# YouTube: /@handle[/tab], /channel/UC...[/tab], /user/name, /c/name
if first.startswith("@"):
handle = first[1:].lower()
if not _is_youtube_handle(handle):
return None
return {"platform": platform, "handle": handle,
"canonicalUrl": f"https://www.youtube.com/@{quote(handle, safe='._-')}"}
if first == "channel" and second and YT_CHANNEL_ID.fullmatch(second):
return {"platform": platform, "channelId": second,
"canonicalUrl": f"https://www.youtube.com/channel/{second}"}
if first in ("user", "c") and second and len(segments) == 2 and YT_LEGACY_NAME.fullmatch(second):
key = "username" if first == "user" else "customName"
return {"platform": platform, key: second,
"canonicalUrl": f"https://www.youtube.com/{first}/{second}"}
return None # videos, shorts, playlists, search and anything else
SHORT_HOSTS = {"vm.tiktok.com", "vt.tiktok.com"}
def is_short_link(value):
url = urlsplit(value if re.match(r"^https?://", value, re.I) else "https://" + value)
host = (url.hostname or "").lower()
return host in SHORT_HOSTS or (
re.sub(r"^www\.", "", host) == "tiktok.com" and url.path.startswith("/t/")
)
def resolve_short_link(value, timeout=10):
if not re.match(r"^https?://", value, re.I):
value = "https://" + value
request = urllib.request.Request(value, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(request, timeout=timeout) as response:
return response.geturl() # the final URL after every redirectPython નાં \w અને str.isalnum() Unicode અક્ષરો અને અંકો આવરે છે, પણ Devanagari જેવી લિપિમાં વપરાતા combining marks નહીં, તેથી YouTube ની તપાસ Unicode ની M શ્રેણીના અક્ષરો પણ સ્વીકારે છે.
Edge cases અને tests
નીચેની દરેક હરોળ test suite નો ભાગ છે જે બંને implementations પાસ કરે છે.
| ઇનપુટ | આઉટપુટ | કેમ |
|---|---|---|
instagram.com/NatGeo?igsh=MWQ1ZGUxMzBkMA== | instagram, natgeo, https://www.instagram.com/natgeo/ | Scheme નથી, મિશ્ર case, share પેરામીટર |
https://m.instagram.com/nat.geo_/#bio | instagram, nat.geo_ | મોબાઇલ host અને fragment |
https://www.instagram.com/p/C9xYzAbCdEf/ | null | પોસ્ટ, પ્રોફાઇલ નહીં |
https://www.instagram.com/stories/natgeo/1234567890/ | null | Story, ભલે path માં હેન્ડલ હોય |
tiktok.com/@Khaby.Lame/ | tiktok, khaby.lame, https://www.tiktok.com/@khaby.lame | છેલ્લો slash અને case |
https://www.tiktok.com/@khaby.lame/video/7106594312292453675 | null | વીડિયો URL |
https://vm.tiktok.com/ZMabc123/ | null (પહેલાં redirect અનુસરો) | હેન્ડલ વિનાની short link |
https://m.youtube.com/@GoogleDevelopers/videos?app=desktop | youtube, googledevelopers | મોબાઇલ host, ચેનલ ટૅબ, query |
https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw/featured | youtube, channelId UC_x5XG1OV2P6uZZ5FSM9Ttw | Channel ID પોતાનો case રાખે છે |
https://www.youtube.com/c/GoogleDevelopers | youtube, customName GoogleDevelopers | જૂનું custom URL; ID માટે lookup જોઈએ |
https://www.youtube.com/@%E3%83%86%E3%82%B9%E3%83%88 | youtube, હેન્ડલ テスト | Percent-encoded Unicode હેન્ડલ |
https://youtu.be/dQw4w9WgXcQ | null | વીડિયો, ચેનલ નહીં |
https://mobile.twitter.com/jack?s=21&t=abc | x, jack, https://x.com/jack | જૂનું domain અને share પેરામીટર |
https://x.com/i/flow/login | null | App route |
https://x.com/this_handle_is_too_long | null | 15 થી વધુ અક્ષર |
https://instagram.com.evil.com/natgeo | null | મળતો આવતો host |
https://instagram.com@evil.com/natgeo | null | User info ની યુક્તિ; સાચો host evil.com છે |
@natgeo | null | ખાલી હેન્ડલનું કોઈ પ્લેટફોર્મ નથી |
Cases તમારી repository માં રાખો અને CI માં ચલાવો. જ્યારે કોઈ પ્લેટફોર્મ નવો route કે share પેરામીટર ઉમેરે, ત્યારે તમે એક હરોળ ઉમેરો છો અને સુધારો સ્પષ્ટ દેખાય છે.
import assert from "node:assert/strict";
import { parseProfileUrl } from "./parse.mjs";
const cases = [
["instagram.com/NatGeo?igsh=MWQ1ZGUxMzBkMA==", { platform: "instagram", handle: "natgeo", canonicalUrl: "https://www.instagram.com/natgeo/" }],
["https://www.instagram.com/reel/C9xYzAbCdEf/", null],
["https://www.instagram.com/explore/", null],
["tiktok.com/@Khaby.Lame/", { platform: "tiktok", handle: "khaby.lame", canonicalUrl: "https://www.tiktok.com/@khaby.lame" }],
["https://www.tiktok.com/@khaby.lame/video/7106594312292453675", null],
["https://vm.tiktok.com/ZMabc123/", null],
["https://m.youtube.com/@GoogleDevelopers/videos", { platform: "youtube", handle: "googledevelopers", canonicalUrl: "https://www.youtube.com/@googledevelopers" }],
["https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw/featured", { platform: "youtube", channelId: "UC_x5XG1OV2P6uZZ5FSM9Ttw", canonicalUrl: "https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw" }],
["https://www.youtube.com/user/GoogleDevelopers", { platform: "youtube", username: "GoogleDevelopers", canonicalUrl: "https://www.youtube.com/user/GoogleDevelopers" }],
["https://youtu.be/dQw4w9WgXcQ", null],
["https://twitter.com/Jack/", { platform: "x", handle: "jack", canonicalUrl: "https://x.com/jack" }],
["https://mobile.twitter.com/jack?s=21", { platform: "x", handle: "jack", canonicalUrl: "https://x.com/jack" }],
["https://x.com/jack/status/20", null],
["https://x.com/i/flow/login", null],
["https://instagram.com.evil.com/natgeo", null],
];
for (const [input, expected] of cases) {
assert.deepEqual(parseProfileUrl(input), expected, input);
}
console.log(`${cases.length} cases passed`);from parse_profile import parse_profile_url
CASES = [
("instagram.com/NatGeo?igsh=MWQ1ZGUxMzBkMA==", {"platform": "instagram", "handle": "natgeo", "canonicalUrl": "https://www.instagram.com/natgeo/"}),
("https://www.instagram.com/reel/C9xYzAbCdEf/", None),
("tiktok.com/@Khaby.Lame/", {"platform": "tiktok", "handle": "khaby.lame", "canonicalUrl": "https://www.tiktok.com/@khaby.lame"}),
("https://vm.tiktok.com/ZMabc123/", None),
("https://m.youtube.com/@GoogleDevelopers/videos", {"platform": "youtube", "handle": "googledevelopers", "canonicalUrl": "https://www.youtube.com/@googledevelopers"}),
("https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw/featured", {"platform": "youtube", "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw", "canonicalUrl": "https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw"}),
("https://youtu.be/dQw4w9WgXcQ", None),
("https://twitter.com/Jack/", {"platform": "x", "handle": "jack", "canonicalUrl": "https://x.com/jack"}),
("https://x.com/i/flow/login", None),
("https://instagram.com.evil.com/natgeo", None),
]
for value, expected in CASES:
assert parse_profile_url(value) == expected, value
print(f"{len(CASES)} cases passed")મૂળ ઇનપુટ અને canonical રૂપ બંને રાખો
નૉર્મલાઇઝેશન રચનાથી જ માહિતી ગુમાવે છે, અને તમારા નિયમો સમય સાથે સુધરશે. બંને મૂલ્યો રાખો:
- `raw_url`: યુઝર કે import એ જે આપ્યું તે બરાબર. તે તમને પછીથી વધુ સારો parser ફરી ચલાવવા દે છે, અને યુઝર પોતાની પ્રોફાઇલ એડિટ કરે ત્યારે તમે તે જ પાછું બતાવો છો.
- `platform` અને `handle` કે `channel_id`: ભાગો,
(platform, handle)પર unique index સાથે. આ તમારી dedupe અને join key છે. - `canonical_url`: ફરી બનાવેલું URL, બતાવવા અને પ્લેટફોર્મને call કરવા માટે.
- `parsed_at` અને parser version: જેથી તમારા નિયમોના જૂના version થી નૉર્મલાઇઝ થયેલી હરોળો શોધી શકો.
હેન્ડલ કાયમી નથી. ક્રિએટર દરેક પ્લેટફોર્મ પર એકાઉન્ટનાં નામ બદલે છે, અને ગયા વર્ષે રાખેલું હેન્ડલ હવે કોઈ બીજાનું હોઈ શકે. જ્યાં સત્તાવાર API સ્થિર ID આપે, જેમ કે YouTube channel ID, ત્યાં તેને લાંબા ગાળાની key તરીકે રાખો અને હેન્ડલને display value ગણો જે તમે રિફ્રેશ કરો. હેન્ડલ પરથી YouTube channel ID મેળવવા વિશેની સાથી માર્ગદર્શિકા બતાવે છે કે કેવી રીતે.
એક પ્રોફાઇલ પરથી બીજી પ્રોફાઇલ સુધી
પ્રોફાઇલ URL નૉર્મલાઇઝ થયા પછી, પછીનો પ્રશ્ન સામાન્ય રીતે એ હોય છે કે એ જ ક્રિએટરનાં બીજાં કયાં એકાઉન્ટ છે. sociallinks.dev એક API છે જે એક Instagram, TikTok, YouTube કે X પ્રોફાઇલ URL લે છે અને ક્રિએટરનાં બીજાં પ્લેટફોર્મ પરનાં એકાઉન્ટ, દરેક મેળ માટે confidence score અને પુરાવા સાથે આપે છે.
વારંવાર પૂછાતા પ્રશ્નો
શું હેન્ડલ રાખતા પહેલાં તેને નાના અક્ષરમાં ફેરવવાં જોઈએ?
Lookup key માટે હા. YouTube અને X કહે છે કે હેન્ડલ case-sensitive નથી, અને TikTok @TikTok ને @tiktok જેવા જ એકાઉન્ટ પર ખોલે છે. Instagram case નો નિયમ દસ્તાવેજિત નથી કરતું, પણ usernames નાના અક્ષરમાં દેખાય છે, તેથી નાના અક્ષર સુરક્ષિત default છે. બતાવવા માગતા હો તો મૂળ casing raw URL માં રાખો. YouTube channel ID ને નાના અક્ષરમાં ન ફેરવો, તે case-sensitive છે.
શું પોસ્ટ કે વીડિયો URL પરથી હેન્ડલ કાઢી શકાય?
TikTok વીડિયો (/@handle/video/id) અને X પોસ્ટ (/handle/status/id) માટે path માંનું હેન્ડલ લેખકનું છે, તેથી તમને તે જોઈતું હોય તો કાઢી શકો. Instagram પોસ્ટ અને reel URL (/p/ અને /reel/) માં હેન્ડલ નથી હોતું. આ માર્ગદર્શિકાનાં functions આ બધા માટે null આપે છે કારણ કે તે પ્રોફાઇલ લિંક નથી.
vm.tiktok.com લિંક કેવી રીતે સંભાળવી?
Short links માં માત્ર એક code હોય છે. HTTP request કરો, redirects અનુસરો, અને અંતિમ URL parse કરો. તે timeout અને cache સાથે અલગ પગલામાં કરો, કારણ કે તે network call છે અને code expire થઈ શકે.
શું twitter.com હજી માન્ય પ્રોફાઇલ URL છે?
હા. twitter.com પ્રોફાઇલ URL x.com પર redirect થાય છે, તેથી બંને domain ને X પ્લેટફોર્મ ગણો અને x.com રૂપને canonical તરીકે રાખો.
એક મોટું regular expression કેમ ન વાપરવું?
આખી string પર એક regex વાંચવું મુશ્કેલ છે અને મળતા આવતા hosts, user info કે encoded અક્ષરોથી સહેલાઈથી છેતરાય છે. પહેલાં standard library થી URL parse કરો, પછી માત્ર હેન્ડલ પર નાનાં regex વાપરો.
YouTube /c/ કે /user/ URL ને channel ID માં કેવી રીતે ફેરવવું?
/user/<name> URL YouTube Data API ના forUsername filter સાથે મેળ ખાય છે. /c/ custom નામ માટે કોઈ API filter નથી. બંને કિસ્સા માટે હેન્ડલ પરથી YouTube channel ID મેળવવા વિશેની માર્ગદર્શિકા જુઓ.