Friday, October 1, 2010

Using C++ to process batch links in youtube

Simple code:


 
//personal documentation
#include<iostream>
#include<fstream>

#include<string>
using namespace std;

int main(){


        int number_of_col = 5;

        string frame_number="frame25";
        string str1="&nbsp;<a href=\"http://www.youtube.com/v/";
        string str2="\" target=";

        ifstream fin;
        char *file;
        string name="mytube.txt";

        string first;
        char output[30];
        int i=0;

        int j=0;
        string line1;


        file=new char[name.size()+1];

        strcpy (file, name.c_str());
        fin.open(file);

        delete [] file;
        cout<<"<table border=\"1\" cellspacing=\"1\"      cellpadding=\"3\"  "<<endl;
        cout <<"width=\"100%\" align=\"center\"> <tr>"<<endl;

        if (fin.is_open()){
                string line;
                while ( getline(fin, line) ) {

                        if(i==number_of_col){
                            cout<< "<tr>"<<endl;}

                        if(j==0) line1=line;
                       cout<<"<td>" <<endl;

                        cout<<str1+line+str2+frame_number+">"<<endl;
                        getline(fin, line);

                        cout<<line+"</a>"<<endl;
                        cout<<"</td>" <<endl;

                        i++;
                        j++;
                         if(i==number_of_col){
                                cout<< "</tr> "<<endl;

                                 i=0;}
                  }//end of while
             }
        else{
                cout<<"Problem with opening the file "<<name<<endl;

                exit(1);
        }
        fin.close();
        cout<<"</tr></table>"<<endl;

        cout<<"<iframe height=\"600\" name=\""<<frame_number<<"\""<<endl;
        cout<<"src=\"http://www.youtube.com/v/"<<line1<<"\""<<endl;

        cout<< "width=\"100%\"></iframe>"<<endl;
        return 0;
}
Advanced code:


//usage: a.out inputfile outputfile part_start part_end
#include<iostream>
#include<fstream>

#include<string>
using namespace std;

int main(int argc, char* argv[]){


        int number_of_col = 5;
        int number_of_url =33;

        int url_start =0;
        int url_end = 34;

        string frame_number="frame26";
        string str1="<a href=\"http://www.youtube.com/v/";
        string str2="\" target=";

        ifstream fin;
        ofstream output;
        char *file,*ofile;

        string name="perlnew.txt";
        string first;
        int i=0;

        int j=0;
        string line1, line_nouse;
         if(argc>1) name=argv[1];

         if(argc>2) {output.open(argv[2]);}
         if(argc>4) {

                    url_start = atoi(argv[3]);
                    url_end   = atoi(argv[4]);
                    }

         else output.open("output.txt");

        file=new char[name.size()+1];

        strcpy (file, name.c_str());
        fin.open(file);

        delete [] file;
        output<<"<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\"  "<<endl;
        output <<"width=\"100%\" align=\"center\"> <tr>"<<endl;

        if (fin.is_open()){
                string line;
                while ( getline(fin, line) ) {

                         getline(fin, line_nouse);
                         getline(fin, line_nouse);

                        if(i==number_of_col){
                        if(j>=url_start-1&&j<=url_end-1) output<< "<tr>"<<endl;
                         }

                        if(j==url_start-1) line1=line;
                        if(j>=url_start-1&&j<=url_end-1) {output<<"<td>" <<endl;

                                     output<<str1+line+str2+frame_number+">"<<endl;}
                        getline(fin, line);

                        if(j>=url_start-1&&j<=url_end-1){ output<<line+"</a>"<<endl;

                                                         output<<"</td>" <<endl;}
                        i++;
                         getline(fin, line_nouse);

                         getline(fin, line_nouse);
                         if(i==number_of_col){

                         if(j>=url_start-1&&j<=url_end-1) output<< "</tr> "<<endl;

                                 i=0;}
                         j++;
                  }//end of while
             }
        else{

                output<<"Problem with opening the file "<<name<<endl;
                exit(1);
        }
        output<<"</tr></table>"<<endl;

        output<<"<iframe height=\"600\" name=\""<<frame_number<<"\""<<endl;
        output<<"src=\"http://www.youtube.com/v/"<<line1<<"\""<<endl;

        output<< "width=\"100%\"></iframe>"<<endl;
       fin.close();
        output.close();

        return 0;
}

[jiansen6@seawolf3 code_test]$ cat  youtube2.cpp
//usage: a.out inputfile outputfile part_start part_end
#include<iostream>
#include<fstream>

#include<string>
using namespace std;

int main(int argc, char* argv[]){


        int number_of_col = 5;
        int number_of_url =33;

        int url_start =0;
        int url_end = 34;

        string frame_number="frame26";
        string str1="<a href=\"http://www.youtube.com/v/";
        string str2="\" target=";

        ifstream fin;
        ofstream output;
        char *file,*ofile;

        string name="perlnew.txt";
        string first;
        int i=0;

        int j=0;
        string line1, line_nouse;
         if(argc>1) name=argv[1];

         if(argc>2) {output.open(argv[2]);}
         if(argc>4) {

                    url_start = atoi(argv[3]);
                    url_end   = atoi(argv[4]);
                    }

         else output.open("output.txt");

        file=new char[name.size()+1];

        strcpy (file, name.c_str());
        fin.open(file);

        delete [] file;
        output<<"<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\"  "<<endl;
        output <<"width=\"100%\" align=\"center\"> <tr>"<<endl;

        if (fin.is_open()){
                string line;
                while ( getline(fin, line) ) {

                         getline(fin, line_nouse);
                         getline(fin, line_nouse);

                        if(i==number_of_col){
                        if(j>=url_start-1&&j<=url_end-1) output<< "<tr>"<<endl;
                         }

                        if(j==0) line1=line;
                        if(j>=url_start-1&&j<=url_end-1) {output<<"<td>" <<endl;

                                     output<<str1+line+str2+frame_number+">"<<endl;}
                        getline(fin, line);

                        if(j>=url_start-1&&j<=url_end-1){ output<<line+"</a>"<<endl;

                                                         output<<"</td>" <<endl;}
                        i++;
                         getline(fin, line_nouse);

                         getline(fin, line_nouse);
                         if(i==number_of_col){

                         if(j>=url_start-1&&j<=url_end-1) output<< "</tr> "<<endl;

                                 i=0;}
                         j++;
                  }//end of while
             }
        else{

                output<<"Problem with opening the file "<<name<<endl;
                exit(1);
        }
        output<<"</tr></table>"<<endl;

        output<<"<iframe height=\"600\" name=\""<<frame_number<<"\""<<endl;
        output<<"src=\"http://www.youtube.com/v/"<<line1<<"\""<<endl;

        output<< "width=\"100%\"></iframe>"<<endl;
       fin.close();
        output.close();

        return 0;
}

No comments:

Post a Comment