-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_data.go
184 lines (168 loc) · 4.42 KB
/
video_data.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
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"flag"
"fmt"
"github.com/bilibili-data-statistics/data"
. "github.com/bilibili-data-statistics/tool/db/config"
"github.com/bilibili-data-statistics/tool/db/mysql"
"github.com/bilibili-data-statistics/tool/db/sqlite3"
"github.com/bilibili-data-statistics/tool/error"
"github.com/bilibili-data-statistics/tool/file"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const (
URL_PREFIX = "http://api.bilibili.com/archive_stat/stat?aid="
CONNECTION = "keep-alive"
DB_CONFIG_FILE = "db_config.json"
GROUP_COUNT = 200
)
// 开始 aid
var start uint64
// 结束 aid
var end uint64
// 写入同一数据库
var samedb bool
var client *http.Client
func main() {
// 解析命令行参数
flag.Uint64Var(&start, "start", 1, "the start aid (Include)")
flag.Uint64Var(&end, "end", 100, "the end aid (Exclude)")
flag.BoolVar(&samedb, "samedb", false, "write new data to an old database.")
flag.Parse()
if start > end {
fmt.Println("start aid is greater than end aid, so start and end exchange.")
start, end = end, start
}
if !file.Exists(DB_CONFIG_FILE) {
fmt.Println(DB_CONFIG_FILE + " file does not exist. use sqlite3 database.")
// 使用 SQLite3 数据库
dataToSqlite3()
return
}
configData, err := ioutil.ReadFile(DB_CONFIG_FILE)
error.CheckErr(err)
dbConfig := ParseDBConfig(configData)
fmt.Println("use_mysql=" + strconv.FormatBool(dbConfig.UseMysql))
if dbConfig.UseMysql {
// 使用 MySQL 数据库
dataToMysql(dbConfig)
} else {
// 使用 SQLite3 数据库
dataToSqlite3()
}
}
func dataToSqlite3() {
// 数据库
if file.Exists(sqlite3.DB_NAME) {
// 旧数据库文件存在
if !samedb {
currTime := time.Now().Format("20060102150405")
err := os.Rename(sqlite3.DB_NAME, strings.TrimRight(sqlite3.DB_NAME, ".db")+"-"+currTime+".db")
error.CheckErr(err)
sqlite3.InitDB()
}
} else {
sqlite3.InitDB()
}
var id int64
oldLastCount := start
lastCount := start
times := getGroupCount()
fmt.Println("times = " + strconv.FormatUint(times, 10))
for i := uint64(0); i < times; i++ {
oldLastCount = lastCount
if lastCount+GROUP_COUNT < end {
lastCount = lastCount + GROUP_COUNT
} else {
lastCount = end
}
groupData := getGroupData(oldLastCount, lastCount)
// 插入数据库
id = sqlite3.InsertGroupData(groupData)
fmt.Println("last insert id: " + strconv.FormatInt(id, 10))
}
}
func dataToMysql(config *Config) {
mysql.InitDB(config)
if !samedb {
currTime := time.Now().Format("20060102150405")
newTableName := mysql.TB_VIDEO_DATA + "-" + currTime
mysql.RenameTable(config, newTableName)
mysql.InitDB(config)
}
var id int64
oldLastCount := start
lastCount := start
times := getGroupCount()
fmt.Println("times = " + strconv.FormatUint(times, 10))
for i := uint64(0); i < times; i++ {
oldLastCount = lastCount
if lastCount+GROUP_COUNT < end {
lastCount = lastCount + GROUP_COUNT
} else {
lastCount = end
}
groupData := getGroupData(oldLastCount, lastCount)
// 插入数据库
id = mysql.InsertGroupData(config, groupData)
fmt.Println("last insert id: " + strconv.FormatInt(id, 10))
}
}
// 得到分组个数,决定了集中写数据库的次数
func getGroupCount() (times uint64) {
if (end-start+1)%GROUP_COUNT == 0 {
times = (end - start + 1) / GROUP_COUNT
} else {
times = (end-start+1)/GROUP_COUNT + 1
}
return times
}
func getGroupData(s uint64, e uint64) []*data.Data {
groupData := make([]*data.Data, e-s+1)
for i := s; i < e; i++ {
jsonStr := getVideoData(i)
if jsonStr == "" {
fmt.Errorf("%s", "av"+strconv.FormatUint(i, 10)+": result is nil")
continue
}
video := data.ParseVideoData(jsonStr)
if video.Code == 0 {
// 获取信息成功
groupData[i-s] = video.Data
fmt.Println("av" + strconv.FormatUint(video.Aid, 10) + " Success!")
} else {
fmt.Println("failed to fetch av" + strconv.FormatUint(i, 10) + " data!")
//fmt.Println(jsonStr)
}
}
return groupData
}
func getVideoData(aid uint64) (data string) {
if client == nil {
client = &http.Client{}
}
url := URL_PREFIX + strconv.FormatUint(aid, 10)
req, err := http.NewRequest("GET", url, nil)
error.CheckErr(err)
resp, err := client.Do(req)
if err != nil || resp == nil {
fmt.Errorf("%s", err)
return ""
}
body := resp.Body
defer body.Close()
if body != nil {
body, err := ioutil.ReadAll(resp.Body)
error.CheckErr(err)
data = string(body)
return
} else {
fmt.Errorf("%s", "body is nil")
return ""
}
}