Change users text file into a yaml file
This commit is contained in:
parent
681cd59e64
commit
4793cf8a4d
@ -17,9 +17,16 @@ 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 []scraper.UserHandle
|
UsersList UsersList
|
||||||
Settings Settings
|
Settings Settings
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
}
|
}
|
||||||
@ -50,7 +57,7 @@ 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.txt")
|
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")
|
||||||
@ -123,7 +130,7 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Profile{target_dir, []scraper.UserHandle{}, settings, db}, nil
|
return Profile{target_dir, UsersList{}, settings, db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -137,7 +144,7 @@ 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.txt")
|
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")
|
||||||
|
|
||||||
@ -155,7 +162,11 @@ func LoadProfile(profile_dir string) (Profile, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
users_list := parse_users_file(users_data)
|
users_list := UsersList{}
|
||||||
|
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 {
|
||||||
@ -166,10 +177,16 @@ func LoadProfile(profile_dir string) (Profile, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", sqlite_file + "?_foreign_keys=on&_journal_mode=WAL")
|
db, err := sql.Open("sqlite3", sqlite_file + "?_foreign_keys=on&_journal_mode=WAL")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Profile{profile_dir, users_list, settings, db}, nil
|
return Profile{
|
||||||
|
ProfileDir: profile_dir,
|
||||||
|
UsersList: users_list,
|
||||||
|
Settings: settings,
|
||||||
|
DB: db,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ func TestNewProfile(t *testing.T) {
|
|||||||
{"profile_images", true},
|
{"profile_images", true},
|
||||||
{"settings.yaml", false},
|
{"settings.yaml", false},
|
||||||
{"twitter.db", false},
|
{"twitter.db", false},
|
||||||
{"users.txt", false},
|
{"users.yaml", false},
|
||||||
{"videos", true},
|
{"videos", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +126,7 @@ func TestLoadProfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create some users
|
// Create some users
|
||||||
err = os.WriteFile(path.Join(profile_path, "users.txt"), []byte("user1\nuser2\n"), 0644)
|
err = os.WriteFile(path.Join(profile_path, "users.yaml"), []byte("- user: user1\n- user: user2\n"), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(err.Error())
|
t.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
@ -143,4 +143,7 @@ func TestLoadProfile(t *testing.T) {
|
|||||||
if len(profile.UsersList) != 2 {
|
if len(profile.UsersList) != 2 {
|
||||||
t.Errorf("Expected 2 users, got %v", profile.UsersList)
|
t.Errorf("Expected 2 users, got %v", profile.UsersList)
|
||||||
}
|
}
|
||||||
|
if profile.UsersList[0].Handle != "user1" {
|
||||||
|
t.Errorf("Expected first user to be %s, got %s", "user1", profile.UsersList[0].Handle)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"offline_twitter/scraper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var NotInDatabase = errors.New("Not in database")
|
var NotInDatabase = errors.New("Not in database")
|
||||||
@ -30,15 +27,3 @@ func file_exists(path string) bool {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func parse_users_file(data []byte) []scraper.UserHandle {
|
|
||||||
users := strings.Split(string(data), "\n")
|
|
||||||
ret := []scraper.UserHandle{}
|
|
||||||
for _, u := range users {
|
|
||||||
if u != "" {
|
|
||||||
ret = append(ret, scraper.UserHandle(u))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user