Add filename helper utils to video and image types

This commit is contained in:
Alessio 2021-08-03 15:38:19 -07:00
parent 3893dda640
commit 2663387f29
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package persistence_test
import (
"testing"
"offline_twitter/scraper"
)
/**
* Should return the base name (filename) of the remote URL
*/
func TestImageFilenameWhenDownloaded(t *testing.T) {
i := scraper.Image{IsDownloaded: false, Filename: "https://pbs.twimg.com/media/E7vG1kxWQAQrWGF.jpg"}
outpath := i.FilenameWhenDownloaded()
expected := "E7vG1kxWQAQrWGF.jpg"
if outpath != expected {
t.Errorf("Expected output path to be %q, but got %q", expected, outpath)
}
}
/**
* Should return an `.mp4`file matching its parent Tweet's ID
*/
func TestVideoFilenameWhenDownloaded(t *testing.T) {
v := scraper.Video{TweetID: scraper.TweetID("23"), IsDownloaded: false, Filename: "https://video.twimg.com/ext_tw_video/1418951950020845568/pu/vid/320x568/IXaQ5rPyf9mbD1aD.mp4?tag=12"}
outpath := v.FilenameWhenDownloaded()
expected := "23.mp4"
if outpath != expected {
t.Errorf("Expected output path to be %q, but got %q", expected, outpath)
}
}

View File

@ -1,5 +1,9 @@
package scraper package scraper
import (
"path"
)
type ImageID int type ImageID int
type Image struct { type Image struct {
@ -8,3 +12,7 @@ type Image struct {
Filename string Filename string
IsDownloaded bool IsDownloaded bool
} }
func (img Image) FilenameWhenDownloaded() string {
return path.Base(img.Filename)
}

View File

@ -1,5 +1,9 @@
package scraper package scraper
import (
"fmt"
)
type VideoID int type VideoID int
type Video struct { type Video struct {
@ -8,3 +12,7 @@ type Video struct {
Filename string Filename string
IsDownloaded bool IsDownloaded bool
} }
func (v Video) FilenameWhenDownloaded() string {
return fmt.Sprintf("%s.mp4", v.TweetID)
}