8 changed files with 302 additions and 10 deletions
@ -1 +1 @@ |
|||
./main.go |
|||
/main.go |
|||
|
@ -0,0 +1,60 @@ |
|||
package tmdb |
|||
|
|||
type RespTVDetails struct { |
|||
Id string `json:"_id"` |
|||
AirDate string `json:"air_date"` |
|||
Episodes []struct { |
|||
} `json:"episodes"` |
|||
Name string `json:"name"` |
|||
Overview string `json:"overview"` |
|||
Id1 int `json:"id"` |
|||
PosterPath string `json:"poster_path"` |
|||
SeasonNumber int `json:"season_number"` |
|||
VoteAverage int `json:"vote_average"` |
|||
} |
|||
|
|||
type RespTVEpisodeDetails struct { |
|||
AirDate string `json:"air_date"` |
|||
EpisodeNumber int `json:"episode_number"` |
|||
EpisodeType string `json:"episode_type"` |
|||
Id int `json:"id"` |
|||
Name string `json:"name"` |
|||
Overview string `json:"overview"` |
|||
ProductionCode string `json:"production_code"` |
|||
Runtime int `json:"runtime"` |
|||
SeasonNumber int `json:"season_number"` |
|||
ShowId int `json:"show_id"` |
|||
StillPath interface{} `json:"still_path"` |
|||
VoteAverage int `json:"vote_average"` |
|||
VoteCount int `json:"vote_count"` |
|||
Crew []struct { |
|||
Department string `json:"department"` |
|||
Job string `json:"job"` |
|||
CreditId string `json:"credit_id"` |
|||
Adult bool `json:"adult"` |
|||
Gender int `json:"gender"` |
|||
Id int `json:"id"` |
|||
KnownForDepartment string `json:"known_for_department"` |
|||
Name string `json:"name"` |
|||
OriginalName string `json:"original_name"` |
|||
Popularity float64 `json:"popularity"` |
|||
ProfilePath *string `json:"profile_path"` |
|||
} `json:"crew"` |
|||
GuestStars []struct { |
|||
Character string `json:"character"` |
|||
CreditId string `json:"credit_id"` |
|||
Order int `json:"order"` |
|||
Adult bool `json:"adult"` |
|||
Gender int `json:"gender"` |
|||
Id int `json:"id"` |
|||
KnownForDepartment string `json:"known_for_department"` |
|||
Name string `json:"name"` |
|||
OriginalName string `json:"original_name"` |
|||
Popularity float64 `json:"popularity"` |
|||
ProfilePath *string `json:"profile_path"` |
|||
} `json:"guest_stars"` |
|||
} |
|||
|
|||
func ListTVEpisodes(id int) (*RespTVDetails, []error) { |
|||
|
|||
} |
@ -0,0 +1,24 @@ |
|||
package tmdb |
|||
|
|||
import ( |
|||
"errors" |
|||
"github.com/parnurzeal/gorequest" |
|||
"net/url" |
|||
) |
|||
|
|||
func sendRequest(target *url.URL) ([]byte, []error) { |
|||
req := gorequest.New().Get(target.String()).Set( |
|||
"accept", |
|||
"application/json", |
|||
).Proxy(proxyURL.String()) |
|||
resp, body, errs := req.End() |
|||
if errs != nil { |
|||
return nil, errs |
|||
} |
|||
if resp.StatusCode != 200 { |
|||
return nil, []error{ |
|||
errors.New(`server error`), |
|||
} |
|||
} |
|||
return []byte(body), nil |
|||
} |
@ -0,0 +1,69 @@ |
|||
package tmdb |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"strconv" |
|||
"test/urlbuilder" |
|||
) |
|||
|
|||
// RespSearchTV 搜索节目响应
|
|||
type RespSearchTV struct { |
|||
Page int `json:"page"` |
|||
Results []RespSearchTVDetails `json:"results"` |
|||
TotalPages int `json:"total_pages"` |
|||
TotalResults int `json:"total_results"` |
|||
} |
|||
|
|||
type RespSearchTVDetails struct { |
|||
Adult bool `json:"adult"` |
|||
BackdropPath *string `json:"backdrop_path"` |
|||
GenreIds []int `json:"genre_ids"` |
|||
Id int `json:"id"` |
|||
OriginCountry []string `json:"origin_country"` |
|||
OriginalLanguage string `json:"original_language"` |
|||
OriginalName string `json:"original_name"` |
|||
Overview string `json:"overview"` |
|||
Popularity float64 `json:"popularity"` |
|||
PosterPath *string `json:"poster_path"` |
|||
FirstAirDate string `json:"first_air_date"` |
|||
Name string `json:"name"` |
|||
VoteAverage float64 `json:"vote_average"` |
|||
VoteCount int `json:"vote_count"` |
|||
} |
|||
|
|||
func SearchTV(name string) (*RespSearchTV, []error) { |
|||
targetURL, _ := urlbuilder.New(baseURL.String()).AddPath( |
|||
strconv.Itoa( |
|||
ApiVersion, |
|||
), |
|||
path_searchTV, |
|||
).AddQuery( |
|||
`query`, |
|||
name, |
|||
).AddQuery( |
|||
`api_key`, |
|||
api_key, |
|||
).AddQuery( |
|||
`include_adult`, |
|||
`true`, |
|||
).AddQuery( |
|||
`language`, |
|||
string(lang), |
|||
).AddQuery( |
|||
`page`, |
|||
`1`, |
|||
).Get() |
|||
content, errs := sendRequest(targetURL) |
|||
if errs != nil { |
|||
return nil, errs |
|||
} |
|||
result := &RespSearchTV{} |
|||
err := json.Unmarshal( |
|||
content, |
|||
result, |
|||
) |
|||
if err != nil { |
|||
return nil, []error{err} |
|||
} |
|||
return result, nil |
|||
} |
@ -0,0 +1,61 @@ |
|||
package tmdb |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"errors" |
|||
"github.com/parnurzeal/gorequest" |
|||
"net/url" |
|||
"strconv" |
|||
"test/urlbuilder" |
|||
) |
|||
|
|||
// SetBase 设置api代理
|
|||
func SetBase(base string) (err error) { |
|||
baseURL, err = url.Parse(base) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// SetProxy 设置proxy代理
|
|||
func SetProxy(proxy string) (err error) { |
|||
proxyURL, err = url.Parse(proxy) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
type RespErrorInfo struct { |
|||
Success bool `json:"success"` |
|||
StatusCode int `json:"status_code"` |
|||
StatusMessage string `json:"status_message"` |
|||
} |
|||
|
|||
func SetKey(key string) []error { |
|||
targetURL, err := urlbuilder.New(baseURL.String()).AddPath(strconv.Itoa(ApiVersion), |
|||
path_auth).AddQuery(`api_key`, key).Get() |
|||
if err != nil { |
|||
return []error{err} |
|||
} |
|||
resp, body, errs := gorequest.New().Get(targetURL.String()).Set("accept", |
|||
"application/json").Proxy(proxyURL.String()).End() |
|||
if errs != nil { |
|||
return errs |
|||
} |
|||
if resp.StatusCode != 200 { |
|||
err = errors.New(`server error`) |
|||
return []error{err} |
|||
} |
|||
authInfo := &RespErrorInfo{} |
|||
err = json.Unmarshal([]byte(body), authInfo) |
|||
if err != nil { |
|||
return []error{err} |
|||
} |
|||
if !authInfo.Success { |
|||
return []error{errors.New(authInfo.StatusMessage)} |
|||
} |
|||
api_key = key |
|||
return nil |
|||
} |
@ -0,0 +1,28 @@ |
|||
package tmdb |
|||
|
|||
import ( |
|||
"net/url" |
|||
) |
|||
|
|||
var ( |
|||
proxyURL = &url.URL{} |
|||
baseURL = &url.URL{ |
|||
Host: `api.themoviedb.org`, |
|||
Scheme: `https`, |
|||
} |
|||
lang = ZH_CN |
|||
api_key = `` |
|||
) |
|||
|
|||
type Lang string |
|||
|
|||
const ( |
|||
ZH_CN Lang = `zh-CN` |
|||
EN_US Lang = `en-US` |
|||
) |
|||
|
|||
const ( |
|||
ApiVersion = 3 |
|||
path_searchTV = `/search/tv` |
|||
path_auth = `/authentication` |
|||
) |
@ -0,0 +1,57 @@ |
|||
package urlbuilder |
|||
|
|||
import ( |
|||
"net/url" |
|||
) |
|||
|
|||
type UrlBuilder struct { |
|||
parts []string |
|||
keys []string |
|||
values []string |
|||
} |
|||
|
|||
func New(base string) *UrlBuilder { |
|||
u := &UrlBuilder{ |
|||
parts: []string{base}, |
|||
} |
|||
return u |
|||
} |
|||
|
|||
func (u *UrlBuilder) AddPath(path ...string) *UrlBuilder { |
|||
u.parts = append( |
|||
u.parts, |
|||
path..., |
|||
) |
|||
return u |
|||
} |
|||
|
|||
func (u *UrlBuilder) AddQuery(key string, value string) *UrlBuilder { |
|||
u.keys = append( |
|||
u.keys, |
|||
key, |
|||
) |
|||
u.values = append( |
|||
u.values, |
|||
value, |
|||
) |
|||
return u |
|||
} |
|||
|
|||
func (u *UrlBuilder) Get() (*url.URL, error) { |
|||
result, err := url.Parse(u.parts[0]) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
if len(u.parts) > 1 { |
|||
result = result.JoinPath(u.parts[1:]...) |
|||
} |
|||
queries := url.Values{} |
|||
for index, key := range u.keys { |
|||
queries.Add( |
|||
key, |
|||
u.values[index], |
|||
) |
|||
} |
|||
result.RawQuery = queries.Encode() |
|||
return result, nil |
|||
} |
Loading…
Reference in new issue