/
urlmaker.go
96 lines (84 loc) · 2.9 KB
/
urlmaker.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
package main
import (
"net/url"
"time"
"github.com/bohai/qunar/utils"
"github.com/go-ini/ini"
)
func getLatestFriday() time.Time {
var day0 time.Time
for i := 0; i < 7; i++ {
day0 = time.Now().AddDate(0, 0, i)
if day0.Weekday() == time.Friday {
break
}
}
return day0
}
func getDayMap() map[time.Time]time.Time {
t := getLatestFriday()
daymap := make(map[time.Time]time.Time)
var day0, day1 time.Time
for i := 0; i < max; i++ {
day0 = t.AddDate(0, 0, i*7)
day1 = t.AddDate(0, 0, i*7+3)
daymap[day0] = day1
}
return daymap
}
// t1: start time
// t2: end time
// city1, code1: departure city and code
// city2, code2: arrive city and code
func getURL(t1, t2 time.Time, city1, city2, code1, code2 string) string {
codedURL := "http://flight.qunar.com/twell/longwell?&tags=2&http%3A%2F%2Fwww.travelco.com%2FsearchArrivalAirport=%E5%8E%A6%E9%97%A8&http%3A%2F%2Fwww.travelco.com%2FsearchDepartureAirport=%E8%A5%BF%E5%AE%89&http%3A%2F%2Fwww.travelco.com%2FsearchDepartureTime=2016-06-03&http%3A%2F%2Fwww.travelco.com%2FsearchReturnTime=2016-06-06&locale=zh&nextNDays=0&op=1&reset=true&searchLangs=zh&searchType=RoundTripFlight&version=thunder&mergeFlag=0&xd=f1464442662000&wyf=0P8FlQr%2FfPPylQy0ERPHWlr%2FfYE%2FWlP%2FbEw%2FlUd8lyeFlUd%2F%7C1441321882698&fromCity=%E8%A5%BF%E5%AE%89&toCity=%E5%8E%A6%E9%97%A8&fromDate=2016-06-03&toDate=2016-06-06&fromCode=SIA&toCode=XMN&from=flight_dom_search&lowestPrice=null&_token=30768"
rawURL, _ := url.QueryUnescape(codedURL)
u, _ := url.Parse(rawURL)
q := u.Query()
q.Set("http://www.travelco.com/searchDepartureAirport", city1)
q.Set("http://www.travelco.com/searchArrivalAirport", city2)
q.Set("http://www.travelco.com/searchDepartureTime", t1.Format(layout))
q.Set("http://www.travelco.com/searchReturnTime", t2.Format(layout))
q.Set("fromCity", city1)
q.Set("toCity", city2)
q.Set("fromCode", code1)
q.Set("toCode", code2)
q.Set("fromDate", t1.Format(layout))
q.Set("toDate", t2.Format(layout))
u.RawQuery = q.Encode()
return u.String()
}
const layout = "2006-01-02"
var fromCitys, toCitys []*ini.Key
var max int
func readConfig() {
cfg, err := ini.Load("./qunar.ini")
if err != nil {
panic(err)
}
sec := cfg.Section("from_citys")
fromCitys = sec.Keys()
sec = cfg.Section("to_citys")
toCitys = sec.Keys()
max = cfg.Section("common").Key("max").MustInt(8)
}
func main() {
readConfig()
db := dbutils.NewDB1()
defer db.Close()
stmt, err := db.Prepare("INSERT INTO urls(url, date) values(?, ?)")
if err != nil {
panic(err)
}
defer stmt.Close()
days := getDayMap()
for d1, d2 := range days {
for i, code1 := range fromCitys {
for j, code2 := range toCitys {
//fmt.Println(d1, d2, fromCitys[i].Name(), toCitys[j].Name(), code1.Value(), code2.Value(), time.Now())
stmt.Exec(getURL(d1, d2, fromCitys[i].Name(), toCitys[j].Name(), code1.Value(), code2.Value()), time.Now())
//fmt.Println(getURL(d1, d2, city1, city2, code1, code2))
}
}
}
}