Added code

This commit is contained in:
nedko 2024-06-11 15:01:58 +03:00
commit b31bceb280
2 changed files with 98 additions and 0 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
all:
g++ -o youtube_playlist main.cpp
.PHONY: all

94
main.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ifstream file;
string curr_id;
int pos;
vector<string> ids_array;
if(argc < 2)
{
cout << "Usage: " << argv[0] << " [playlist]" << endl;
return -1;
}
file.open(argv[1]);
if(!file.is_open())
{
cout << "Unable to open file" << endl;
return -1;
}
while(!file.eof())
{
getline(file, curr_id);
for (pos = 0; pos < curr_id.length(); ++pos)
{
if (0 != isspace(curr_id[pos]))
{
curr_id.erase(pos, 1);
--pos;
}
}
// Delete everything after comment
pos = curr_id.find("/*");
if (string::npos != pos)
{
curr_id.erase(pos);
}
// Find beginning of parameters
pos = curr_id.find("?");
if (string::npos == pos)
{
continue;
}
curr_id.erase(0, pos + 1);
// Find beginning of video id
pos = curr_id.find("v=");
if (string::npos == pos)
{
continue;
}
curr_id.erase(0, pos + 2);
// Delete everything after the id if exists
pos = curr_id.find("&");
if (string::npos != pos)
{
curr_id.erase(pos);
}
// Put it in array
if (!curr_id.empty())
{
ids_array.push_back(curr_id);
}
}
if(0 != ids_array.size())
{
cout << "http://www.youtube.com/watch_videos?video_ids=";
cout << ids_array[0];
for(vector<string>::iterator it = ids_array.begin() + 1; it != ids_array.end(); ++it)
{
cout << "," << *it;
}
cout << endl;
}
else
{
cout << "No video links found. 'youtu.be' links are not supported" << endl;
}
return 0;
}