2021-07-28 15:56:47 -07:00
package persistence
import (
"offline_twitter/scraper"
)
2021-08-01 15:52:04 -07:00
/ * *
2021-08-04 01:23:55 -07:00
* Save an Image
2021-08-01 15:52:04 -07:00
*
* args :
* - img : the Image to save
* /
2021-08-04 01:23:55 -07:00
func ( p Profile ) SaveImage ( img scraper . Image ) error {
_ , err := p . DB . Exec ( `
2021-08-05 14:22:16 -07:00
insert into images ( id , tweet_id , remote_url , local_filename , is_downloaded )
values ( ? , ? , ? , ? , ? )
2021-08-04 01:23:55 -07:00
on conflict do update
2021-10-09 18:58:52 -07:00
set is_downloaded = ( is_downloaded or ? )
2021-08-04 01:23:55 -07:00
` ,
2021-08-05 14:22:16 -07:00
img . ID , img . TweetID , img . RemoteURL , img . LocalFilename , img . IsDownloaded ,
2021-08-04 01:23:55 -07:00
img . IsDownloaded ,
)
return err
2021-08-01 15:52:04 -07:00
}
/ * *
2021-08-04 23:41:58 -07:00
* Save a Video
2021-08-01 15:52:04 -07:00
*
* args :
* - img : the Video to save
* /
2021-08-04 23:41:58 -07:00
func ( p Profile ) SaveVideo ( vid scraper . Video ) error {
_ , err := p . DB . Exec ( `
2021-10-04 21:06:53 -07:00
insert into videos ( id , tweet_id , remote_url , local_filename , is_downloaded , is_gif )
values ( ? , ? , ? , ? , ? , ? )
2021-08-04 23:41:58 -07:00
on conflict do update
2021-10-09 18:58:52 -07:00
set is_downloaded = ( is_downloaded or ? )
2021-08-04 23:41:58 -07:00
` ,
2021-10-04 21:06:53 -07:00
vid . ID , vid . TweetID , vid . RemoteURL , vid . LocalFilename , vid . IsDownloaded , vid . IsGif ,
2021-08-04 23:41:58 -07:00
vid . IsDownloaded ,
)
return err
2021-08-01 15:52:04 -07:00
}
2021-09-17 18:04:12 -07:00
/ * *
* Save an Url
* /
func ( p Profile ) SaveUrl ( url scraper . Url ) error {
_ , err := p . DB . Exec ( `
2021-09-17 20:50:28 -07:00
insert into urls ( tweet_id , domain , text , title , description , creator_id , site_id , thumbnail_remote_url , thumbnail_local_path , has_card , has_thumbnail , is_content_downloaded )
values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )
2021-09-17 18:04:12 -07:00
on conflict do update
2021-10-09 18:58:52 -07:00
set is_content_downloaded = ( is_content_downloaded or ? )
2021-09-17 18:04:12 -07:00
` ,
2021-09-17 20:50:28 -07:00
url . TweetID , url . Domain , url . Text , url . Title , url . Description , url . CreatorID , url . SiteID , url . ThumbnailRemoteUrl , url . ThumbnailLocalPath , url . HasCard , url . HasThumbnail , url . IsContentDownloaded ,
2021-09-17 18:04:12 -07:00
url . IsContentDownloaded ,
)
return err
}
2021-07-28 15:56:47 -07:00
/ * *
* Get the list of images for a tweet
* /
func ( p Profile ) GetImagesForTweet ( t scraper . Tweet ) ( imgs [ ] scraper . Image , err error ) {
2021-08-05 14:22:16 -07:00
stmt , err := p . DB . Prepare ( "select id, remote_url, local_filename, is_downloaded from images where tweet_id=?" )
2021-07-28 15:56:47 -07:00
if err != nil {
return
}
defer stmt . Close ( )
rows , err := stmt . Query ( t . ID )
if err != nil {
return
}
var img scraper . Image
for rows . Next ( ) {
2021-08-05 14:22:16 -07:00
err = rows . Scan ( & img . ID , & img . RemoteURL , & img . LocalFilename , & img . IsDownloaded )
2021-07-28 15:56:47 -07:00
if err != nil {
return
}
img . TweetID = t . ID
imgs = append ( imgs , img )
}
return
}
/ * *
* Get the list of videos for a tweet
* /
func ( p Profile ) GetVideosForTweet ( t scraper . Tweet ) ( vids [ ] scraper . Video , err error ) {
2021-10-04 21:06:53 -07:00
stmt , err := p . DB . Prepare ( "select id, remote_url, local_filename, is_downloaded, is_gif from videos where tweet_id=?" )
2021-07-28 15:56:47 -07:00
if err != nil {
return
}
defer stmt . Close ( )
rows , err := stmt . Query ( t . ID )
if err != nil {
return
}
var vid scraper . Video
for rows . Next ( ) {
2021-10-04 21:06:53 -07:00
err = rows . Scan ( & vid . ID , & vid . RemoteURL , & vid . LocalFilename , & vid . IsDownloaded , & vid . IsGif )
2021-07-28 15:56:47 -07:00
if err != nil {
return
}
vid . TweetID = t . ID
vids = append ( vids , vid )
}
return
}
2021-09-17 18:04:12 -07:00
/ * *
* Get the list of Urls for a Tweet
* /
func ( p Profile ) GetUrlsForTweet ( t scraper . Tweet ) ( urls [ ] scraper . Url , err error ) {
2021-09-17 20:50:28 -07:00
stmt , err := p . DB . Prepare ( "select domain, text, title, description, creator_id, site_id, thumbnail_remote_url, thumbnail_local_path, has_card, has_thumbnail, is_content_downloaded from urls where tweet_id=? order by rowid" )
2021-09-17 18:04:12 -07:00
if err != nil {
return
}
defer stmt . Close ( )
rows , err := stmt . Query ( t . ID )
if err != nil {
return
}
var url scraper . Url
for rows . Next ( ) {
2021-09-17 20:50:28 -07:00
err = rows . Scan ( & url . Domain , & url . Text , & url . Title , & url . Description , & url . CreatorID , & url . SiteID , & url . ThumbnailRemoteUrl , & url . ThumbnailLocalPath , & url . HasCard , & url . HasThumbnail , & url . IsContentDownloaded )
2021-09-17 18:04:12 -07:00
if err != nil {
return
}
url . TweetID = t . ID
urls = append ( urls , url )
}
return
}