-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
124 lines (104 loc) · 3.25 KB
/
core.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
package main
import (
"encoding/xml"
"io/ioutil"
"net/http"
)
var NicoAPIUrls = map[string]string{
"login": "https://secure.nicovideo.jp/secure/login?site=niconico",
"getthumbinfo": "http://ext.nicovideo.jp/api/getthumbinfo/",
"getflv": "http://flapi.nicovideo.jp/api/getflv/",
}
type NicoVideoErrorInfo struct {
XMLName xml.Name `xml:"error"`
Code string `xml:"code"`
Description string `xml:"description"`
}
type NicoVideoTag struct {
XMLName xml.Name `xml:"tag"`
Lock string `xml:"lock,attr"`
Value string `xml:",innerxml"`
}
type NicoVideoTags struct {
XMLName xml.Name `xml:"tags"`
Domain string `xml:"domain,attr"`
Tag []NicoVideoTag `xml:"tag"`
}
type NicoVideoInfo struct {
XMLName xml.Name `xml:"thumb"`
VideoId string `xml:"video_id"`
Title string `xml:"title"`
Description string `xml:"description"`
ThumbnailUrl string `xml:"thumbnail_url"`
FirstRetrieve string `xml:"first_retrieve"`
Length string `xml:"length"`
MovieType string `xml:"movie_type"`
SizeHigh int `xml:"size_high"`
SizeLow int `xml:"size_low"`
ViewCounter int `xml:"view_counter"`
CommentNum int `xml:"comment_num"`
MylistCounter int `xml:"mylist_counter"`
LastResBody string `xml:"last_res_body"`
WatchUrl string `xml:"watch_url"`
ThumbType string `xml:"thumb_type"`
Embeddable int `xml:"embeddable"`
NoLivePlay int `xml:"no_live_play"`
Tags []NicoVideoTags `xml:"tags"`
UserId int `xml:"user_id"`
}
type NicoVideoThumbResponse struct {
XMLName xml.Name `xml:"nicovideo_thumb_response"`
Status string `xml:"status,attr"`
ErrorInfo NicoVideoErrorInfo `xml:"error"`
VideoInfo NicoVideoInfo `xml:"thumb"`
}
func GetVideoThumbResponse(videoId string) (result NicoVideoThumbResponse, err error) {
resp, _ := http.Get(NicoAPIUrls["getthumbinfo"] + videoId)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
err = xml.Unmarshal(body, &result)
return
}
func GetVideoTitle(videoId string) (title string) {
resp, err := GetVideoThumbResponse(videoId)
if err != nil {
return
}
if resp.Status != "ok" {
return
}
title = resp.VideoInfo.Title
return
}
func GetAllKindsOfVideoTags(videoId string) (allTags []string, lockedTags []string, unlockedTags []string) {
resp, err := GetVideoThumbResponse(videoId)
if err != nil {
return
}
if resp.Status != "ok" {
return
}
videoInfo := resp.VideoInfo
for _, tag := range videoInfo.Tags[0].Tag {
allTags = append(allTags, tag.Value)
switch tag.Lock {
case "1":
lockedTags = append(lockedTags, tag.Value)
default:
unlockedTags = append(unlockedTags, tag.Value)
}
}
return
}
func GetVideoTags(videoId string) (tags []string) {
tags, _, _ = GetAllKindsOfVideoTags(videoId)
return
}
func GetLockedVideoTags(videoId string) (lockedTags []string) {
_, lockedTags, _ = GetAllKindsOfVideoTags(videoId)
return
}
func GetUnlockedVideoTags(videoId string) (unlockedTags []string) {
_, _, unlockedTags = GetAllKindsOfVideoTags(videoId)
return
}