Files
supermarket/internal/web/links.go
T
davidandClaude Opus 5.5 fc7dcf1f66 Phase 8 part 2: who links to you, from Bing
With a Bing Webmaster key, Supermarket reads once a week (or on Read
now) how many links Bing knows to each page of the site, and for the 25
most linked pages, the pages that link and their anchor text, into the
backlinks table. SEO → Links lists the linking sites, new ones first,
with the pages they link to and their link text; without a key it says
how to get one. A refused key keeps the last read, with the error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 08:31:27 -07:00

116 lines
3.0 KiB
Go

package web
import (
"context"
"net/http"
"strconv"
"time"
"supermarket/internal/backlinks"
"supermarket/internal/profile"
"supermarket/internal/tracking"
)
// SEO → Links: links from other sites to yours.
// linkDays is the period the Links page covers, as What ranks does.
const linkDays = 90
func (s *Server) seoLinksPage(w http.ResponseWriter, r *http.Request) {
data, cur := s.seoBase(r)
data["Path"] = "/seo/links"
data["Plausible"] = s.plausible() != nil
if cur != nil {
if p, _ := profile.Load(s.DB); p != nil {
for _, web := range p.Websites {
if web.URL != cur.URL {
continue
}
list, ok, err := tracking.Referrers(s.DB, web, linkDays)
if err != nil {
s.serverError(w, err)
return
}
data["Read"] = ok
var links []tracking.Referrer
others := map[string]int{}
for _, ref := range list {
if ref.Kind == tracking.RefLink {
links = append(links, ref)
} else {
others[ref.Kind] += ref.Visitors
}
}
data["Links"], data["Others"] = links, others
}
}
var known int
_ = s.DB.QueryRow(`SELECT count(*) FROM backlinks WHERE site = ?`, cur.URL).Scan(&known)
data["Known"] = known
}
if cur != nil {
data["BingKey"] = s.bing() != nil
data["BingSite"] = s.bingSite(*cur)
st, err := backlinks.LastBing(s.DB, cur.URL)
if err != nil {
s.serverError(w, err)
return
}
data["Bing"] = st
links, err := backlinks.Links(s.DB, cur.URL)
if err != nil {
s.serverError(w, err)
return
}
first := ""
if st != nil {
first = st.First
}
sites := backlinks.Sites(links, first)
data["LinkingSites"], data["LinkCount"] = sites, len(links)
if len(sites) > 30 {
data["MoreSites"], data["LinkingSites"] = len(sites)-30, sites[:30]
}
}
last, _ := tracking.Last(s.DB, "referrers")
data["Last"] = last
s.render(w, r, "seo-links", page{Title: "Links", Nav: "seo/links", Data: data})
}
// seoLinksBing reads the site's links from Bing now.
func (s *Server) seoLinksBing(w http.ResponseWriter, r *http.Request) {
_, cur := s.seoCurrent(r)
c := s.bing()
if cur == nil || c == nil {
redirect(w, r, "/seo/links", "")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
back := "/seo/links?site=" + strconv.Itoa(cur.Index) + "#bing"
if _, err := backlinks.SyncBing(ctx, s.DB, c, cur.URL, s.bingSite(*cur), time.Now()); err != nil {
redirect(w, r, back, "") // the card shows the error
return
}
redirect(w, r, back, "bing-read")
}
// weeklyBingLinks reads each website's links from Bing once a week.
func (s *Server) weeklyBingLinks(ctx context.Context) {
c := s.bing()
if c == nil {
return
}
now := time.Now()
for _, site := range s.seoSites() {
if site.URL == "" || !backlinks.DueBing(s.DB, site.URL, now) {
continue
}
bctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
if _, err := backlinks.SyncBing(bctx, s.DB, c, site.URL, s.bingSite(site), now); err != nil {
s.Log.Warn("backlinks: reading Bing", "site", site.URL, "err", err)
}
cancel()
}
}