Create a 'Follow' type

This commit is contained in:
Alessio 2022-01-04 14:40:51 -05:00
parent 80f6787aa0
commit 2210b61b3c
4 changed files with 52 additions and 17 deletions

23
persistence/follows.go Normal file
View File

@ -0,0 +1,23 @@
package persistence
import (
"offline_twitter/scraper"
)
/**
* Create a Type for this to make it easier to expand later
*/
type Follow struct {
Handle scraper.UserHandle `yaml:"user"`
AutoFetch bool `yaml:"auto_fetch_tweets"`
}
func (p Profile) IsFollowing(handle scraper.UserHandle) bool {
for _, follow := range p.UsersList {
if follow.Handle == handle {
return true;
}
}
return false;
}

View File

@ -0,0 +1,23 @@
package persistence_test
import (
"testing"
. "offline_twitter/persistence"
)
func TestIsFollowing(t *testing.T) {
p := Profile{}
p.UsersList = []Follow{
Follow{Handle: "guy1"},
Follow{Handle: "guy2"},
}
if p.IsFollowing("guy3") {
t.Errorf("Should not be following guy3")
}
if !p.IsFollowing("guy2") {
t.Errorf("Should be following guy2")
}
}

View File

@ -46,6 +46,7 @@ func (d DefaultDownloader) Curl(url string, outpath string) error {
return nil return nil
} }
/** /**
* Downloads an Image, and if successful, marks it as downloaded in the DB * Downloads an Image, and if successful, marks it as downloaded in the DB
*/ */
@ -146,11 +147,8 @@ func (p Profile) DownloadUserContentFor(u *scraper.User) error {
* Enable injecting a custom MediaDownloader (i.e., for testing) * Enable injecting a custom MediaDownloader (i.e., for testing)
*/ */
func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader MediaDownloader) error { func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader MediaDownloader) error {
var err error outfile := path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath)
var outfile string err := downloader.Curl(u.ProfileImageUrl, outfile)
outfile = path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath)
err = downloader.Curl(u.ProfileImageUrl, outfile)
if err != nil { if err != nil {
return err return err
} }

View File

@ -8,8 +8,6 @@ import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"offline_twitter/scraper"
) )
//go:embed schema.sql //go:embed schema.sql
@ -17,16 +15,9 @@ var sql_init string
type Settings struct {} type Settings struct {}
/**
* Create a Type for this to make it easier to expand later
*/
type UsersList []struct {
Handle scraper.UserHandle `yaml:"user"`
}
type Profile struct { type Profile struct {
ProfileDir string ProfileDir string
UsersList UsersList UsersList []Follow
Settings Settings Settings Settings
DB *sql.DB DB *sql.DB
} }
@ -139,7 +130,7 @@ func NewProfile(target_dir string) (Profile, error) {
return Profile{}, err return Profile{}, err
} }
return Profile{target_dir, UsersList{}, settings, db}, nil return Profile{target_dir, []Follow{}, settings, db}, nil
} }
@ -171,7 +162,7 @@ func LoadProfile(profile_dir string) (Profile, error) {
if err != nil { if err != nil {
return Profile{}, err return Profile{}, err
} }
users_list := UsersList{} users_list := []Follow{}
err = yaml.Unmarshal(users_data, &users_list); err = yaml.Unmarshal(users_data, &users_list);
if err != nil { if err != nil {
return Profile{}, err return Profile{}, err