Move models to api package

This commit is contained in:
Maksym Pavlenko
2017-08-10 15:23:07 -07:00
parent 402ac4cde7
commit dc7cc4784f
10 changed files with 44 additions and 42 deletions
@@ -1,4 +1,4 @@
package storage
package api
import "time"
+3 -3
View File
@@ -4,7 +4,7 @@ import (
"fmt"
itunes "github.com/mxpv/podcast"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
)
const (
@@ -22,10 +22,10 @@ const (
linkTypeGroup
)
func makeEnclosure(feed *storage.Feed, id string, lengthInBytes int64) (string, itunes.EnclosureType, int64) {
func makeEnclosure(feed *api.Feed, id string, lengthInBytes int64) (string, itunes.EnclosureType, int64) {
ext := "mp4"
contentType := itunes.MP4
if feed.Format == storage.AudioFormat {
if feed.Format == api.AudioFormat {
ext = "mp3"
contentType = itunes.MP3
}
+8 -8
View File
@@ -8,7 +8,7 @@ import (
"strings"
itunes "github.com/mxpv/podcast"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/pkg/errors"
"github.com/silentsokolov/go-vimeo"
"golang.org/x/net/context"
@@ -77,19 +77,19 @@ func (v *VimeoBuilder) parseUrl(link string) (kind linkType, id string, err erro
return
}
func (v *VimeoBuilder) selectImage(p *vimeo.Pictures, q storage.Quality) string {
func (v *VimeoBuilder) selectImage(p *vimeo.Pictures, q api.Quality) string {
if p == nil || len(p.Sizes) < 1 {
return ""
}
if q == storage.LowQuality {
if q == api.LowQuality {
return p.Sizes[0].Link
} else {
return p.Sizes[len(p.Sizes)-1].Link
}
}
func (v *VimeoBuilder) queryChannel(channelId string, feed *storage.Feed) (*itunes.Podcast, error) {
func (v *VimeoBuilder) queryChannel(channelId string, feed *api.Feed) (*itunes.Podcast, error) {
ch, resp, err := v.client.Channels.Get(channelId)
if err != nil {
return nil, errors.Wrapf(err, "failed to query channel with channelId %s", channelId)
@@ -109,7 +109,7 @@ func (v *VimeoBuilder) queryChannel(channelId string, feed *storage.Feed) (*itun
return &podcast, nil
}
func (v *VimeoBuilder) queryGroup(groupId string, feed *storage.Feed) (*itunes.Podcast, error) {
func (v *VimeoBuilder) queryGroup(groupId string, feed *api.Feed) (*itunes.Podcast, error) {
gr, resp, err := v.client.Groups.Get(groupId)
if err != nil {
return nil, errors.Wrapf(err, "failed to query group with id %s", groupId)
@@ -129,7 +129,7 @@ func (v *VimeoBuilder) queryGroup(groupId string, feed *storage.Feed) (*itunes.P
return &podcast, nil
}
func (v *VimeoBuilder) queryUser(userId string, feed *storage.Feed) (*itunes.Podcast, error) {
func (v *VimeoBuilder) queryUser(userId string, feed *api.Feed) (*itunes.Podcast, error) {
user, resp, err := v.client.Users.Get(userId)
if err != nil {
return nil, errors.Wrapf(err, "failed to query user with id %s", userId)
@@ -156,7 +156,7 @@ func (v *VimeoBuilder) getVideoSize(video *vimeo.Video) int64 {
type queryVideosFunc func(id string, opt *vimeo.ListVideoOptions) ([]*vimeo.Video, *vimeo.Response, error)
func (v *VimeoBuilder) queryVideos(queryVideos queryVideosFunc, id string, podcast *itunes.Podcast, feed *storage.Feed) error {
func (v *VimeoBuilder) queryVideos(queryVideos queryVideosFunc, id string, podcast *itunes.Podcast, feed *api.Feed) error {
opt := vimeo.ListVideoOptions{}
opt.Page = 1
opt.PerPage = vimeoDefaultPageSize
@@ -207,7 +207,7 @@ func (v *VimeoBuilder) queryVideos(queryVideos queryVideosFunc, id string, podca
}
}
func (v *VimeoBuilder) Build(feed *storage.Feed) (podcast *itunes.Podcast, err error) {
func (v *VimeoBuilder) Build(feed *api.Feed) (podcast *itunes.Podcast, err error) {
kind, id, err := v.parseUrl(feed.URL)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse link: %s", feed.URL)
+3 -3
View File
@@ -7,13 +7,13 @@ import (
"context"
itunes "github.com/mxpv/podcast"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/stretchr/testify/require"
)
var (
vimeoKey = os.Getenv("VIMEO_TEST_API_KEY")
defaultFeed = &storage.Feed{Quality: storage.HighQuality}
defaultFeed = &api.Feed{Quality: api.HighQuality}
)
func TestParseVimeoGroupLink(t *testing.T) {
@@ -125,7 +125,7 @@ func TestQueryVimeoVideos(t *testing.T) {
feed := &itunes.Podcast{}
err = builder.queryVideos(builder.client.Channels.ListVideo, "staffpicks", feed, &storage.Feed{})
err = builder.queryVideos(builder.client.Channels.ListVideo, "staffpicks", feed, &api.Feed{})
require.NoError(t, err)
require.Equal(t, vimeoDefaultPageSize, len(feed.Items))
+9 -9
View File
@@ -9,7 +9,7 @@ import (
"github.com/BrianHicks/finch/duration"
itunes "github.com/mxpv/podcast"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/pkg/errors"
"google.golang.org/api/youtube/v3"
)
@@ -170,10 +170,10 @@ func (yt *YouTubeBuilder) parseDate(s string) (time.Time, error) {
return date, nil
}
func (yt *YouTubeBuilder) selectThumbnail(snippet *youtube.ThumbnailDetails, quality storage.Quality) string {
func (yt *YouTubeBuilder) selectThumbnail(snippet *youtube.ThumbnailDetails, quality api.Quality) string {
// Use high resolution thumbnails for high quality mode
// https://github.com/mxpv/Podsync/issues/14
if quality == storage.HighQuality {
if quality == api.HighQuality {
if snippet.Maxres != nil {
return snippet.Maxres.Url
}
@@ -190,7 +190,7 @@ func (yt *YouTubeBuilder) selectThumbnail(snippet *youtube.ThumbnailDetails, qua
return snippet.Default.Url
}
func (yt *YouTubeBuilder) queryFeed(kind linkType, id string, feed *storage.Feed) (*itunes.Podcast, string, error) {
func (yt *YouTubeBuilder) queryFeed(kind linkType, id string, feed *api.Feed) (*itunes.Podcast, string, error) {
now := time.Now()
if kind == linkTypeChannel || kind == linkTypeUser {
@@ -255,7 +255,7 @@ func (yt *YouTubeBuilder) queryFeed(kind linkType, id string, feed *storage.Feed
return nil, "", errors.New("unsupported link format")
}
func (yt *YouTubeBuilder) getVideoSize(definition string, duration int64, fmt storage.Format) int64 {
func (yt *YouTubeBuilder) getVideoSize(definition string, duration int64, fmt api.Format) int64 {
// Video size information requires 1 additional call for each video (1 feed = 50 videos = 50 calls),
// which is too expensive, so get approximated size depending on duration and definition params
var size int64 = 0
@@ -268,14 +268,14 @@ func (yt *YouTubeBuilder) getVideoSize(definition string, duration int64, fmt st
// Some podcasts are coming in with exactly double the actual runtime and with the second half just silence.
// https://github.com/mxpv/Podsync/issues/6
if fmt == storage.AudioFormat {
if fmt == api.AudioFormat {
size /= 2
}
return size
}
func (yt *YouTubeBuilder) queryVideoDescriptions(ids []string, feed *storage.Feed, podcast *itunes.Podcast) error {
func (yt *YouTubeBuilder) queryVideoDescriptions(ids []string, feed *api.Feed, podcast *itunes.Podcast) error {
req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(strings.Join(ids, ",")).Do(yt.key)
if err != nil {
return errors.Wrap(err, "failed to query video descriptions")
@@ -329,7 +329,7 @@ func (yt *YouTubeBuilder) queryVideoDescriptions(ids []string, feed *storage.Fee
return nil
}
func (yt *YouTubeBuilder) queryItems(itemId string, feed *storage.Feed, podcast *itunes.Podcast) error {
func (yt *YouTubeBuilder) queryItems(itemId string, feed *api.Feed, podcast *itunes.Podcast) error {
pageToken := ""
count := 0
@@ -361,7 +361,7 @@ func (yt *YouTubeBuilder) queryItems(itemId string, feed *storage.Feed, podcast
}
}
func (yt *YouTubeBuilder) Build(feed *storage.Feed) (*itunes.Podcast, error) {
func (yt *YouTubeBuilder) Build(feed *api.Feed) (*itunes.Podcast, error) {
kind, id, err := yt.parseUrl(feed.URL)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse link: %s", feed.URL)
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"os"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/stretchr/testify/require"
)
@@ -78,7 +78,7 @@ func TestBuildYTFeed(t *testing.T) {
builder, err := NewYouTubeBuilder(ytKey)
require.NoError(t, err)
podcast, err := builder.Build(&storage.Feed{
podcast, err := builder.Build(&api.Feed{
URL: "https://youtube.com/channel/UCupvZG-5ko_eiXAupbDfxWw",
PageSize: maxYoutubeResults,
})
+2 -2
View File
@@ -3,7 +3,7 @@ package id
import (
"hash/fnv"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
hd "github.com/speps/go-hashids"
)
@@ -23,7 +23,7 @@ func hashString(s string) int {
return int(h.Sum32())
}
func (h *hashId) Encode(feed *storage.Feed) (string, error) {
func (h *hashId) Encode(feed *api.Feed) (string, error) {
// Don't create duplicate urls for same playlist/settings
// https://github.com/podsync/issues/issues/6
numbers := []int{
+4 -4
View File
@@ -3,7 +3,7 @@ package id
import (
"testing"
"github.com/mxpv/podsync/web/pkg/storage"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/stretchr/testify/require"
)
@@ -11,12 +11,12 @@ func TestEncode(t *testing.T) {
hid, err := NewIdGenerator()
require.NoError(t, err)
feed := &storage.Feed{
feed := &api.Feed{
UserId: "1",
URL: "https://www.youtube.com/channel/UC2yTVSttx7lxAOAzx1opjoA",
PageSize: 10,
Quality: storage.HighQuality,
Format: storage.AudioFormat,
Quality: api.HighQuality,
Format: api.AudioFormat,
}
hash1, err := hid.Encode(feed)
+4 -3
View File
@@ -8,6 +8,7 @@ import (
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy"
"github.com/go-pg/pg"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/pkg/errors"
)
@@ -19,7 +20,7 @@ type PgStorage struct {
db *pg.DB
}
func (p *PgStorage) CreateFeed(feed *Feed) error {
func (p *PgStorage) CreateFeed(feed *api.Feed) error {
feed.LastAccess = time.Now().UTC()
_, err := p.db.Model(feed).OnConflict("DO NOTHING").Insert()
if err != nil {
@@ -29,10 +30,10 @@ func (p *PgStorage) CreateFeed(feed *Feed) error {
return nil
}
func (p *PgStorage) GetFeed(hashId string) (*Feed, error) {
func (p *PgStorage) GetFeed(hashId string) (*api.Feed, error) {
lastAccess := time.Now().UTC()
feed := &Feed{}
feed := &api.Feed{}
_, err := p.db.Model(feed).
Set("last_access = ?", lastAccess).
Where("hash_id = ?", hashId).
+8 -7
View File
@@ -3,11 +3,12 @@ package storage
import (
"testing"
"github.com/mxpv/podsync/web/pkg/api"
"github.com/stretchr/testify/require"
)
func TestCreate(t *testing.T) {
feed := &Feed{
feed := &api.Feed{
HashId: "xyz",
URL: "http://youtube.com",
}
@@ -19,7 +20,7 @@ func TestCreate(t *testing.T) {
}
func TestCreateDuplicate(t *testing.T) {
feed := &Feed{
feed := &api.Feed{
HashId: "123",
URL: "http://youtube.com",
}
@@ -29,7 +30,7 @@ func TestCreateDuplicate(t *testing.T) {
require.NoError(t, err)
// Ensure 1 record
count, err := client.db.Model(&Feed{}).Count()
count, err := client.db.Model(&api.Feed{}).Count()
require.NoError(t, err)
require.Equal(t, 1, count)
@@ -38,13 +39,13 @@ func TestCreateDuplicate(t *testing.T) {
require.NoError(t, err)
// Check no duplicates inserted
count, err = client.db.Model(&Feed{}).Count()
count, err = client.db.Model(&api.Feed{}).Count()
require.NoError(t, err)
require.Equal(t, 1, count)
}
func TestGetFeed(t *testing.T) {
feed := &Feed{
feed := &api.Feed{
HashId: "xyz",
UserId: "123",
URL: "http://youtube.com",
@@ -59,7 +60,7 @@ func TestGetFeed(t *testing.T) {
}
func TestUpdateLastAccess(t *testing.T) {
feed := &Feed{
feed := &api.Feed{
HashId: "xyz",
UserId: "123",
URL: "http://youtube.com",
@@ -88,7 +89,7 @@ func createClient(t *testing.T) *PgStorage {
pg, err := NewPgStorage(&PgConfig{ConnectionUrl: TestDatabaseConnectionUrl})
require.NoError(t, err)
_, err = pg.db.Model(&Feed{}).Where("1=1").Delete()
_, err = pg.db.Model(&api.Feed{}).Where("1=1").Delete()
require.NoError(t, err)
return pg