offline-twitter/persistence/user_queries.go

276 lines
8.0 KiB
Go
Raw Normal View History

2021-07-24 12:24:57 -07:00
package persistence
import (
2022-03-06 18:09:43 -08:00
"database/sql"
2022-03-13 16:13:16 -07:00
"errors"
"fmt"
"path"
2022-03-13 16:13:16 -07:00
2022-03-06 18:09:43 -08:00
"offline_twitter/scraper"
2021-07-24 12:24:57 -07:00
)
/**
* Save the given User to the database.
* If the User is already in the database, it will update most of its attributes (follower count, etc)
*
* args:
* - u: the User
*/
func (p Profile) SaveUser(u *scraper.User) error {
2022-03-06 17:07:05 -08:00
if u.IsNeedingFakeID {
err := p.DB.QueryRow("select id from users where lower(handle) = lower(?)", u.Handle).Scan(&u.ID)
2022-03-06 20:31:04 -08:00
if errors.Is(err, sql.ErrNoRows) {
2022-03-06 17:07:05 -08:00
// We need to continue-- create a new fake user
u.ID = p.NextFakeUserID()
} else if err == nil {
2022-11-13 12:04:25 -05:00
// We're done; a user exists with this handle already. No need to fake anything, and we have no new data
// to provide (since the ID is fake).
// ID has already been scanned into the User, for use by the caller.
2022-03-06 17:07:05 -08:00
return nil
} else {
// A real error occurred
2022-03-06 20:31:04 -08:00
panic(fmt.Errorf("Error checking for existence of fake user with handle %q:\n %w", u.Handle, err))
2022-03-06 17:07:05 -08:00
}
}
2021-07-24 12:24:57 -07:00
2022-03-06 17:07:05 -08:00
_, err := p.DB.Exec(`
insert into users (id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private,
is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path,
pinned_tweet_id, is_content_downloaded, is_id_fake)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2021-07-24 12:24:57 -07:00
on conflict do update
2022-05-07 15:23:37 -07:00
set handle=?,
bio=?,
2022-01-07 12:38:56 -05:00
display_name=?,
following_count=?,
followers_count=?,
location=?,
website=?,
is_private=?,
is_verified=?,
is_banned=?,
profile_image_url=?,
profile_image_local_path=?,
banner_image_url=?,
banner_image_local_path=?,
pinned_tweet_id=?,
is_content_downloaded=(is_content_downloaded or ?)
2021-07-24 12:24:57 -07:00
`,
2022-03-06 18:09:43 -08:00
u.ID, u.DisplayName, u.Handle, u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.JoinDate, u.IsPrivate,
u.IsVerified, u.IsBanned, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID,
u.IsContentDownloaded, u.IsIdFake,
2022-05-07 15:23:37 -07:00
u.Handle, u.Bio, u.DisplayName, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.IsPrivate, u.IsVerified, u.IsBanned,
2022-03-06 18:09:43 -08:00
u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded,
)
if err != nil {
2022-03-13 16:13:16 -07:00
return fmt.Errorf("Error executing SaveUser(%s):\n %w", u.Handle, err)
2022-03-06 18:09:43 -08:00
}
2021-07-24 12:24:57 -07:00
2022-03-06 18:09:43 -08:00
return nil
2021-07-24 12:24:57 -07:00
}
/**
* Check if the database has a User with the given user handle.
*
* args:
* - handle: the user handle to search for
*
* returns:
* - true if there is such a User in the database, false otherwise
*/
func (p Profile) UserExists(handle scraper.UserHandle) bool {
2022-03-06 19:17:43 -08:00
db := p.DB
2021-07-24 12:24:57 -07:00
2022-03-06 19:17:43 -08:00
var dummy string
err := db.QueryRow("select 1 from users where lower(handle) = lower(?)", handle).Scan(&dummy)
if err != nil {
2022-03-06 20:31:04 -08:00
if !errors.Is(err, sql.ErrNoRows) {
2022-03-06 19:17:43 -08:00
// A real error
panic(err)
}
return false
}
return true
2021-07-24 12:24:57 -07:00
}
/**
* Retrieve a User from the database, by handle.
*
* args:
* - handle: the user handle to search for
*
* returns:
* - the User, if it exists
*/
func (p Profile) GetUserByHandle(handle scraper.UserHandle) (scraper.User, error) {
2022-03-06 19:17:43 -08:00
db := p.DB
2021-07-24 12:24:57 -07:00
2022-03-06 19:17:43 -08:00
var ret scraper.User
err := db.Get(&ret, `
2022-03-06 15:36:07 -08:00
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id,
is_content_downloaded, is_followed
2021-07-24 12:24:57 -07:00
from users
2021-08-22 18:04:10 -07:00
where lower(handle) = lower(?)
2022-03-06 19:17:43 -08:00
`, handle)
2021-07-24 12:24:57 -07:00
2022-03-06 20:31:04 -08:00
if errors.Is(err, sql.ErrNoRows) {
2022-03-06 19:17:43 -08:00
return ret, ErrNotInDatabase{"User", handle}
}
return ret, nil
2021-07-24 12:24:57 -07:00
}
/**
* Retrieve a User from the database, by user ID.
*
* args:
* - id: the user ID to search for
*
* returns:
* - the User, if it exists
*/
func (p Profile) GetUserByID(id scraper.UserID) (scraper.User, error) {
2022-03-06 19:17:43 -08:00
db := p.DB
2021-07-24 12:24:57 -07:00
2022-03-06 19:17:43 -08:00
var ret scraper.User
err := db.Get(&ret, `
2022-03-06 15:36:07 -08:00
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id,
is_content_downloaded, is_followed
2021-07-24 12:24:57 -07:00
from users
where id = ?
2022-03-06 19:17:43 -08:00
`, id)
2022-03-06 20:31:04 -08:00
if errors.Is(err, sql.ErrNoRows) {
2022-03-06 19:17:43 -08:00
return ret, ErrNotInDatabase{"User", id}
}
2022-03-13 16:13:16 -07:00
if err != nil {
panic(err)
}
return ret, nil
2021-07-24 12:24:57 -07:00
}
/**
* Returns `true` if content download is needed, `false` otherwise
*
2022-01-07 13:40:22 -05:00
* If the user is banned, returns false because downloading will be impossible.
*
* If:
* - the user isn't in the DB at all (first time scraping), OR
* - `is_content_downloaded` is false in the DB, OR
* - the banner / profile image URL has changed from what the DB has
* then it needs to be downloaded.
*
* The `user` object will always have `is_content_downloaded` = false on every scrape. This is
* why the No Worsening Principle is needed.
*/
func (p Profile) CheckUserContentDownloadNeeded(user scraper.User) bool {
2022-03-06 18:09:43 -08:00
row := p.DB.QueryRow(`select is_content_downloaded, profile_image_url, banner_image_url from users where id = ?`, user.ID)
2022-03-06 18:09:43 -08:00
var is_content_downloaded bool
var profile_image_url string
var banner_image_url string
err := row.Scan(&is_content_downloaded, &profile_image_url, &banner_image_url)
if err != nil {
2022-03-06 20:31:04 -08:00
if errors.Is(err, sql.ErrNoRows) {
2022-03-06 18:09:43 -08:00
return true
} else {
panic(err)
}
}
2022-03-06 18:09:43 -08:00
if !is_content_downloaded {
return true
}
banner_path := p.get_banner_image_output_path(user)
if banner_path != "" && !file_exists(banner_path) {
2022-03-06 18:09:43 -08:00
return true
}
profile_path := p.get_profile_image_output_path(user)
2022-05-07 19:02:02 -07:00
return !file_exists(profile_path)
}
2022-02-26 15:58:30 -08:00
/**
* Follow / unfollow a user. Update the given User object's IsFollowed field.
*/
func (p Profile) SetUserFollowed(user *scraper.User, is_followed bool) {
2022-03-06 18:09:43 -08:00
result, err := p.DB.Exec("update users set is_followed = ? where id = ?", is_followed, user.ID)
if err != nil {
2022-03-06 20:31:04 -08:00
panic(fmt.Errorf("Error inserting user with handle %q:\n %w", user.Handle, err))
2022-03-06 18:09:43 -08:00
}
count, err := result.RowsAffected()
if err != nil {
2022-03-06 20:31:04 -08:00
panic(fmt.Errorf("Unknown error retrieving row count:\n %w", err))
2022-03-06 18:09:43 -08:00
}
if count != 1 {
2022-03-06 20:31:04 -08:00
panic(fmt.Errorf("User with handle %q not found", user.Handle))
2022-03-06 18:09:43 -08:00
}
user.IsFollowed = is_followed
2022-02-26 15:58:30 -08:00
}
func (p Profile) NextFakeUserID() scraper.UserID {
2022-03-06 18:09:43 -08:00
_, err := p.DB.Exec("update fake_user_sequence set latest_fake_id = latest_fake_id + 1")
if err != nil {
panic(err)
}
var ret scraper.UserID
err = p.DB.QueryRow("select latest_fake_id from fake_user_sequence").Scan(&ret)
if err != nil {
panic(err)
}
return ret
}
2022-02-28 16:06:58 -08:00
func (p Profile) GetAllFollowedUsers() []scraper.UserHandle {
2022-03-06 18:09:43 -08:00
rows, err := p.DB.Query("select handle from users where is_followed = 1")
if err != nil {
panic(err)
}
2022-02-28 16:06:58 -08:00
2022-03-06 18:09:43 -08:00
ret := []scraper.UserHandle{}
2022-02-28 16:06:58 -08:00
2022-03-06 18:09:43 -08:00
var tmp scraper.UserHandle
2022-02-28 16:06:58 -08:00
2022-03-06 18:09:43 -08:00
for rows.Next() {
err = rows.Scan(&tmp)
if err != nil {
panic(err)
}
ret = append(ret, tmp)
}
2022-02-28 16:06:58 -08:00
2022-03-06 18:09:43 -08:00
return ret
2022-02-28 16:06:58 -08:00
}
2022-03-06 16:11:45 -08:00
func (p Profile) IsFollowing(user scraper.User) bool {
row := p.DB.QueryRow("select is_followed from users where id like ?", user.ID)
var ret bool
err := row.Scan(&ret)
if err != nil {
2022-05-07 14:38:21 -07:00
if errors.Is(err, sql.ErrNoRows) {
return false
}
panic(err) // A real error
2022-03-06 18:09:43 -08:00
}
return ret
2022-03-06 16:11:45 -08:00
}
/**
* Utility function to compute the path to save banner image to
*/
func (p Profile) get_banner_image_output_path(u scraper.User) string {
return path.Join(p.ProfileDir, "profile_images", u.BannerImageLocalPath)
}
/**
* Utility function to compute the path to save profile image to
*/
func (p Profile) get_profile_image_output_path(u scraper.User) string {
if u.ProfileImageUrl == "" {
return path.Join(p.ProfileDir, "profile_images", path.Base(scraper.DEFAULT_PROFILE_IMAGE_URL))
}
return path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath)
}