Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | package utils |
||
2 | |||
3 | import ( |
||
4 | "regexp" |
||
5 | |||
6 | "github.com/SerhiiCho/shoshka-go/models" |
||
7 | ) |
||
8 | |||
9 | // GetPostsFromHTML returns all html posts |
||
10 | func GetPostsFromHTML(html *string) []string { |
||
11 | 1 | linksRegex := "<article class(.|\n)*?<\\/article>" |
|
12 | 1 | return regexp.MustCompile(linksRegex).FindAllString(*html, -1) |
|
13 | } |
||
14 | |||
15 | // GetAllInformation parses anchor tags and takes image src, |
||
16 | // link url and title. |
||
17 | func GetAllInformation(html *[]string) []models.PhotoReport { |
||
18 | 1 | var photoReport models.PhotoReport |
|
19 | 1 | var result []models.PhotoReport |
|
20 | |||
21 | 1 | compiledTitle := regexp.MustCompile("<h3 (.|\\n)*?[^ ]>(.*)<\\/a>") |
|
22 | 1 | compiledImage := regexp.MustCompile(" src=\"([\\S]+)\" ") |
|
23 | 1 | compiledURL := regexp.MustCompile(" href=[\"|']([\\S]+)(\"|') ") |
|
24 | |||
25 | 1 | for _, tag := range *html { |
|
26 | 1 | photoReport.Title = compiledTitle.FindStringSubmatch(tag)[2] |
|
27 | 1 | photoReport.Image = compiledImage.FindStringSubmatch(tag)[1] |
|
28 | 1 | photoReport.URL = compiledURL.FindStringSubmatch(tag)[1] |
|
29 | |||
30 | 1 | result = append(result, photoReport) |
|
31 | } |
||
32 | |||
33 | 1 | return result |
|
34 | } |
||
35 | |||
36 | // ParseErrors returns slice of errors |
||
37 | func ParseErrors(text string) []string { |
||
38 | 1 | return regexp.MustCompile("\\[([A-z0-9 :-]+)\\] (.*)").FindAllString(text, -1) |
|
39 | } |
||
40 |