Remove users.yaml file
This commit is contained in:
parent
1582dfe44e
commit
1d990e8a40
@ -1,23 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,6 @@ type Settings struct {}
|
|||||||
|
|
||||||
type Profile struct {
|
type Profile struct {
|
||||||
ProfileDir string
|
ProfileDir string
|
||||||
UsersList []Follow
|
|
||||||
Settings Settings
|
Settings Settings
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
}
|
}
|
||||||
@ -48,7 +47,6 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
return Profile{}, ErrTargetAlreadyExists{target_dir}
|
return Profile{}, ErrTargetAlreadyExists{target_dir}
|
||||||
}
|
}
|
||||||
|
|
||||||
user_list_file := path.Join(target_dir, "users.yaml")
|
|
||||||
settings_file := path.Join(target_dir, "settings.yaml")
|
settings_file := path.Join(target_dir, "settings.yaml")
|
||||||
sqlite_file := path.Join(target_dir, "twitter.db")
|
sqlite_file := path.Join(target_dir, "twitter.db")
|
||||||
profile_images_dir := path.Join(target_dir, "profile_images")
|
profile_images_dir := path.Join(target_dir, "profile_images")
|
||||||
@ -76,13 +74,6 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
}
|
}
|
||||||
InitializeDatabaseVersion(db)
|
InitializeDatabaseVersion(db)
|
||||||
|
|
||||||
// Create `users.txt`
|
|
||||||
fmt.Printf("Creating............. %s\n", user_list_file)
|
|
||||||
err = os.WriteFile(user_list_file, []byte{}, os.FileMode(0644))
|
|
||||||
if err != nil {
|
|
||||||
return Profile{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create `settings.yaml`
|
// Create `settings.yaml`
|
||||||
fmt.Printf("Creating............. %s\n", settings_file)
|
fmt.Printf("Creating............. %s\n", settings_file)
|
||||||
settings := Settings{}
|
settings := Settings{}
|
||||||
@ -130,7 +121,7 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Profile{target_dir, []Follow{}, settings, db}, nil
|
return Profile{target_dir, settings, db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -144,12 +135,10 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
* - the loaded Profile
|
* - the loaded Profile
|
||||||
*/
|
*/
|
||||||
func LoadProfile(profile_dir string) (Profile, error) {
|
func LoadProfile(profile_dir string) (Profile, error) {
|
||||||
user_list_file := path.Join(profile_dir, "users.yaml")
|
|
||||||
settings_file := path.Join(profile_dir, "settings.yaml")
|
settings_file := path.Join(profile_dir, "settings.yaml")
|
||||||
sqlite_file := path.Join(profile_dir, "twitter.db")
|
sqlite_file := path.Join(profile_dir, "twitter.db")
|
||||||
|
|
||||||
for _, file := range []string{
|
for _, file := range []string{
|
||||||
user_list_file,
|
|
||||||
settings_file,
|
settings_file,
|
||||||
sqlite_file,
|
sqlite_file,
|
||||||
} {
|
} {
|
||||||
@ -158,16 +147,6 @@ func LoadProfile(profile_dir string) (Profile, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
users_data, err := os.ReadFile(user_list_file)
|
|
||||||
if err != nil {
|
|
||||||
return Profile{}, err
|
|
||||||
}
|
|
||||||
users_list := []Follow{}
|
|
||||||
err = yaml.Unmarshal(users_data, &users_list);
|
|
||||||
if err != nil {
|
|
||||||
return Profile{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
settings_data, err := os.ReadFile(settings_file)
|
settings_data, err := os.ReadFile(settings_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
@ -185,7 +164,6 @@ func LoadProfile(profile_dir string) (Profile, error) {
|
|||||||
|
|
||||||
ret := Profile{
|
ret := Profile{
|
||||||
ProfileDir: profile_dir,
|
ProfileDir: profile_dir,
|
||||||
UsersList: users_list,
|
|
||||||
Settings: settings,
|
Settings: settings,
|
||||||
DB: db,
|
DB: db,
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package persistence_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -63,14 +62,11 @@ func TestNewProfile(t *testing.T) {
|
|||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
assert.Equal(profile_path,profile.ProfileDir)
|
assert.Equal(profile_path,profile.ProfileDir)
|
||||||
if len(profile.UsersList) != 0 {
|
|
||||||
t.Errorf("Expected empty users list, got %v instead", profile.UsersList)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check files were created
|
// Check files were created
|
||||||
contents, err := os.ReadDir(profile_path)
|
contents, err := os.ReadDir(profile_path)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(contents, 8)
|
assert.Len(contents, 7)
|
||||||
|
|
||||||
expected_files := []struct {
|
expected_files := []struct {
|
||||||
filename string
|
filename string
|
||||||
@ -81,7 +77,6 @@ func TestNewProfile(t *testing.T) {
|
|||||||
{"profile_images", true},
|
{"profile_images", true},
|
||||||
{"settings.yaml", false},
|
{"settings.yaml", false},
|
||||||
{"twitter.db", false},
|
{"twitter.db", false},
|
||||||
{"users.yaml", false},
|
|
||||||
{"video_thumbnails", true},
|
{"video_thumbnails", true},
|
||||||
{"videos", true},
|
{"videos", true},
|
||||||
}
|
}
|
||||||
@ -113,14 +108,8 @@ func TestLoadProfile(t *testing.T) {
|
|||||||
_, err := persistence.NewProfile(profile_path)
|
_, err := persistence.NewProfile(profile_path)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
// Create some users
|
|
||||||
err = os.WriteFile(path.Join(profile_path, "users.yaml"), []byte("- user: user1\n- user: user2\n"), 0644)
|
|
||||||
require.NoError(err)
|
|
||||||
|
|
||||||
profile, err := persistence.LoadProfile(profile_path)
|
profile, err := persistence.LoadProfile(profile_path)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
assert.Equal(t, profile_path, profile.ProfileDir)
|
assert.Equal(t, profile_path, profile.ProfileDir)
|
||||||
assert.Len(t, profile.UsersList, 2)
|
|
||||||
assert.Equal(t, "user1", string(profile.UsersList[0].Handle))
|
|
||||||
}
|
}
|
||||||
|
@ -266,3 +266,12 @@ func (p Profile) GetAllFollowedUsers() []scraper.UserHandle {
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Profile) IsFollowing(handle scraper.UserHandle) bool {
|
||||||
|
for _, follow := range p.GetAllFollowedUsers() {
|
||||||
|
if follow == handle {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user