Add RemoteURL and LocalFilename to Images, remove Filename

This commit is contained in:
Alessio 2021-08-05 14:22:16 -07:00
parent 9c9100d874
commit d1ba8b48f3
5 changed files with 9 additions and 16 deletions

View File

@ -12,12 +12,12 @@ import (
*/ */
func (p Profile) SaveImage(img scraper.Image) error { func (p Profile) SaveImage(img scraper.Image) error {
_, err := p.DB.Exec(` _, err := p.DB.Exec(`
insert into images (id, tweet_id, filename, is_downloaded) insert into images (id, tweet_id, remote_url, local_filename, is_downloaded)
values (?, ?, ?, ?) values (?, ?, ?, ?, ?)
on conflict do update on conflict do update
set is_downloaded=? set is_downloaded=?
`, `,
img.ID, img.TweetID, img.Filename, img.IsDownloaded, img.ID, img.TweetID, img.RemoteURL, img.LocalFilename, img.IsDownloaded,
img.IsDownloaded, img.IsDownloaded,
) )
return err return err
@ -46,7 +46,7 @@ func (p Profile) SaveVideo(vid scraper.Video) error {
* Get the list of images for a tweet * Get the list of images for a tweet
*/ */
func (p Profile) GetImagesForTweet(t scraper.Tweet) (imgs []scraper.Image, err error) { func (p Profile) GetImagesForTweet(t scraper.Tweet) (imgs []scraper.Image, err error) {
stmt, err := p.DB.Prepare("select id, filename, is_downloaded from images where tweet_id=?") stmt, err := p.DB.Prepare("select id, remote_url, local_filename, is_downloaded from images where tweet_id=?")
if err != nil { if err != nil {
return return
} }
@ -58,7 +58,7 @@ func (p Profile) GetImagesForTweet(t scraper.Tweet) (imgs []scraper.Image, err e
var img scraper.Image var img scraper.Image
for rows.Next() { for rows.Next() {
err = rows.Scan(&img.ID, &img.Filename, &img.IsDownloaded) err = rows.Scan(&img.ID, &img.RemoteURL, &img.LocalFilename, &img.IsDownloaded)
if err != nil { if err != nil {
return return
} }

View File

@ -61,7 +61,8 @@ create table urls (rowid integer primary key,
create table images (rowid integer primary key, create table images (rowid integer primary key,
id integer unique not null check(typeof(id) = 'integer'), id integer unique not null check(typeof(id) = 'integer'),
tweet_id integer not null, tweet_id integer not null,
filename text not null unique, remote_url text not null unique,
local_filename text not null unique,
is_downloaded boolean default 0, is_downloaded boolean default 0,
foreign key(tweet_id) references tweets(id) foreign key(tweet_id) references tweets(id)

View File

@ -66,7 +66,8 @@ func create_image_from_id(id int) scraper.Image {
return scraper.Image{ return scraper.Image{
ID: scraper.ImageID(id), ID: scraper.ImageID(id),
TweetID: -1, TweetID: -1,
Filename: filename, RemoteURL: filename,
LocalFilename: filename,
IsDownloaded: false, IsDownloaded: false,
} }
} }

View File

@ -9,7 +9,6 @@ type ImageID int64
type Image struct { type Image struct {
ID ImageID ID ImageID
TweetID TweetID TweetID TweetID
Filename string
RemoteURL string RemoteURL string
LocalFilename string LocalFilename string
IsDownloaded bool IsDownloaded bool
@ -19,13 +18,8 @@ func ParseAPIMedia(apiMedia APIMedia) Image {
local_filename := path.Base(apiMedia.MediaURLHttps) local_filename := path.Base(apiMedia.MediaURLHttps)
return Image{ return Image{
ID: ImageID(apiMedia.ID), ID: ImageID(apiMedia.ID),
Filename: apiMedia.MediaURLHttps, // TODO filename
RemoteURL: apiMedia.MediaURLHttps, RemoteURL: apiMedia.MediaURLHttps,
LocalFilename: local_filename, LocalFilename: local_filename,
IsDownloaded: false, IsDownloaded: false,
} }
} }
func (img Image) FilenameWhenDownloaded() string {
return path.Base(img.Filename)
}

View File

@ -28,9 +28,6 @@ func TestParseAPIMedia(t *testing.T) {
if image.RemoteURL != expected_remote_url { if image.RemoteURL != expected_remote_url {
t.Errorf("Expected %q, got %q", expected_remote_url, image.RemoteURL) t.Errorf("Expected %q, got %q", expected_remote_url, image.RemoteURL)
} }
if image.Filename != expected_remote_url { // XXX filename: delete this check
t.Errorf("Expected %q, got %q", expected_remote_url, image.Filename)
}
expected_local_filename := "E18sEUrWYAk8dBl.jpg" expected_local_filename := "E18sEUrWYAk8dBl.jpg"
if image.LocalFilename != expected_local_filename { if image.LocalFilename != expected_local_filename {
t.Errorf("Expected %q, got %q", expected_local_filename, image.LocalFilename) t.Errorf("Expected %q, got %q", expected_local_filename, image.LocalFilename)