-
Notifications
You must be signed in to change notification settings - Fork 7
/
sql.go
172 lines (155 loc) · 4.96 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"database/sql"
"fmt"
"math/rand"
"time"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 9998
user = "nyaapantsu"
password = "nyaapantsu"
dbname = "nyaapantsu"
pwChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
//sqlHashExists returns a boolean on whether or not the hash is already in the table
func sqlHashExists(db *sql.DB, hash, table string) bool {
sqlTorrentQuery := `SELECT torrent_hash FROM ` + table + ` WHERE torrent_hash=$1;`
var torrentHash string
row := db.QueryRow(sqlTorrentQuery, hash)
switch err := row.Scan(&torrentHash); err {
case sql.ErrNoRows:
fmt.Println("No rows returned, attempting insert")
return false
case nil:
fmt.Println("Found", torrentHash, "skipping")
return true
default:
fmt.Println(err)
return false
}
}
//sqlTorrentInsert does what it says on the tin
func sqlTorrentInsert(db *sql.DB, torrent Torrent, table string) {
sqlTorrentInsert := `INSERT INTO ` + table + ` (torrent_name, torrent_hash,
category, sub_category, status, date, uploader, downloads, stardom,
filesize, description, hidden)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`
_, err := db.Exec(sqlTorrentInsert, torrent.Title, torrent.Hash,
torrent.Category[0], torrent.Category[1], 0, torrent.Date,
torrent.UploaderID, 0, 0, torrent.FileSize, torrent.Description, 0)
if err != nil {
fmt.Println(err)
}
fmt.Println("Inserted", torrent.Hash, "into DB!")
}
func sqlStatsInsert(db *sql.DB, torrent Torrent, table string) {
sqlUserQuery := `SELECT torrent_id FROM public` + table + ` WHERE torrent_hash=$1;`
row := db.QueryRow(sqlUserQuery, torrent.Hash)
var torrentID string
switch err := row.Scan(&torrentID); err {
case sql.ErrNoRows:
fmt.Println("torrent not found skipping")
return
case nil:
fmt.Println("Torrent Found inserting stats")
default:
fmt.Println(err)
return
}
sqlStatsInsert := `INSERT INTO` + table + `(torrent_id, seeders, leechers, completed, last_scrape) values($1, $2, $3, $4, $5)`
_, err := db.Exec(sqlStatsInsert, torrentID, torrent.Seeders, torrent.Leechers, torrent.Completed, time.Now())
if err != nil {
fmt.Println(err)
}
}
//sqlUserExists does what it says on the tin
//If the user doesnt exist, attempts an insert
//Returns the userID and userStatus
func sqlUserExists(db *sql.DB, username string) (userID, userStatus int) {
sqlUserQuery := `SELECT user_id, status FROM public.users WHERE username=$1;`
row := db.QueryRow(sqlUserQuery, username)
switch err := row.Scan(&userID, &userStatus); err {
case sql.ErrNoRows:
fmt.Println("User not found, attempting insert")
userID = sqlUserInsert(db, username)
userStatus = 3
return
case nil:
fmt.Println("User found, checking hash")
return
default:
fmt.Println(err)
return
}
}
func sqlUserInsert(db *sql.DB, username string) (userID int) {
sqlUserInsert := `INSERT INTO public.users (username, password, status,
created_at, api_token_expiry) VALUES ($1, $2, $3, $4, $5)`
//Status is hardcoded to 3, as that means it was a scraped user
_, err := db.Exec(sqlUserInsert, username, RandPassword(), 3, time.Now(), time.Now())
if err != nil {
fmt.Println(err)
}
//TODO: Rewrite this in a non-stupid fashion
userID, _ = sqlUserExists(db, username)
return
}
//RandPassword generates a random password for scraped users not in the DB
//Unabashedly stolen from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func RandPassword() string {
b := make([]byte, 12)
for i := range b {
b[i] = pwChars[rand.Intn(len(pwChars))]
}
return string(b)
}
func sqlWorker(chTorrent <-chan Torrent, chFinished chan<- bool, chInsertCount chan<- int, chFoundCount chan<- int) {
defer func() {
chFinished <- true
}()
//Connect to the DB
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Connected!")
var table string
var scrape string
var userStatus int
for torrent := range chTorrent {
//Check if the user exists first
//Special condition for nyaa.si anonymous uploads
if torrent.Uploader != "Anonymous" {
torrent.UploaderID, userStatus = sqlUserExists(db, torrent.Uploader)
} else {
torrent.UploaderID = 0
userStatus = 3 //Anonymous username means we scraped it
}
//Determine the table we want
if torrent.Adult {
table = "public.sukebei_torrents"
scrape = "public.sukebei_scrape"
} else {
table = "public.torrents"
scrape = "public.scrape"
}
//If our user was scraped and the hash doesnt exist, insert
if userStatus == 3 && !sqlHashExists(db, torrent.Hash, table) {
sqlTorrentInsert(db, torrent, table)
}
sqlStatsInsert(db, torrent, scrape)
chInsertCount <- 1 //Tracker to ensure we've attempted every hash we find
}
fmt.Println("Exiting SQL worker")
}