-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (129 loc) · 3.04 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
package main
import(
"net/http"
"net/url"
"log"
"flag"
"io/ioutil"
"fmt"
"golang.org/x/net/html"
"strings"
"encoding/json"
"os"
"html/template"
"strconv"
// "sync"
)
type fileInfo struct{
Title string `json:"Title"`;
Year string `json:"Year"`;
Runtime string `json:"Runtime"`;
Genre string `json:"Genre"` ;
Rating string `json:"imdbRating"`;
Description string `json:"Plot"`;
Image string `json:"Poster"`;
Awards string `json:"Awards"`;
}
type node struct{
Movie fileInfo
Left *(node)
Right *(node)
}
var movie struct{
Name string;
Year string;
}
var root *node
var Movies []fileInfo
func main() {
// goGroup := new(sync.WaitGroup)
flag.Parse()
files, _ := ioutil.ReadDir(flag.Args()[0])
var queryNames []string
for _, f := range files {
queryNames= append(queryNames,url.QueryEscape(f.Name()))
}
fmt.Println("Preparing data")
for i:=0;i<len(queryNames);i++{
go GetTitleAndYear("https://opensubtitles.co/search?q=" + queryNames[i])
}
http.HandleFunc("/",ShowRatings)
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/static"))))
log.Fatal(http.ListenAndServe(":8080",nil))
}
func ShowRatings(w http.ResponseWriter,r *http.Request){
t,err := template.ParseFiles(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/template/index.tpl")
if(err!=nil){
log.Fatal(err)
}
t.Execute(w,Movies)
}
func GetTitleAndYear(url string){
resp,err := http.Get(url)
if err!=nil{
fmt.Println(err)
GetTitleAndYear(url)
return
}
defer resp.Body.Close()
var movieData string
if resp.StatusCode != 200 {
fmt.Println("statuscode",err)
}
z := html.NewTokenizer(resp.Body)
for{
tt := z.Next()
if tt == html.ErrorToken{
return
}else if tt==html.StartTagToken{
t:= z.Token()
if t.Data=="h4"{
tt = z.Next()
tt = z.Next()
tt = z.Next()
t = z.Token()
movieData = strings.TrimSpace(t.Data)
break
}
}
}
movie.Name = movieData[:len(movieData)-6]
movie.Year = movieData[len(movieData)-5:len(movieData)-1]
movie.Name = strings.Replace(movie.Name, " ", "+", -1)
url = "http://www.omdbapi.com/?t=" + movie.Name + "&y=" + movie.Year + "&plot=short&r=json"
req,err := http.Get(url)
if err!=nil{
log.Fatal(err)
}
x := fileInfo{}
jsonParser := json.NewDecoder(req.Body)
if err := jsonParser.Decode(&x); err != nil {
log.Fatal("parsing config file", err)
}
if x == (fileInfo{}){
return
}
root = InsertTree(root,x)
Movies = nil
InorderTraversal(root)
fmt.Println(x.Title,x.Year)
}
func InsertTree(leaf *node,x fileInfo) *node{
a,_ := strconv.ParseFloat(x.Rating,32)
if leaf == nil{
return &node{x,nil,nil}
}else if b,_ := strconv.ParseFloat(leaf.Movie.Rating,32); a>b{
leaf.Left = InsertTree(leaf.Left,x)
return leaf
}
leaf.Right = InsertTree(leaf.Right,x)
return leaf
}
func InorderTraversal(leaf *node){
if leaf == nil{
return
}
InorderTraversal(leaf.Left)
Movies = append(Movies,leaf.Movie)
InorderTraversal(leaf.Right)
}