func ApiShowsPutOne(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params, user User) (int, []byte) { id, err := strconv.Atoi(parms["id"]) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } show, err := store.GetShowOrRetrieve(id) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } var showPost Show body, _ := ioutil.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &showPost) if err != nil { return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, "Could not decode body"))) } userShow := UserShow{UserID: user.ID, ShowID: show.ID, Favorite: showPost.Favorite, Library: showPost.Library} err = store.UserShowUpsert(&userShow) show.Personalize(user.ID) return http.StatusOK, encoder.Must(enc.Encode(show)) }
func ApiRss(res http.ResponseWriter, w http.ResponseWriter, r *http.Request, enc encoder.Encoder, store Store) (int, []byte) { db := GetDbSession() if r.URL.Query().Get("token") == "" { return http.StatusUnauthorized, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, "Error"))) } token := r.URL.Query().Get("token") user := User{} err := db.SelectOne(&user, "select * from users where token=?", token) if err != nil { return http.StatusUnauthorized, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, "Error"))) } now := time.Now() feed := &feeds.Feed{ Title: "42minutes", Link: &feeds.Link{Href: "http://42minutes.tv"}, Description: "Tv Shows Etc.", Created: now, } feed.Items = []*feeds.Item{} episodesRss := []*EpisodeRss{} db.Select(&episodesRss, "SELECT shows.title AS show_title, episodes.title, episodes.season, episodes.episode, episodes.first_aired, episodes.infohash_hd720p, episodes.infohash_sd480p FROM episodes LEFT JOIN shows ON episodes.show_id = shows.id LEFT JOIN users_shows ON shows.id = users_shows.show_id WHERE users_shows.library = true") // AND users_shows.user_id = "" for _, episodeRss := range episodesRss { magnet := "" if episodeRss.InfohashHd != "" { magnet = torrentlookup.FakeMagnet(episodeRss.InfohashHd) } else if episodeRss.InfohashSd != "" { magnet = torrentlookup.FakeMagnet(episodeRss.InfohashSd) } else { continue } item := feeds.Item{ Title: fmt.Sprintf("%s S%02dE%02d", episodeRss.ShowTitle, episodeRss.Season, episodeRss.Episode), Link: &feeds.Link{Href: magnet}, Created: *episodeRss.FirstAired, } feed.Items = append(feed.Items, &item) } rss, _ := feed.ToRss() w.Header().Set("Content-Type", "application/xml; charset=utf-8") return http.StatusOK, []byte(rss) }
func ApiSeasonsGetAllByShow(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params) (int, []byte) { id, err := strconv.Atoi(parms["showId"]) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } else { seasons, err := store.GetSeasonsOrRetrieveByShowId(id) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } return http.StatusOK, encoder.Must(enc.Encode(seasons)) } }
func ApiUsersRegister(r *http.Request, enc encoder.Encoder, store Store) (int, []byte) { if r.URL.Query().Get("email") != "" && r.URL.Query().Get("password") != "" { db := GetDbSession() user := User{ID: uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), Email: r.URL.Query().Get("email"), Password: sha1Password(r.URL.Query().Get("password")), Token: uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen)} err := db.Insert(&user) if err != nil { log.Println(err) return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } log.Printf("Registering new user with email %s, password: %s , userid:%s, token:%s", user.Email, user.Password, user.ID, user.Token) return http.StatusOK, encoder.Must(enc.Encode(user)) } return http.StatusBadRequest, encoder.Must(enc.Encode("Missing email param")) }
func ApiEpisodesGetOneByShowAndSeasonAndEpisode(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params) (int, []byte) { showId, errShow := strconv.Atoi(parms["showId"]) seasonNumber, errSeason := strconv.Atoi(parms["seasonNumber"]) episodeNumber, errEpisode := strconv.Atoi(parms["episodeNumber"]) if errShow != nil || errSeason != nil || errEpisode != nil { return http.StatusBadRequest, encoder.Must(enc.Encode("Missing show_id or season or episode")) } else { episode, err := store.GetEpisodeOrRetrieveByShowIdAndSeasonAndEpisode(showId, seasonNumber, episodeNumber) if err != nil || episode == nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } episode.Normalize() return http.StatusOK, encoder.Must(enc.Encode(episode)) } }
func ApiShowsGetOne(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params, user User) (int, []byte) { id, err := strconv.Atoi(parms["id"]) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } show, err := store.GetShowOrRetrieve(id) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } show.Personalize(user.ID) return http.StatusOK, encoder.Must(enc.Encode(show)) }
func ApiEpisodesGetAllByShowAndSeason(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params) (int, []byte) { showId, errShow := strconv.Atoi(parms["showId"]) seasonNumber, errSeason := strconv.Atoi(parms["seasonNumber"]) if errShow != nil || errSeason != nil { return http.StatusBadRequest, encoder.Must(enc.Encode("Missing show_id or season")) } else { episodes, err := store.GetEpisodesOrRetrieveByShowIdAndSeason(showId, seasonNumber) if err != nil { return http.StatusBadRequest, encoder.Must(enc.Encode(err)) } for _, episode := range episodes { episode.Normalize() } return http.StatusOK, encoder.Must(enc.Encode(episodes)) } }
func ApiUsersLogin(r *http.Request, enc encoder.Encoder, store Store) (int, []byte) { type UserLoginRequest struct { Email string `json:"email"` Password string `json:"password"` } var ulRequest UserLoginRequest body, _ := ioutil.ReadAll(r.Body) r.Body.Close() err := json.Unmarshal(body, &ulRequest) if err != nil { return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, "Could not decode body"))) } email := ulRequest.Email pass := ulRequest.Password if email != "" && pass != "" { db := GetDbSession() user := User{} passHash := sha1Password(pass) err := db.SelectOne(&user, "select * from users where email=? and password=? ", email, passHash) if err == nil { // TODO Create new token and store it some place // But for now simply return the existing token // user.Token = uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen) // _, err := db.Update(&user) // if err == nil { return http.StatusOK, encoder.Must(enc.Encode(user)) // } } else { return http.StatusBadRequest, encoder.Must(enc.Encode("Wrong email or password")) } } return http.StatusBadRequest, encoder.Must(enc.Encode("Missing email or pass param")) }
func ApiFilesPost(r *http.Request, enc encoder.Encoder, store Store, parms martini.Params) (int, []byte) { db := GetDbSession() token := r.Header.Get("X-API-TOKEN") user := User{} err := db.SelectOne(&user, "select * from users where token=?", token) if err != nil { return http.StatusUnauthorized, encoder.Must(enc.Encode( NewError(ErrCodeNotExist, "Error"))) } var userFiles []UserFile body, _ := ioutil.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &userFiles) if err != nil { return http.StatusNotFound, encoder.Must(enc.Encode( NewError(ErrCodeNotExist, "Could not decode body"))) } for userFile_i, userFile := range userFiles { err = db.SelectOne(&userFiles[userFile_i], "select * from users_files where user_id=? and relative_path=?", userFile.UserID, userFile.RelativePath) if err == sql.ErrNoRows { userFiles[userFile_i].ID = uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen) userFiles[userFile_i].UserID = user.ID db.Insert(&userFiles[userFile_i]) // TODO Error } else if err != nil { // TODO Error } } //Temp call for testing go func(userId string) { ApiProcessFiles(userId) }(user.ID) return http.StatusOK, encoder.Must(enc.Encode(userFiles)) }
func ApiShowsGetAll(r *http.Request, enc encoder.Encoder, store Store, user User) (int, []byte) { db := GetDbSession() var shows []*Show if r.URL.Query().Get("name") == "" { _, err := db.Select(&shows, "SELECT shows.* FROM shows LEFT JOIN users_shows ON shows.id = users_shows.show_id WHERE users_shows.user_id = ? AND (users_shows.favorite = true OR users_shows.library = true) ORDER BY shows.title asc", user.ID) if err != nil { return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, fmt.Sprintf("TODO error")))) } } else { name := r.URL.Query().Get("name") fmt.Println("Looking for show...", name) shows, _ = ShowFindAllByName(name, 5) if len(shows) == 0 { return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrCodeNotExist, fmt.Sprintf("could not find Show with name '%s'", name)))) } } for show_i, _ := range shows { shows[show_i].Personalize(user.ID) } return http.StatusOK, encoder.Must(enc.Encode(shows)) }
func getBooks(enc encoder.Encoder) (int, []byte) { return http.StatusOK, encoder.Must(enc.Encode(model.BooksGetAll())) }