Created
November 9, 2023 21:19
-
-
Save SkYNewZ/876c3fe69d7d84cf47510032194ff888 to your computer and use it in GitHub Desktop.
Get YouTube channel ID from channel URL in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"regexp" | |
) | |
func main() { | |
// Check if the command-line argument is provided | |
if len(os.Args) < 2 { | |
fmt.Println("Please provide a URL to search into.") | |
return | |
} | |
// Get the URL from the command-line argument | |
url := os.Args[1] | |
// Make an HTTP GET request | |
response, err := http.Get(url) | |
if err != nil { | |
fmt.Println("Error fetching the URL:", err) | |
return | |
} | |
defer response.Body.Close() | |
// Read the response body | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
fmt.Println("Error reading the response:", err) | |
return | |
} | |
// Convert the body bytes to string | |
content := string(body) | |
// Regular expression to find the identifier | |
re := regexp.MustCompile(`https://www\.youtube\.com/channel/([^\s"]+)`) | |
// Find the match in the retrieved content | |
match := re.FindStringSubmatch(content) | |
if len(match) >= 2 { | |
// The first element in the slice will be the whole match, and the second will be the captured group. | |
identifier := match[1] | |
fmt.Println("Identifier:", identifier) | |
} else { | |
fmt.Println("No match found") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment