11package main
22
33import (
4+ "bufio"
45 "encoding/json"
56 "fmt"
67 "io"
78 "net/http"
89 "net/url"
910 "os"
11+ "regexp"
1012 "strings"
1113 "time"
1214)
1315
14- func main () {
15- // Try to find the specific post about Kitten TTS
16- searchTerm := "Kitten TTS"
16+ // Copy of the extractGitHubURL function for testing
17+ func extractGitHubURL (content string ) string {
18+ if ! strings .Contains (content , "github.com" ) {
19+ return ""
20+ }
1721
18- // Set up API request
19- baseURL := "https://hn.algolia.com/api/v1/search"
20- u , err := url .Parse (baseURL )
21- if err != nil {
22- fmt .Printf ("Error parsing URL: %v\n " , err )
23- os .Exit (1 )
22+ // Process content line by line for better control
23+ r := strings .NewReader (content )
24+ scanner := bufio .NewScanner (r )
25+
26+ for scanner .Scan () {
27+ line := scanner .Text ()
28+
29+ // Handle markdown links with GitHub URLs - most common in Reddit posts
30+ // Example: [repo name](https://github.com/user/repo)
31+ markdownLinkRegex := regexp .MustCompile (`\[.*?\]\((https?://)?github\.com/([^/\s]+/[^/\s]+)` )
32+ markdownMatches := markdownLinkRegex .FindStringSubmatch (line )
33+ if len (markdownMatches ) > 2 {
34+ return "https://github.com/" + markdownMatches [2 ]
35+ }
36+
37+ // Handle markdown nested in brackets
38+ // Example: [https://github.com/user/repo](https://github.com/user/repo)
39+ nestedMarkdownRegex := regexp .MustCompile (`\[(https?://)?github\.com/([^/\s\]]+/[^/\s\]]+)\]` )
40+ nestedMatches := nestedMarkdownRegex .FindStringSubmatch (line )
41+ if len (nestedMatches ) > 2 {
42+ return "https://github.com/" + nestedMatches [2 ]
43+ }
44+
45+ // Handle URLs with text prefixes like "months:", "Link]", etc.
46+ // Example: months: [https://github.com/user/repo](https://github.com/user/repo
47+ prefixedURLRegex := regexp .MustCompile (`(?:months:|Link[\]\)]|APK[\]\)]|GitHub:|Github[\]\)]|\|https?://)?\s*(?:\[|\()?(?:https?://)?github\.com/([^/\s\]\)]+/[^/\s\]\)]+)` )
48+ prefixMatches := prefixedURLRegex .FindStringSubmatch (line )
49+ if len (prefixMatches ) > 1 {
50+ // Clean the repo name from any trailing characters
51+ repoName := strings .TrimRight (prefixMatches [1 ], ".,;:!?)\\ ]\" " )
52+ return "https://github.com/" + repoName
53+ }
54+
55+ // Basic GitHub URL pattern as fallback
56+ basicURLRegex := regexp .MustCompile (`(?:https?://)?github\.com/([^/\s]+/[^/\s]+)` )
57+ basicMatches := basicURLRegex .FindStringSubmatch (line )
58+ if len (basicMatches ) > 1 {
59+ // Clean the repo name from any trailing characters
60+ repoName := strings .TrimRight (basicMatches [1 ], ".,;:!?)\\ ]\" " )
61+ return "https://github.com/" + repoName
62+ }
2463 }
2564
26- q := u .Query ()
27- q .Set ("tags" , "story,show_hn" )
28- q .Set ("query" , searchTerm )
29- q .Set ("hitsPerPage" , "100" )
30- u .RawQuery = q .Encode ()
65+ return ""
66+ }
67+
68+ func main () {
69+ // Test cases from the provided data
70+ testURLs := []string {
71+ "https://months: [https://github.com/getlilac/lilac](https://github.com/getlilac/lilac" ,
72+ "https://[MCPJam](https://github.com/MCPJam/inspector" ,
73+ "https://[https://github.com/NevaMind-AI/memU](https://github.com/NevaMind-AI/memU" ,
74+ "https://https://github.com/clidey/dory" ,
75+ "https://[Leaflet](https://github.com/Leaflet/Leaflet" ,
76+ "https://[https://github.com/TrueTheos/Aniki](https://github.com/TrueTheos/Aniki" ,
77+ "https://[https://github.com/spel987/PolyUploader](https://github.com/spel987/PolyUploader/" ,
78+ "https://github.com/naruaika/eruo-data-studio" , // A proper URL for comparison
79+ "https://GitHub: [https://github.com/timoheimonen/securememo.app](https://github.com/timoheimonen/securememo.app" ,
80+ "https://[https://github.com/MCPJam/inspector](https://github.com/MCPJam/inspector" ,
81+ "https://(https://github.com/nsarathy/coffy" ,
82+ "https://[https://github.com/comma-compliance](https://github.com/comma-compliance" ,
83+ "https://Link](http://github.com/rohankishore/Schemix/" ,
84+ "https://APK](https://github.com/adeeteya/Awake-AlarmApp/releases/latest/download/Awake-Android.apk" ,
85+ }
3186
32- apiURL := u .String ()
33- fmt .Printf ("Searching with URL: %s\n " , apiURL )
87+ fmt .Println ("Testing GitHub URL extraction..." )
3488
3589 // Make the request
3690 client := & http.Client {Timeout : 10 * time .Second }
0 commit comments