/
tasks.go
177 lines (165 loc) · 4.35 KB
/
tasks.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
// Copyright 2017 Yoshi Yamaguchi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/tebeka/selenium"
)
const Timeout = 20 * time.Second
const (
TDMBProgramList = "https://tv.yahoo.co.jp/listings/23/"
BSProgramList = "https://tv.yahoo.co.jp/listings/bs1/"
)
const (
ProgramWaitVisible = `//*[@id="tvpgm"]`
ProgramPath = `//*[@id="tvpgm"]/table/tbody/tr/td/table/tbody/tr/td/span/a`
ProgramNextPage = `//*[@id="nexttime"]/a`
DetailedPageTitle = `//*[@id="main"]/div[1]/div/div/div[1]/h2/b`
DetailedPageTime = `//*[@id="main"]/div[1]/div/div/div[1]/p/em`
DetailedPageProvider = `//*[@itemprop="provider"]`
DetailedPageProvider2 = `//*[@id="yjContentsBody"]/div[1]/div[1]/p`
)
// WaitForXPath
func WaitForXPath(xpath string) selenium.Condition {
cond := func(wd selenium.WebDriver) (bool, error) {
target, err := wd.FindElement(selenium.ByXPATH, xpath)
if err != nil {
return false, err
}
return target.IsDisplayed()
}
return cond
}
func filterProgramWithTitle(r Result) bool {
patterns := programTitleFilter()
for _, p := range patterns {
if p.MatchString(r.Title) {
return true
}
}
return false
}
// fetchProgramsOn get all the URLs to the program detail pages and iss link title on a program list.
func fetchProgramsOn(wd selenium.WebDriver, url string, ch chan<- Result) error {
log.Println(url)
if err := wd.Get(url); err != nil {
return err
}
err := wd.WaitWithTimeout(WaitForXPath(ProgramWaitVisible), Timeout)
if err != nil {
return err
}
elems, err := wd.FindElements(selenium.ByXPATH, ProgramPath)
if err != nil {
return err
}
for _, e := range elems {
url, err := e.GetAttribute("href")
if err != nil {
fmt.Printf("href error: %s\n", err)
}
title, err := e.Text()
if err != nil {
fmt.Printf("text error: %s\n", err)
}
r := NewResult(url, title)
if filterProgramWithTitle(r) {
ch <- r
}
}
return nil
}
// fetchPrograms repeat fetchProgramsOn for all available progrm list pages.
func fetchPrograms(wd selenium.WebDriver, startURL string, ch chan<- Result, done chan<- bool) error {
if err := fetchProgramsOn(wd, startURL, ch); err != nil {
return err
}
curURL, err := wd.CurrentURL()
if err != nil {
return err
}
for {
err = wd.WaitWithTimeout(WaitForXPath(ProgramNextPage), Timeout)
if err != nil {
return err
}
elem, err := wd.FindElement(selenium.ByXPATH, ProgramNextPage)
if err != nil {
return err
}
url, err := elem.GetAttribute("href")
if err != nil {
return err
}
if url == curURL {
break
}
curURL = url
fetchProgramsOn(wd, url, ch)
time.Sleep(3 * time.Second)
}
done <- true
return nil
}
func getDetailedPage(wd selenium.WebDriver, url string) (*Page, error) {
if !strings.HasPrefix(url, "http") {
return nil, fmt.Errorf("URL must start with http(s)")
}
if err := wd.Get(url); err != nil {
return nil, err
}
if err := wd.WaitWithTimeout(WaitForXPath(DetailedPageTitle), Timeout); err != nil {
return nil, err
}
elem, err := wd.FindElement(selenium.ByXPATH, DetailedPageTitle)
if err != nil {
return nil, err
}
title, err := elem.Text()
if err != nil {
return nil, err
}
title = replaceChar(title)
elem, err = wd.FindElement(selenium.ByXPATH, DetailedPageTime)
if err != nil {
return nil, err
}
t, err := elem.Text()
if err != nil {
return nil, err
}
elem, err = wd.FindElement(selenium.ByXPATH, DetailedPageProvider)
if err != nil {
elem, err = wd.FindElement(selenium.ByXPATH, DetailedPageProvider2)
if err != nil {
return nil, err
}
}
provider, err := elem.Text()
if err != nil {
return nil, err
}
p := ProviderMap[provider]
return NewPage(url, title, p, t)
}
func replaceChar(s string) string {
s = strings.TrimSpace(s)
for k, v := range ReplaceCharMap {
s = strings.Replace(s, k, v, -1)
}
return s
}