video.go 3.68 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
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
package main

import (
	"html"
	"net/url"
	"regexp"
	"strconv"
	"strings"
)

type videoContainer struct {
	url      string
	title    string
	duration string
	user     string
	date     string
	views    string
}

type videoSort struct {
	slice []*videoContainer
}

var (
	videoPatternSearch = regexp.MustCompile(`<div class="yt-lockup-content">.*?href="/watch\?v=(.*?)("|\&).*?title="(.*?)".*?> - Duration: (.*?)\..*?<a href="/user/(.*?)".*?<li>(.*?) ago</li>.*?<li>(.*?) views</li>`)
26
	videoPatternUser   = regexp.MustCompile(`<h3 class="yt-lockup-title.*?title="(.*?)".*?href="/watch\?v=(.*?)("|\&).*?> - Duration: (.*?)\.(?s:.*?)<li>(.*?) views</li>.*?<li>(.*?) ago</li>`)
Jonny Schäfer's avatar
Jonny Schäfer committed
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
)

// videoList returns a list of found videos of the given page. If an error occured nil is returned.
func videoListSearch(title, page string) []*videoContainer {
	videos := []*videoContainer{}
	content := webReadString("https://www.youtube.com/results?search_query=" + url.QueryEscape(title) + "&page=" + page)
	if content == "" {
		return nil
	}

	for _, block := range videoPatternSearch.FindAllStringSubmatch(content, -1) {
		url := block[1]
		title := html.UnescapeString(block[3])
		duration := block[4]
		user := html.UnescapeString(block[5])
		date := block[6]
		views := block[7]

		videos = append(videos, &videoContainer{url, title, duration, user, date, views})
	}

	return videos
}

// videoList returns a list of found videos of the given page. If an error occured nil is returned.
func videoListUser(user string) []*videoContainer {
	videos := []*videoContainer{}
	content := webReadString("https://www.youtube.com/user/" + url.QueryEscape(user) + "/videos")
	if content == "" {
		return nil
	}

	for _, block := range videoPatternUser.FindAllStringSubmatch(content, -1) {
		title := html.UnescapeString(block[1])
		url := block[2]
		duration := videoConvertDuration(block[4])
		views := block[5]
		date := block[6]
		user := user

		videos = append(videos, &videoContainer{url, title, duration, user, date, views})
	}

	return videos
}

// videoConvertDuration converts the given string to a format like hh:mm:ss
func videoConvertDuration(in string) (duration string) {
	stamp := []string{"00", "00", "00"}
	split := strings.Split(in, " ")

	for len(split) >= 2 {
		stamp[split[1][0]*2/11.0-18] += split[0] // TODO this will break
		split = split[2:]
	}
	for i, n := range stamp {
		stamp[i] = n[len(n)-2:]
	}
	if stamp[0] == "00" {
		stamp[0] = ""
	} else {
		stamp[0] = stamp[0] + ":"
	}

	duration = stamp[0] + stamp[1] + ":" + stamp[2]
	return
}

// Less implements the interface for sort
func (v *videoSort) Less(i, j int) bool {
	return videoIsYounger(v.slice[i], v.slice[j])
}

// Swap implements the interface for sort
func (v *videoSort) Swap(i, j int) {
	v.slice[i], v.slice[j] = v.slice[j], v.slice[i]
}

// Len implements the interface for sort
func (v *videoSort) Len() int {
	return len(v.slice)
}

110
111
// videoIsYounger compares the given two videos and returns true if the release
// date of young is more recent than the release date of old
Jonny Schäfer's avatar
Jonny Schäfer committed
112
func videoIsYounger(young, old *videoContainer) bool {
113
	return videoDateToSeconds(young.date) < videoDateToSeconds(old.date)
Jonny Schäfer's avatar
Jonny Schäfer committed
114
115
116
117
}

// videoDateToSeconds calculates the number of past seconds since the given date
func videoDateToSeconds(date string) int {
118
119
	date = strings.TrimPrefix(date, "Streamed ")

Jonny Schäfer's avatar
Jonny Schäfer committed
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
	split := strings.Split(date, " ")
	if len(split) < 2 {
		return 0
	}
	sec, err := strconv.Atoi(split[0])
	if err != nil {
		return 0
	}

	switch split[1][0:3] {
	case "sec":
		sec *= 1
	case "min":
		sec *= 60
	case "hou":
		sec *= 60 * 60
	case "day":
		sec *= 60 * 60 * 24
	case "wee":
		sec *= 60 * 60 * 24 * 7
	case "mon":
		sec *= 60 * 60 * 24 * 7 * 4.3
	case "yea":
		sec *= 60 * 60 * 24 * 7 * 4.3 * 12
	}

	return sec
}