This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_port.go
100 lines (80 loc) · 1.76 KB
/
db_port.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
// +build ignore
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"strings"
_ "github.com/lib/pq"
)
var (
db *sql.DB
driver = flag.String("driver", "postgres", "The database driver to use")
connect = flag.String("connect", "dbname=nnev host=/var/run/postgresql sslmode=disable", "The connection string to use")
)
func openDB() (err error) {
db, err = sql.Open(*driver, *connect)
if err != nil {
return err
}
_, err = db.Exec("SELECT 1")
if err != nil {
return err
}
return nil
}
func guessKind(infourl string) string {
if strings.HasSuffix(infourl, ".pdf") {
fmt.Println("Guessing kind 'Folien' for", infourl)
return "Folien"
}
if strings.Contains(infourl, "youtu.be") {
fmt.Println("Guessing kind 'Aufzeichnung' for", infourl)
return "Aufzeichung"
}
if strings.Contains(infourl, "noname-ev.de/w/") {
fmt.Println("Guessing kind 'Wiki-Link' for", infourl)
return "Wikilink"
}
return "Sonstiges"
}
func main() {
flag.Parse()
err := openDB()
if err != nil {
log.Fatal("Could not connect to database:", err)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Rollback()
rows, err := tx.Query("SELECT id, infourl FROM vortraege;")
if err != nil {
log.Fatal(err)
}
infos := make(map[int][]string)
for rows.Next() {
var id int
var infourl sql.NullString
if err := rows.Scan(&id, &infourl); err != nil {
log.Fatal(err)
}
if infourl.String == "" {
continue
}
kind := guessKind(infourl.String)
infos[id] = []string{kind, infourl.String}
}
for id, info := range infos {
_, err = tx.Exec("INSERT INTO vortrag_links (vortrag, kind, url) values($1, $2, $3);", id, info[0], info[1])
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
}