Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions r_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ type Request struct {
Conductor []string `json:"conductor"`
RemixedBy []string `json:"remixedBy"`
Producer []string `json:"producer"`
} `json"musicInfo"`
} `json:"musicInfo"`
CatalogueNumber string `json:"catalogueNumber"`
ReleaseType int `json:"releaseType"`
ReleaseName string `json:"releaseName"`
BitrateList []string `json:"bitrateList"`
FormatList []string `json:"formatList"`
MediaList []string `json:"mediaList"`
BitrateList []string `json:"bitrateList"`
FormatList []string `json:"formatList"`
MediaList []string `json:"mediaList"`
LogCue string `json:"logCue"`
IsFilled bool `json:"isFilled"`
FillerID int `json:"fillerID"`
Expand All @@ -68,4 +68,4 @@ type Request struct {
} `json:"comments"`
CommentPage int `json:"commentPage"`
CommentPages int `json:"commentPages"`
}
}
35 changes: 25 additions & 10 deletions r_torrent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package whatapi

type Torrent struct {
Group GroupType `json:"group"`
Group GroupType `json:"group"`
Torrent TorrentType `json:"torrent"`
}

Expand All @@ -19,21 +19,36 @@ type GroupType struct {
Time string `json:"time"`
VanityHouse bool `json:"vanityHouse"`
MusicInfo struct {
Composers []string `json:"composers"`
DJ []string `json:"dj"`
Artists []struct {
Composers []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"composers"`
DJ []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"dj"`
Artists []struct {
ID int `json:"id"`
Name string `json:"name"`
}
With []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"with"`
Conductor []string `json:"conductor"`
RemixedBy []string `json:"remixedBy"`
Producer []string `json:"producer"`
} `json"musicInfo"`
Tags []string `json"tags"`
Conductor []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"conductor"`
RemixedBy []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"remixedBy"`
Producer []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"producer"`
} `json:"musicInfo"`
Tags []string `json:"tags"`
}

type TorrentType struct {
Expand Down Expand Up @@ -62,4 +77,4 @@ type TorrentType struct {
FilePath string `json:"filePath"`
UserID int `json:"userID"`
Username string `json:"username"`
}
}
111 changes: 74 additions & 37 deletions whatapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
)

//NewWhatAPI creates a new client for the What.CD API using the provided URL.
func NewWhatAPI(url string) (*WhatAPI, error) {
func NewWhatAPI(url, agent string) (*WhatAPI, error) {
w := new(WhatAPI)
w.baseURL = url
w.userAgent = agent
cookieJar, err := cookiejar.New(nil)
if err != nil {
return w, err
Expand All @@ -24,61 +26,96 @@ func NewWhatAPI(url string) (*WhatAPI, error) {

//WhatAPI represents a client for the What.CD API.
type WhatAPI struct {
baseURL string
client *http.Client
authkey string
passkey string
loggedIn bool
baseURL string
userAgent string
client *http.Client
authkey string
passkey string
loggedIn bool
}

//GetJSON sends a HTTP GET request to the API and decodes the JSON response into responseObj.
func (w *WhatAPI) GetJSON(requestURL string, responseObj interface{}) error {
if w.loggedIn {
resp, err := w.client.Get(requestURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errRequestFailedReason("Status Code " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(body, responseObj)

}
return errRequestFailedLogin
if !w.loggedIn {
return errRequestFailedLogin
}

req, err := http.NewRequest("GET", requestURL, nil)
req.Header.Set("User-Agent", w.userAgent)
if err != nil {
return err
}
resp, err := w.client.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
return errRequestFailedReason("Status Code " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

var st GenericResponse
if err := json.Unmarshal(body, &st); err != nil {
return err
}

if err := checkResponseStatus(st.Status, st.Error); err != nil {
return err
}
return json.Unmarshal(body, responseObj)
}

type GenericResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}

func (w *WhatAPI) Do(action string, params url.Values, result interface{}) error {
requestURL, err := buildURL(w.baseURL, "ajax.php", action, params)
if err != nil {
return err
}
return w.GetJSON(requestURL, result)
}

//CreateDownloadURL constructs a download URL using the provided torrent id.
func (w *WhatAPI) CreateDownloadURL(id int) (string, error) {
if w.loggedIn {
params := url.Values{}
params.Set("action", "download")
params.Set("id", strconv.Itoa(id))
params.Set("authkey", w.authkey)
params.Set("torrent_pass", w.passkey)
downloadURL, err := buildURL(w.baseURL, "torrents.php", "", params)
if err != nil {
return "", err
}
return downloadURL, nil
}
return "", errRequestFailedLogin
if !w.loggedIn {
return "", errRequestFailedLogin
}

params := url.Values{}
params.Set("action", "download")
params.Set("id", strconv.Itoa(id))
params.Set("authkey", w.authkey)
params.Set("torrent_pass", w.passkey)
downloadURL, err := buildURL(w.baseURL, "torrents.php", "", params)
if err != nil {
return "", err
}
return downloadURL, nil
}

//Login logs in to the API using the provided credentials.
func (w *WhatAPI) Login(username, password string) error {
params := url.Values{}
params.Set("username", username)
params.Set("password", password)
resp, err := w.client.PostForm(w.baseURL+"login.php?", params)

reqBody := strings.NewReader(params.Encode())
req, err := http.NewRequest("POST", w.baseURL+"login.php", reqBody)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", w.userAgent)
resp, err := w.client.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()
if resp.Request.URL.String()[len(w.baseURL):] != "index.php" {
return errLoginFailed
Expand Down