-
Notifications
You must be signed in to change notification settings - Fork 2
/
imdb.go
108 lines (92 loc) · 3.28 KB
/
imdb.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
/*
* Copyright (c) 2015 Gaurav Mittal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"flag"
"regexp"
)
var Movie struct {
Response string
Type string
Title string
Year string
Rated string
Released string
Runtime string
Genre string
Director string
Writer string
Actors string
Plot string
Language string
Country string
ImdbRating string
Metascore string
ImdbVotes string
ImdbID string
Error string
}
func incorrectUsage() {
fmt.Printf("Incorrect Usage. Use `imdb -h` or `imdb --help` for correct usage\n")
os.Exit(0)
}
func main() {
var movieName string
flag.StringVar(&movieName, "m", "moviename", "Name of Movie")
var movieYear string
flag.StringVar(&movieYear, "y", "0000", "Releasing Year")
flag.Parse()
var req string
regex, _ := regexp.Compile(" ")
regex = regexp.MustCompile(" ")
movieName = regex.ReplaceAllString(movieName, "+")
if (movieName == "moviename") {
incorrectUsage()
} else if movieYear == "0000" {
req = "http://www.omdbapi.com/?t=" + movieName + "&type=movie&plot=full&r=json"
} else {
req = "http://www.omdbapi.com/?t=" + movieName + "&y=" + movieYear + "&type=movie&plot=full&r=json"
}
r, _ := http.Get(req)
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
dec.Decode(&Movie)
fmt.Printf("\n")
if Movie.Response == "True" {
fmt.Printf("%s (%s)\n", Movie.Title, Movie.Year)
fmt.Printf("%s | %s | %s | %s (%s)\n", Movie.Rated, Movie.Runtime, Movie.Genre, Movie.Released, Movie.Country)
fmt.Printf("Ratings: %s/10 from %s votes. \t Metascore: %s/100\n", Movie.ImdbRating, Movie.ImdbVotes, Movie.Metascore)
fmt.Printf("\n%s\n", Movie.Plot)
fmt.Printf("\nDirector: %s\n", Movie.Director)
fmt.Printf("Writer: %s\n", Movie.Writer)
fmt.Printf("Stars: %s\n", Movie.Actors)
fmt.Printf("\nImdb Page: http://www.imdb.com/title/%s\n", Movie.ImdbID)
} else {
fmt.Printf("%s\n", Movie.Error)
}
fmt.Printf("\n")
}