-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
155 lines (139 loc) · 4.6 KB
/
utils.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"sync"
)
// Video structure containing all metadata for the video
type Video struct {
ID string
Title string
Annotations string
Thumbnail string
Description string
Path string
RawHTML string
STS float64
InfoJSON infoJSON
playerArgs map[string]interface{}
RawFormats []url.Values
}
// Tracklist structure containing all subtitles tracks for the video
type Tracklist struct {
Tracks []Track `xml:"track"`
}
// Track structure for data about single subtitle
type Track struct {
LangCode string `xml:"lang_code,attr"`
Lang string `xml:"lang_translated,attr"`
}
// infoJSON structure containing the generated json data
type infoJSON struct {
ID string `json:"id"`
Uploader string `json:"uploader"`
UploaderID string `json:"uploader_id"`
UploaderURL string `json:"uploader_url"`
UploadDate string `json:"upload_date"`
License string `json:"license,omitempty"`
Creator string `json:"creator,omitempty"`
Title string `json:"title"`
AltTitle string `json:"alt_title,omitempty"`
Thumbnail string `json:"thumbnail"`
Description string `json:"description"`
Category string `json:"category"`
Tags []string `json:"tags"`
Subtitles map[string][]Subtitle `json:"subtitles"`
Duration float64 `json:"duration"`
AgeLimit float64 `json:"age_limit"`
Annotations string `json:"annotations"`
WebpageURL string `json:"webpage_url"`
ViewCount float64 `json:"view_count"`
LikeCount float64 `json:"like_count"`
DislikeCount float64 `json:"dislike_count"`
AverageRating float64 `json:"average_rating"`
Formats []Format `json:"formats"`
subLock sync.Mutex
}
// Subtitle struct hold subtitle data
type Subtitle struct {
URL string `json:"url"`
Ext string `json:"ext"`
}
// Format structure for all different formats informations
type Format struct {
FormatID string `json:"format_id"`
Ext string `json:"ext"`
URL string `json:"url"`
Height float64 `json:"height,omitempty"`
Width float64 `json:"width,omitempty"`
FormatNote string `json:"format_note"`
Bitrate float64 `json:"bitrate"`
Fps float64 `json:"fps,omitempty"`
Format string `json:"format"`
Clen float64 `json:"clen,omitempty"`
EOTF string `json:"eotf,omitempty"`
Index string `json:"index,omitempty"`
Init string `json:"init,omitempty"`
Lmt float64 `json:"lmt,omitempty"`
Primaries string `json:"primaries,omitempty"`
QualityLabel string `json:"quality_label,omitempty"`
Type string `json:"type"`
Size string `json:"size,omitempty"`
}
// JSONMarshalIndentNoEscapeHTML allow proper json formatting
func JSONMarshalIndentNoEscapeHTML(i interface{}, prefix string, indent string) ([]byte, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent(prefix, indent)
err := encoder.Encode(i)
return buf.Bytes(), err
}
func genPath(video *Video) error {
if _, err := os.Stat(video.Path); os.IsNotExist(err) {
err = os.MkdirAll(video.Path, 0755)
if err != nil {
return err
}
}
return nil
}
func checkFiles(video *Video) error {
// Define path based on the video ID
firstLayer := video.ID[:1]
secondLayer := video.ID[:3]
video.Path = arguments.Output + "/" + firstLayer + "/" + secondLayer + "/" + video.ID + "/"
// Check if the path contain at least 3 files
// if not, return an error
files, err := ioutil.ReadDir(video.Path)
if err == nil && len(files) >= 3 {
return errors.New("this video has already been archived")
}
return nil
}
func writeFiles(video *Video) error {
// write description
descriptionFile, err := os.Create(video.Path + video.ID + "_" + video.Title + ".description")
if err != nil {
return err
}
defer descriptionFile.Close()
// write info json file
infoFile, err := os.Create(video.Path + video.ID + "_" + video.Title + ".info.json")
if err != nil {
return err
}
defer infoFile.Close()
fmt.Fprintf(descriptionFile, "%s", video.Description)
JSON, err := JSONMarshalIndentNoEscapeHTML(video.InfoJSON, "", " ")
if err != nil {
return err
}
fmt.Fprintf(infoFile, "%s", string(JSON))
return nil
}