-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (187 loc) · 5.3 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"fuskbreak/pager"
"log"
"os"
"strconv"
"strings"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
)
const version string = "1.1"
// Page is the main page struct
type Page struct {
Url string // page url
Type string // video / pictures
Text *string // the html text of the page
Dom *html.Node // the page converted to dom
Title string // meta=description content
Thumbnail string // meta[property="og:image"] content
VideoURI string // link to the video directly
}
type mapPages map[string]*Page
var subPages mapPages // Sub Pages
var fifoPages []string // Sub Pages
// PageCreator create a page
func PageCreator(url string) Page {
//log.Print("Fethcing Page: ", url)
var Type string
if strings.Contains(url, "/video/") {
Type = "video"
} else if strings.Contains(url, "/pictures/") {
Type = "pictures"
}
// else {
// log.Fatal("Unknown type in url")
// }
//log.Println(" - Type: ", Type)
text, err := pager.GetPage(url)
if err != nil {
log.Fatal(err)
}
dom, err := html.Parse(strings.NewReader(*text))
if err != nil {
log.Fatal(err)
}
return Page{Url: url, Type: Type, Text: text, Dom: dom}
}
// GetItems get all items
func (p *Page) GetItems(size int) []string {
var err error
var hrefs []string
// compile selector
s, err := cascadia.Compile(".Timestream-item a")
if err != nil {
log.Fatal(err)
}
// get all items
matches := s.MatchAll(p.Dom)
//match := s.MatchFirst(doc)
for _, match := range matches {
got := pager.NodeString(match)
//fmt.Println("Results:", got)
href, err := pager.ParseHref(got)
if err != nil {
log.Fatal(err)
}
log.Println("HREF:", href)
hrefs = append(hrefs, href)
}
return hrefs
}
// GetInfo get info on the page
func (p *Page) GetInfo() {
var err error
// compile selectors
sDescription, err := cascadia.Compile("meta[name=\"description\"]")
if err != nil {
log.Fatal(err)
}
match := sDescription.MatchFirst(p.Dom)
p.Title = *pager.GetAttr(match, "content")
sImage, err := cascadia.Compile("meta[property=\"og:image\"]")
if err != nil {
log.Fatal(err)
}
match = sImage.MatchFirst(p.Dom)
p.Thumbnail = *pager.GetAttr(match, "content")
}
// GetVideo gets the video
func (p *Page) GetVideo() {
var err error
// compile selectors
sDescription, err := cascadia.Compile("meta[name=\"embed_video_url\"]")
if err != nil {
log.Fatal(err)
}
match := sDescription.MatchFirst(p.Dom)
embedURL := pager.GetAttr(match, "content")
if embedURL == nil {
log.Println("embed_video_url Not found in page")
return
}
embedPage := PageCreator(*embedURL)
p.VideoURI = pager.StupidJSON("videoUri", *embedPage.Text)
p.VideoURI = strings.Replace(p.VideoURI, "496_kbps", "864_kbps", 1) // upgrade the kbps
//log.Println("VideoURI:", p.VideoURI)
}
// GetByType get page by it's type
func (p *Page) GetByType() {
p.GetInfo()
switch p.Type {
case "pictures":
log.Println("Pictures not implemented yet")
case "video":
p.GetVideo()
default:
//p.GetItems()
}
}
// --------------------------------------------------------------------------
func fetcher(url string, pages *mapPages, ch chan string) {
log.Println("Fetcher: ", url)
page := PageCreator(url)
page.GetByType()
(*pages)[url] = &page
ch <- url
}
func main() {
// check for parameters
if len(os.Args) <= 1 {
log.Fatal("Usage ", os.Args[0], " [number of posts to fetch]")
}
size, err := strconv.Atoi(os.Args[1])
if size <= 0 || err != nil {
log.Println("Usage ", os.Args[0], " [number of posts to fetch]")
log.Fatal("Error: ", err)
}
subPages = make(mapPages)
// fetch main pages
log.Println("START fuskbreak version:" + version)
for page := 0; len(fifoPages) < size; page++ {
nextPage := PageCreator("http://www.break.com/" + strconv.Itoa(page))
moreItems := nextPage.GetItems(size)
for _, href := range moreItems { // merging maps and adding to mainPage
_, ok := subPages[href]
if !ok {
subPages[href] = nil
fifoPages = append(fifoPages, href)
}
}
log.Println("Current items: ", len(fifoPages), " items")
}
// start fetching items (using go routines)
log.Println("Found ", len(fifoPages), " items")
ch := make(chan string)
for _, url := range fifoPages {
go fetcher(url, &subPages, ch)
}
// waiting for fetchers to finish
log.Println("Waiting ...")
for size = len(fifoPages); size > 0; size-- {
url := <-ch
log.Println(size, ". Received: ", url)
}
// echo results to the screen
var str string
str += "<!doctype html><html><head><title>break.com fuskator</title><meta charset=\"UTF-8\"><style>html {text-align: center} footer {padding: 40px}</style></head><body><h1>break.com fuskator</h1>"
n := 0
for _, url := range fifoPages {
n++
p, ok := subPages[url]
if !ok || p == nil {
continue
}
str += "<hr><section><a href=\"" + p.Url + "\" target=\"blank\"><h1>" + strconv.Itoa(n) + ". " + p.Title + " (" + p.Type + ") </a></h1>"
if p.VideoURI != "" {
str += "<br><a href=\"" + p.VideoURI + "\" target=\"blank\"><img src=\"" + p.Thumbnail + "\" style=\"width:25%\"></a>"
} else {
str += "<br><a href=\"" + p.Url + "\" target=\"blank\"><img src=\"" + p.Thumbnail + "\" style=\"width:25%\"></a>"
}
str += "</section>"
}
str += "<br><br><footer>Made by fuskbreak (Cnaan Aviv)</footer></body></html>"
fmt.Println(str)
log.Println("END")
}