diff --git a/persistence/media_download_test.go b/persistence/media_download_test.go new file mode 100644 index 0000000..77d2c79 --- /dev/null +++ b/persistence/media_download_test.go @@ -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) + } +} diff --git a/scraper/image.go b/scraper/image.go index 40d910c..3427207 100644 --- a/scraper/image.go +++ b/scraper/image.go @@ -1,5 +1,9 @@ package scraper +import ( + "path" +) + type ImageID int type Image struct { @@ -8,3 +12,7 @@ type Image struct { Filename string IsDownloaded bool } + +func (img Image) FilenameWhenDownloaded() string { + return path.Base(img.Filename) +} diff --git a/scraper/video.go b/scraper/video.go index c437cf5..45ecf5d 100644 --- a/scraper/video.go +++ b/scraper/video.go @@ -1,5 +1,9 @@ package scraper +import ( + "fmt" +) + type VideoID int type Video struct { @@ -8,3 +12,7 @@ type Video struct { Filename string IsDownloaded bool } + +func (v Video) FilenameWhenDownloaded() string { + return fmt.Sprintf("%s.mp4", v.TweetID) +}