MCPcopy Create free account
hub / github.com/AandStep/ResultV / pickIconFromSubscriptionHTML

Function pickIconFromSubscriptionHTML

app.go:1882–1938  ·  view source on GitHub ↗
(client *http.Client, subURL string, html string)

Source from the content-addressed store, hash-verified

1880}
1881
1882func pickIconFromSubscriptionHTML(client *http.Client, subURL string, html string) string {
1883 html = strings.TrimSpace(html)
1884 if html == "" {
1885 return ""
1886 }
1887 if len(html) > 262144 {
1888 html = html[:262144]
1889 }
1890 reMeta := regexp.MustCompile(`(?is)<meta[^>]+(?:property|name)=["'](?:og:image|twitter:image)["'][^>]+content=["']([^"']+)["']`)
1891 reMetaRev := regexp.MustCompile(`(?is)<meta[^>]+content=["']([^"']+)["'][^>]+(?:property|name)=["'](?:og:image|twitter:image)["']`)
1892 reApple1 := regexp.MustCompile(`(?is)<link[^>]+rel=["']apple-touch-icon["'][^>]+href=["']([^"']+)["']`)
1893 reApple2 := regexp.MustCompile(`(?is)<link[^>]+href=["']([^"']+)["'][^>]+rel=["']apple-touch-icon["']`)
1894 reLink := regexp.MustCompile(`(?is)<link[^>]+rel=["'][^"']*icon[^"']*["'][^>]+href=["']([^"']+)["']`)
1895 reLinkHrefFirst := regexp.MustCompile(`(?is)<link[^>]+href=["']([^"']+)["'][^>]+rel=["'][^"']*icon[^"']*["']`)
1896 reImgLogo := regexp.MustCompile(`(?is)<img[^>]+src=["']([^"']+)["'][^>]*(?:logo|brand)|(?:logo|brand)[^>]*<img[^>]+src=["']([^"']+)["']`)
1897 parsedBase, _ := url.Parse(subURL)
1898 resolve := func(raw string) string {
1899 raw = strings.TrimSpace(raw)
1900 if raw == "" {
1901 return ""
1902 }
1903 u, err := url.Parse(raw)
1904 if err != nil {
1905 return ""
1906 }
1907 if u.IsAbs() {
1908 return u.String()
1909 }
1910 if parsedBase == nil {
1911 return ""
1912 }
1913 return parsedBase.ResolveReference(u).String()
1914 }
1915 tryRegexes := func(re *regexp.Regexp) string {
1916 m := re.FindStringSubmatch(html)
1917 if len(m) == 0 {
1918 return ""
1919 }
1920 for i := 1; i < len(m); i++ {
1921 candidate := resolve(m[i])
1922 if candidate == "" {
1923 continue
1924 }
1925 if data := inlineSmallImageFromURL(client, candidate, subURL); data != "" {
1926 return data
1927 }
1928 continue
1929 }
1930 return ""
1931 }
1932 for _, re := range []*regexp.Regexp{reMeta, reMetaRev, reApple1, reApple2, reLink, reLinkHrefFirst, reImgLogo} {
1933 if got := tryRegexes(re); got != "" {
1934 return got
1935 }
1936 }
1937 return ""
1938}
1939

Calls 1

inlineSmallImageFromURLFunction · 0.85