-
Notifications
You must be signed in to change notification settings - Fork 3
/
movie.go
172 lines (157 loc) · 3.6 KB
/
movie.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
package main
import (
"fmt"
"os"
"strings"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"strconv"
"io"
"time"
"./pup"
"github.com/fatih/color"
)
type Yts struct {
SearchResults struct {
Status string `json:"status"`
Movies []struct {
Url string `json:"url"`
Img string `json:"img"`
Title string `json:"title"`
Year string `json:"year"`
} `json:"data"`
}
DownloadResults []struct {
Href string `json:"href"`
Text string `json:"text"`
Title string `json:"Title"`
}
}
var (
timeout = time.Duration(5 * time.Second)
HttpClient = http.Client{
Timeout: timeout,
}
YTS = Yts{}
MOVIES_VERSION string = "1.0.1"
language string
)
func UrlEncoded(str string) string {
u, err := url.Parse(str)
if err != nil {
return ""
}
return u.String()
}
func moviesResultCheck(length int){
if length <= 0 {
color.Set(color.FgMagenta, color.Bold)
fmt.Println("No movies found.")
color.Unset()
os.Exit(0)
}
}
func main(){
search := ParseFlages()
if strings.ToLower(language) == "tamil"{
SearchTamilMovies(search)
return
}
searchMovie("https://yts.am/ajax/search?query=",search)
moviesResultCheck(len(YTS.SearchResults.Movies))
fmt.Printf("%s",listMovies())
downloadIndex := getConformation(len(YTS.SearchResults.Movies))
torrentHtml := downloadHTML(YTS.SearchResults.Movies[downloadIndex].Url)
os.Args = []string{
"anbuksv",
"#movie-info > p a json{}",
}
nodes := pup.Run(torrentHtml)
json.Unmarshal(nodes,&YTS.DownloadResults)
color.Set(color.FgYellow, color.Bold)
for _,result:= range(YTS.DownloadResults){
color.New(color.FgYellow,color.Bold).Print("("+ result.Text +") ")
color.White(result.Href + "\n")
}
// fmt.Println(YTS.DownloadResults[len(YTS.DownloadResults)-1].Href) //By default high resolution torrent link will be printed
color.Unset()
}
func ParseFlages() string {
cmds := os.Args[1:]
nonFlagCmds := make([]string, len(cmds))
n := 0
for i := 0; i < len(cmds); i++ {
cmd := cmds[i]
switch cmd {
case "-h","--help":
PrintMoviesHelp(os.Stdout, 0)
case "--version":
fmt.Println(MOVIES_VERSION)
os.Exit(0)
case "-l","--language":
language = cmds[i+1]
i++
default:
nonFlagCmds[n] = cmds[i]
n++
}
}
return strings.Join(nonFlagCmds," ")
}
func PrintMoviesHelp(w io.Writer, exitCode int) {
helpString := `Usage
movies Name [flags]
Version
%s
Flags
-h --help display this help
--version display version
-l --language movie language
`
fmt.Fprintf(w, helpString, MOVIES_VERSION)
os.Exit(exitCode)
}
func getConformation(limit int) int {
var downloadIndex int
color.Set(color.FgWhite, color.Bold)
fmt.Print("awesome-movie> ")
color.Unset()
fmt.Scan(&downloadIndex)
downloadIndex = downloadIndex - 1
if downloadIndex < 0 || downloadIndex >= limit{
fmt.Println("awesome-movie> Please enter valid number.")
return getConformation(limit)
}
return downloadIndex
}
func onHttpError(err error){
color.Set(color.FgRed, color.Bold)
fmt.Println(err)
color.Unset()
os.Exit(0)
}
func searchMovie(url string, query string) {
resp,err := HttpClient.Get(url+UrlEncoded(query))
if err != nil{
onHttpError(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
json.Unmarshal([]byte(body),&YTS.SearchResults)
}
func downloadHTML(url string) io.ReadCloser {
resp,err := HttpClient.Get(url)
if err != nil {
onHttpError(err)
}
return resp.Body
}
func listMovies() string {
var _movies string = ""
for index,movie := range(YTS.SearchResults.Movies){
_movies = _movies + strconv.Itoa(index+1) + ". " + movie.Title + " (" + movie.Year + ")\n"
}
return _movies
}