diff --git a/cmd/tests.sh b/cmd/tests.sh index c06b246..815117a 100755 --- a/cmd/tests.sh +++ b/cmd/tests.sh @@ -9,67 +9,77 @@ PS4='+(${BASH_SOURCE}:${LINENO}): ' test -e data && rm -r data -./tw create_profile data +PATH=`pwd`:$PATH + +tw create_profile data +cd data + # Fetch a user -./tw fetch_user data Denlesks -test $(sqlite3 data/twitter.db "select handle from users") = "Denlesks" -test $(sqlite3 data/twitter.db "select count(*) from users") = "1" -./tw fetch_user data Denlesks -test $(sqlite3 data/twitter.db "select count(*) from users") = "1" +tw fetch_user Denlesks +test "$(sqlite3 twitter.db "select handle from users")" = "Denlesks" +test $(sqlite3 twitter.db "select count(*) from users") = "1" +tw fetch_user Denlesks +test $(sqlite3 twitter.db "select count(*) from users") = "1" # Fetch a tweet with images -./tw fetch_tweet_only data https://twitter.com/Denlesks/status/1261483383483293700 -test $(sqlite3 data/twitter.db "select count(*) from tweets") = "1" -test "$(sqlite3 data/twitter.db "select text from tweets")" = "These are public health officials who are making decisions about your lifestyle because they know more about health, fitness and well-being than you do" -test $(sqlite3 data/twitter.db "select count(*) from images") = "4" +tw fetch_tweet_only https://twitter.com/Denlesks/status/1261483383483293700 +test $(sqlite3 twitter.db "select count(*) from tweets") = "1" +test "$(sqlite3 twitter.db "select text from tweets")" = "These are public health officials who are making decisions about your lifestyle because they know more about health, fitness and well-being than you do" +test $(sqlite3 twitter.db "select count(*) from images") = "4" # Download its images -test $(sqlite3 data/twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 0") = "4" -test $(sqlite3 data/twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 1") = "0" -test $(sqlite3 data/twitter.db "select is_content_downloaded from tweets where id = 1261483383483293700") = "0" -test $(find data/images | wc -l) = "1" -./tw download_tweet_content data 1261483383483293700 -test $(sqlite3 data/twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 0") = "0" -test $(sqlite3 data/twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 1") = "4" -test $(sqlite3 data/twitter.db "select is_content_downloaded from tweets where id = 1261483383483293700") = "1" -test $(find data/images | wc -l) = "5" +test $(sqlite3 twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 0") = "4" +test $(sqlite3 twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 1") = "0" +test $(sqlite3 twitter.db "select is_content_downloaded from tweets where id = 1261483383483293700") = "0" +test $(find images | wc -l) = "1" +tw download_tweet_content 1261483383483293700 +test $(sqlite3 twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 0") = "0" +test $(sqlite3 twitter.db "select count(*) from images where tweet_id = 1261483383483293700 and is_downloaded = 1") = "4" +test $(sqlite3 twitter.db "select is_content_downloaded from tweets where id = 1261483383483293700") = "1" +test $(find images | wc -l) = "5" # Try to double-download it -./tw fetch_tweet_only data https://twitter.com/Denlesks/status/1261483383483293700 -test $(sqlite3 data/twitter.db "select count(*) from tweets") = "1" -test $(sqlite3 data/twitter.db "select count(*) from images") = "4" +tw fetch_tweet_only https://twitter.com/Denlesks/status/1261483383483293700 +test $(sqlite3 twitter.db "select count(*) from tweets") = "1" +test $(sqlite3 twitter.db "select count(*) from images") = "4" # Fetch a tweet with a video -./tw fetch_user data DiamondChariots -test $(sqlite3 data/twitter.db "select handle from users" | wc -l) = "2" -./tw fetch_tweet_only data https://twitter.com/DiamondChariots/status/1418971605674467340 -test $(sqlite3 data/twitter.db "select count(*) from tweets") = "2" -test $(sqlite3 data/twitter.db "select count(*) from videos") = "1" +tw fetch_user DiamondChariots +test $(sqlite3 twitter.db "select handle from users" | wc -l) = "2" +tw fetch_tweet_only https://twitter.com/DiamondChariots/status/1418971605674467340 +test $(sqlite3 twitter.db "select count(*) from tweets") = "2" +test $(sqlite3 twitter.db "select count(*) from videos") = "1" # Download the video -test $(sqlite3 data/twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 0") = "1" -test $(sqlite3 data/twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 1") = "0" -test $(sqlite3 data/twitter.db "select is_content_downloaded from tweets where id = 1418971605674467340") = "0" -test $(find data/videos| wc -l) = "1" -./tw download_tweet_content data 1418971605674467340 -test $(sqlite3 data/twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 0") = "0" -test $(sqlite3 data/twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 1") = "1" -test $(sqlite3 data/twitter.db "select is_content_downloaded from tweets where id = 1418971605674467340") = "1" -test $(find data/videos | wc -l) = "2" +test $(sqlite3 twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 0") = "1" +test $(sqlite3 twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 1") = "0" +test $(sqlite3 twitter.db "select is_content_downloaded from tweets where id = 1418971605674467340") = "0" +test $(find videos| wc -l) = "1" +tw download_tweet_content 1418971605674467340 +test $(sqlite3 twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 0") = "0" +test $(sqlite3 twitter.db "select count(*) from videos where tweet_id = 1418971605674467340 and is_downloaded = 1") = "1" +test $(sqlite3 twitter.db "select is_content_downloaded from tweets where id = 1418971605674467340") = "1" +test $(find videos | wc -l) = "2" # Try to double-download it -./tw fetch_tweet_only data https://twitter.com/DiamondChariots/status/1418971605674467340 -test $(sqlite3 data/twitter.db "select count(*) from tweets") = "2" -test $(sqlite3 data/twitter.db "select count(*) from videos") = "1" +tw fetch_tweet_only https://twitter.com/DiamondChariots/status/1418971605674467340 +test $(sqlite3 twitter.db "select count(*) from tweets") = "2" +test $(sqlite3 twitter.db "select count(*) from videos") = "1" # Download a user's profile image and banner image -test $(sqlite3 data/twitter.db "select is_content_downloaded from users where handle = 'DiamondChariots'") = "0" -./tw download_user_content data DiamondChariots -test $(sqlite3 data/twitter.db "select is_content_downloaded from users where handle = 'DiamondChariots'") = "1" -test -f data/profile_images/DiamondChariots_profile_rE4OTedS.jpg -test -f data/profile_images/DiamondChariots_banner_1615811094.jpg +test $(sqlite3 twitter.db "select is_content_downloaded from users where handle = 'DiamondChariots'") = "0" +tw download_user_content DiamondChariots +test $(sqlite3 twitter.db "select is_content_downloaded from users where handle = 'DiamondChariots'") = "1" +test -f profile_images/DiamondChariots_profile_rE4OTedS.jpg +test -f profile_images/DiamondChariots_banner_1615811094.jpg + + +# Test that the `--profile` flag works +cd .. +tw --profile data fetch_user michaelmalice +test $(sqlite3 data/twitter.db "select count(*) from users where handle = 'michaelmalice'") = "1" echo -e "\033[32mAll tests passed. Finished successfully.\033[0m" diff --git a/cmd/twitter/helpers.go b/cmd/twitter/helpers.go index e63d5d2..a9034c4 100644 --- a/cmd/twitter/helpers.go +++ b/cmd/twitter/helpers.go @@ -13,18 +13,18 @@ import ( /** * Help message to print if command syntax is incorrect */ -const help_message = `Usage: twitter [TARGET] +const help_message = `Usage: twitter [--profile ] : - - create_profile (no target needed) + - create_profile ( is the directory to create). + must not exist. will be ignored if provided. - - fetch_user (TARGET is the user handle) - - fetch_tweet (TARGET is the full URL of the tweet) - - fetch_tweet_and_replies (TARGET is the full URL of the tweet) + - fetch_user ( is the user handle) + - fetch_tweet_only ( is the full URL of the tweet) + - download_tweet_content ( is the ID of the tweet whomst contents to download / back up) + - download_user_content ( is the user handle of the user whomst banner image and profile to download / back up) -: the path to the directory containing the data directories, database files, and settings files. - -TARGET is optional depending on +: the path to the directory containing the data directories, database files, and settings files. By default, refers to the current directory. Ignored if is "create_profile". ` diff --git a/cmd/twitter/main.go b/cmd/twitter/main.go index 5e68eb5..92a3386 100644 --- a/cmd/twitter/main.go +++ b/cmd/twitter/main.go @@ -4,6 +4,7 @@ import ( "os" "strconv" "fmt" + "flag" "offline_twitter/scraper" "offline_twitter/persistence" ) @@ -14,8 +15,6 @@ import ( var profile persistence.Profile -// TODO: use the current directory by default, add flag to set data-dir - /** * Main method */ @@ -24,22 +23,26 @@ func main() { die("", true, 1) } - operation := os.Args[1] - profile_dir := os.Args[2] + profile_dir := flag.String("profile", ".", "TODO USAGE") + flag.Parse() + args := flag.Args() - if operation == "create_profile" { - create_profile(profile_dir) - return - } - - if len(os.Args) < 4 { + if len(args) < 2 { die("", true, 1) } - target := os.Args[3] + operation := args[0] + target := args[1] + + if operation == "create_profile" { + create_profile(target) + return + } + + var err error - profile, err = persistence.LoadProfile(profile_dir) + profile, err = persistence.LoadProfile(*profile_dir) if err != nil { die("Could not load profile: " + err.Error(), true, 2) }