-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMoviesApi.go
139 lines (109 loc) · 3.87 KB
/
MoviesApi.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
package main
import (
"encoding/json"
"fmt"
"github.com/gocolly/colly/v2"
"net/http"
"regexp"
"strings"
)
type item struct {
Title string
Link string
Poster string
}
// Returns JSON Array for Movies
func getContents(cType string, pageNo string) string {
c := colly.NewCollector()
var tempString string
c.OnHTML("#main_bg > div:nth-child(5) > div > div.vc_row.wpb_row.vc_row-fluid.vc_custom_1404913114846 > div.vc_col-sm-12.wpb_column.column_container > div > div > ul > li > a", func(e *colly.HTMLElement) {
temp := item{Title: e.ChildText("div.name"), Link: "https://vidcloud9.com" + e.Attr("href"), Poster: e.ChildAttr("div.img>div.picture>img", "src")}
b, _ := json.Marshal(temp)
tempString += string(b) + ","
})
/* I know this is not Optimal solution for returning as JSON
I am new to Go Language
If you know how to return struct as JSON array please teach me !!
*/
c.Visit("https://vidcloud9.com/" + cType + "?page=" + pageNo)
i := strings.LastIndex(tempString, ",")
excludingLast := tempString[:i] + strings.Replace(tempString[i:], ",", "", 1)
results := "[" + excludingLast + "]"
return results
}
func fixStreamLink(streamLink string) string {
var jsScript string
var re = regexp.MustCompile(`(?m)file: '(.*?)'`)
c := colly.NewCollector()
c.OnHTML("body>div>div>script", func(element *colly.HTMLElement) {
jsScript = element.Text
})
c.Visit(streamLink)
var match = re.FindString(jsScript)
fixString := strings.ReplaceAll(match, "file: '", "")
fixString = strings.ReplaceAll(fixString, "'", "")
return fixString
}
func getDirectLink(videoLink string) string {
type directLink struct {
Url string
}
var fixedUrl string
c := colly.NewCollector()
c.OnHTML("#main_bg > div:nth-child(5) > div > div.video-info-left > div.watch_play > div.play-video > iframe", func(element *colly.HTMLElement) {
fixedUrl = fixStreamLink("https:" + strings.ReplaceAll(element.Attr("src"), "streaming.php", "loadserver.php"))
})
c.Visit(videoLink)
temp := directLink{Url: fixedUrl}
b, _ := json.Marshal(temp)
return string(b)
}
func getEpisodes(videoLink string) string {
type episodesJsonArray struct {
EpisodeNumber string
EpisodeUrl string
}
var tempString string
c := colly.NewCollector()
c.OnHTML("#main_bg > div:nth-child(5) > div > div.video-info-left > ul > li > a", func(element *colly.HTMLElement) {
temp := episodesJsonArray{EpisodeNumber: element.ChildText("div.name"), EpisodeUrl: "https://vidcloud9.com" + element.Attr("href")}
b, _ := json.Marshal(temp)
tempString += string(b) + ","
})
c.Visit(videoLink)
i := strings.LastIndex(tempString, ",")
excludingLast := tempString[:i] + strings.Replace(tempString[i:], ",", "", 1)
results := "[" + excludingLast + "]"
return results
}
func moviesHandler(w http.ResponseWriter, r *http.Request) {
pageNo := r.URL.Query().Get("pageNo")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, getContents("movies", pageNo))
}
func seriesHandler(w http.ResponseWriter, r *http.Request) {
pageNo := r.URL.Query().Get("pageNo")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, getContents("series", pageNo))
}
func directLinkHandler(w http.ResponseWriter, r *http.Request) {
videoLink := r.URL.Query().Get("videoLink")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, getDirectLink(videoLink))
}
func episodesHandler(w http.ResponseWriter, r *http.Request) {
videoLink := r.URL.Query().Get("videoLink")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, getEpisodes(videoLink))
}
func main() {
http.HandleFunc("/movies", moviesHandler)
http.HandleFunc("/series", seriesHandler)
http.HandleFunc("/directLink", directLinkHandler)
http.HandleFunc("/episodes", episodesHandler)
http.ListenAndServe(":8080", nil)
}