Added feature to upload all songs from a directory (optional recursive). Need to document the use of it as well as how to even use this CLI

This commit is contained in:
kdeng00
2019-10-23 20:48:42 -04:00
parent 27aecc7a0d
commit 81892cc52e
11 changed files with 162 additions and 29 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ namespace Managers
}
if (std::any_of(supportedFlags.begin(), supportedFlags.end(),
[&](string val)
[&](string& val)
{
return !val.compare(flag);
}))
+26 -4
View File
@@ -157,14 +157,17 @@ namespace Managers
}
void CommitManager::uploadSong()
{
bool uploadSingleSong = true;
bool recursiveDirectory = false;
string songDirectory;
APIParser apiPrs{icaAction};
auto api = apiPrs.retrieveAPI();
auto token = parseToken(api);
Song song{};
Song song;
for (auto arg : icaAction.flags)
for (auto& arg : icaAction.flags)
{
auto flag = arg.flag;
auto value = arg.value;
@@ -173,11 +176,30 @@ namespace Managers
{
song.songPath.assign(arg.value);
}
else if (flag.compare("-sd") == 0)
{
songDirectory = value;
uploadSingleSong = false;
}
else if (flag.compare("-sr") == 0)
{
songDirectory = value;
uploadSingleSong = false;
recursiveDirectory = true;
}
}
Upload upld{api};
cout<<"Uploading song..."<<endl;
upld.uploadSong(token, song);
if (uploadSingleSong)
{
cout<<"Uploading song..."<<endl;
upld.uploadSong(token, song);
}
else
{
cout<<"Uploading songs from " << songDirectory << endl;
upld.uploadSongsFromDirectory(token, songDirectory, recursiveDirectory);
}
}
#pragma Functions
}
+2 -2
View File
@@ -18,11 +18,11 @@ using Models::User;
namespace Managers
{
#pragma
TokenManager::TokenManager(const User user)
TokenManager::TokenManager(const User& user)
{
this->user = user;
}
TokenManager::TokenManager(const User user, API api)
TokenManager::TokenManager(const User& user, API& api)
{
this->user = user;
this->api = api;
+12 -8
View File
@@ -42,15 +42,19 @@ namespace Syncers
cout<<"song path "<<song.songPath<<endl;
string auth{token.tokenType};
auth.append(" " + token.accessToken);
/**
auto r = cpr::Get(cpr::Url(url),
cpr::Header{{"authorization", auth}});
*/
auto r = cpr::Get(cpr::Url(url),
cpr::Header{{"Content-type", "audio/mpeg"}});
int statusCode = r.status_code;
if (statusCode == OK)
{
if (statusCode == OK) {
song.data = r.text;
saveSong(&song);
saveSong(song);
}
@@ -74,14 +78,14 @@ namespace Syncers
return url;
}
void Download::saveSong(Song *song)
void Download::saveSong(Song& song)
{
cout<<"\nSaving song to: "<<song->songPath<<endl;
int bufferLength = song->data.length();
const char *data = song->data.c_str();
cout<<"\nSaving song to: "<<song.songPath<<endl;
int bufferLength = song.data.length();
const char *data = song.data.c_str();
cout<<"buff length "<<bufferLength<<endl;
ofstream saveSong{song->songPath, std::ios::binary};
ofstream saveSong{song.songPath, std::ios::binary};
saveSong.write(data, bufferLength);
saveSong.close();
}
+100 -4
View File
@@ -1,6 +1,6 @@
#include<iostream>
#include<filesystem>
#include<exception>
#include<string>
#include<cpr/cpr.h>
#include<nlohmann/json.hpp>
@@ -30,7 +30,7 @@ namespace Syncers
}
void Upload::uploadSong(const Models::Token token, Song song)
Song Upload::uploadSong(const Models::Token& token, Song& song)
{
try
{
@@ -45,16 +45,77 @@ namespace Syncers
cpr::Header{{"authorization", auth}}
);
cout << r.status_code<< std::endl;
cout << "status code: " << r.status_code<< std::endl;
auto result = nlohmann::json::parse(r.text);
cout<<"Finished"<<endl;
song.id = result["id"].get<int>();
song.title = result["title"].get<std::string>();
song.artist = result["artist"].get<std::string>();
song.album = result["album"].get<std::string>();
song.genre = result["genre"].get<std::string>();
song.year = result["year"].get<int>();
song.duration = result["duration"].get<int>();
song.track = result["track"].get<int>();
return song;
}
catch (exception e)
{
auto msg = e.what();
cout<<msg<<endl;
}
cout<<"Finished"<<endl;
return song;
}
void Upload::uploadSongsFromDirectory(const Models::Token& token,
const std::string& directory,
bool recursive = false)
{
try
{
auto songs = retrieveAllSongsFromDirectory(directory, recursive);
for (auto& song: songs)
{
song = uploadSong(token, song);
}
printSongDetails(songs);
}
catch (exception& e)
{
cout<<e.what()<<endl;
}
}
std::vector<Song> Upload::retrieveAllSongsFromDirectory(const std::string& directory,
bool recursive)
{
std::vector<Song> allSongs;
if (recursive)
{
for (auto p: fs::recursive_directory_iterator(directory))
{
auto song = retrieveSongPath(p);
if (!song.songPath.empty())
allSongs.push_back(song);
}
}
else
{
for (auto p: fs::directory_iterator(directory))
{
auto song = retrieveSongPath(p);
if (!song.songPath.empty())
allSongs.push_back(song);
}
}
return allSongs;
}
string Upload::retrieveUrl()
{
const string url{api.url + "api/" + api.version + "/" +
@@ -62,6 +123,26 @@ namespace Syncers
return url;
}
Song Upload::retrieveSongPath(fs::directory_entry& dirEntry)
{
constexpr auto mp3Ext = ".mp3";
Song song;
if (fs::is_regular_file(dirEntry.path()))
{
const auto ext = dirEntry.path().extension().string();
if (ext.compare(mp3Ext) == 0)
{
cout << "found mp3 file" << endl;
song.songPath = dirEntry.path().string();
}
}
return song;
}
#pragma
void Upload::printSongDetails()
{
@@ -74,6 +155,21 @@ namespace Syncers
cout<<"Year: "<<song.year<<endl;
cout<<"Duration: "<<song.duration<<endl;
}
void Upload::printSongDetails(std::vector<Song>& songs)
{
for (auto& song: songs)
{
cout<<"Song details: "<<endl;
cout<<"Id: "<<song.id<<endl;
cout<<"Title: "<<song.title<<endl;
cout<<"Artist: "<<song.artist<<endl;
cout<<"Album: "<<song.album<<endl;
cout<<"Genre: "<<song.genre<<endl;
cout<<"Year: "<<song.year<<endl;
cout<<"Duration: "<<song.duration<<endl;
cout<<"Path: "<<song.songPath<<endl;
}
}
void Upload::printJsonData(json obj)
{
cout<<endl<<endl<<"JSon data: "<<endl;