-
Notifications
You must be signed in to change notification settings - Fork 1
/
dl_mysql.go
140 lines (129 loc) · 4.38 KB
/
dl_mysql.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
package main
import (
log "github.com/Sirupsen/logrus"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
"strings"
"os"
)
/*
The following Table Structure is for hls-get to download from a MySQL db table.
`url` field is the source url for downloading,
`dest` field will be filled with file saved path after downloaded,
`ret_code` and `ret_msg` indicates the download result, 0 and empty message means DONE well.
*/
/*
-- ----------------------------
-- Table structure for download_list
-- ----------------------------
DROP TABLE IF EXISTS `hlsget_downloads`;
CREATE TABLE `hlsget_downloads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(256) NOT NULL,
`status` int(11) NOT NULL DEFAULT '0',
`dest` varchar(256) DEFAULT NULL,
`ret_code` int(11) DEFAULT '0',
`ret_msg` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB AUTO_INCREMENT=393211 DEFAULT CHARSET=latin1;
*/
/****
The following script shows load download list from epgdb_vod.pulish_movie:
INSERT INTO hlsgetdb.hlsget_downloads (url, ret_code) SELECT `guid`, 0 FROM epgdb_vod.publish_movie WHERE `guid` <> "";
*/
type Dl_MySQL struct {
host string
port uint
db string
table string
username string
password string
_pdb *sql.DB
}
func ShowMySQLSchema() {
os.Stderr.Write([]byte(" -- ----------------------------\n"))
os.Stderr.Write([]byte(" -- Table structure for download_list\n"))
os.Stderr.Write([]byte(" -- ----------------------------\n"))
os.Stderr.Write([]byte(" DROP TABLE IF EXISTS `hlsget_downloads`;\n"))
os.Stderr.Write([]byte(" CREATE TABLE `hlsget_downloads` (\n"))
os.Stderr.Write([]byte(" `id` int(11) NOT NULL AUTO_INCREMENT,\n"))
os.Stderr.Write([]byte(" `url` varchar(256) NOT NULL,\n"))
os.Stderr.Write([]byte(" `status` int(11) NOT NULL DEFAULT '0',\n"))
os.Stderr.Write([]byte(" `dest` varchar(256) DEFAULT NULL,\n"))
os.Stderr.Write([]byte(" `ret_code` int(11) DEFAULT '0',\n"))
os.Stderr.Write([]byte(" `ret_msg` varchar(128) DEFAULT NULL,\n"))
os.Stderr.Write([]byte(" PRIMARY KEY (`id`),\n"))
os.Stderr.Write([]byte(" UNIQUE KEY `url` (`url`)\n"))
os.Stderr.Write([]byte(" ) ENGINE=InnoDB AUTO_INCREMENT=393211 DEFAULT CHARSET=latin1;\n"))
}
func NewMySQLDl(host string, port uint, db string, table string, username string, password string) *Dl_MySQL {
dl := new(Dl_MySQL)
dl.host = host
dl.port = port
dl.username = username
dl.password = password
dl.db = db
dl.table = table
var dburi string
if password != "" {
dburi = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, db)
} else {
dburi = fmt.Sprintf("%s@tcp(%s:%d)/%s", username, host, port, db)
}
pdb, err := sql.Open("mysql", dburi)
if err != nil {
return nil
} else {
dl._pdb = pdb
}
return dl
}
func (self *Dl_MySQL) NextLinks(limit int) ([]string, error) {
sql := "SELECT id, url FROM " + self.table + " WHERE status = 0 LIMIT ?"
ret := []string{}
ids := []interface{}{}
rows, err := self._pdb.Query(sql, limit)
if nil != err {
log.Errorln("NextLinks[1]:", err)
return ret, err
}
for rows.Next() {
var id int
var link string
e := rows.Scan(&id, &link)
if nil != e {
log.Errorln("NextLinks[2]:", e)
break;
}
ret = append(ret, link)
ids = append(ids, id)
}
if len(ids) > 0 {
sql = "UPDATE " + self.table + " SET status=? WHERE id IN (?" + strings.Repeat(",?", len(ids)-1) + ")"
args := []interface{}{1}
//for _, i := range ids {
// args = append(args, i)
//}
args = append(args, ids...)
_, e := self._pdb.Exec(sql, args...)
if e != nil {
log.Errorln("NextLinks[3]:", e)
}
}
return ret, nil
//return nil, errors.New("Not implemented!")
}
func (self *Dl_MySQL) SubmitResult(link string, dest string, ret_code int, ret_msg string) {
log.Infoln("DL >", link, dest, ret_code, ret_msg)
status := 2
if ret_code != 0 {
status = -1
}
_, e := self._pdb.Exec("UPDATE "+self.table+" SET status=?, dest=?, ret_code=?, ret_msg=? WHERE url = ?", status,
dest, ret_code, ret_msg, link)
if e != nil {
log.Errorln("SubmitResult[1]:", e)
}
}