Track artist popularity
[brackets.git] / scrape / graph / graph.go
... / ...
CommitLineData
1package graph
2
3import (
4 "git.jacobcasper.com/brackets/env"
5 "github.com/zmb3/spotify"
6 "log"
7 "net/http"
8 "net/url"
9 "time"
10)
11
12func Scrape(env *env.Env) {
13infinite:
14 for {
15 time.Sleep(time.Second * 5)
16 rows, err := env.Db.Db.Query(`
17SELECT ID
18FROM ARTIST
19WHERE ID NOT IN (
20 SELECT ARTIST_ID
21 FROM SCRAPED_ARTIST
22 WHERE SCRAPED == 1
23)`,
24 )
25 if err != nil {
26 log.Print(err)
27 continue infinite
28 }
29 defer rows.Close()
30
31 var artistId string
32 for rows.Next() {
33 if err := rows.Scan(&artistId); err != nil {
34 log.Print(err)
35 continue infinite
36 }
37
38 artists, err := env.C.GetRelatedArtists(spotify.ID(artistId))
39 if err != nil {
40 log.Print(err)
41 continue infinite
42 }
43
44 success := true
45 postArtists:
46 for _, artist := range artists {
47 row := env.Db.Db.QueryRow(`
48SELECT EXISTS (
49 SELECT 1
50 FROM ARTIST
51 WHERE ID = ?
52)
53`,
54 artist.ID,
55 )
56 var exists bool
57 if err := row.Scan(&exists); err != nil {
58 // We don't care, this was a short circuit check
59 }
60 if exists {
61 continue postArtists
62 }
63
64 resp, err := http.PostForm("http://localhost:8080/artist/add", url.Values{"id": {string(artist.ID)}})
65 if err != nil {
66 log.Print(err)
67 success = false
68 continue postArtists
69 }
70 if resp.StatusCode != http.StatusCreated {
71 success = false
72 }
73 }
74
75 if success {
76 env.Db.Mu.Lock()
77 env.Db.Db.Exec(`
78REPLACE INTO SCRAPED_ARTIST (ARTIST_ID, SCRAPED)
79VALUES (?, 1)`,
80 string(artistId),
81 )
82 env.Db.Mu.Unlock()
83 }
84 }
85 }
86}