Commit fe9b6ff2 authored by jan.koester's avatar jan.koester
Browse files

better youtube plugin

parent e6fe510d
Loading
Loading
Loading
Loading
+173 −151
Original line number Diff line number Diff line
@@ -110,36 +110,34 @@ namespace blogi {
                <<   "channelname character varying(255) NOT NULL,"
                <<   "apikey character varying(255) NOT NULL,"
                <<   "channelkey character varying(255) NOT NULL"
                << ");";
                << ");"
                << " CREATE TABLE IF NOT EXISTS youtube_videos ("
                <<   "id integer PRIMARY KEY " << Args->database->autoincrement() << ","
                <<   "youtube_channels_pkey bigint REFERENCES youtube_channels(id),"
                <<   "youtube_id varchar(255) NOT NULL"
                << ")";

            Args->database->exec(&sql,res);

            sql="select channelname,apikey,channelkey from youtube_channels";

            int count = Args->database->exec(&sql,res);

            for (int i = 0; i < count; i++) {
                Channel chan;
                chan.Name=res[i][0];
                chan.ApiKey=res[i][1];
                chan.Key=res[i][2];
                channels.push_back(chan);
            }
        }

        bool Controller(libhttppp::HttpRequest * req,libhtmlpp::HtmlElement *page){
            char url[512];
            if(strncmp(req->getRequestURL(),Args->config->buildurl("youtube",url,512),strlen(Args->config->buildurl("youtube",url,512)))!=0){
                return false;
        }

        void SyncYoutube(int channel_id,libhtmlpp::HtmlString &out){
            std::string json;
            libhttppp::HttpResponse res;
            size_t hsize=0,cpos;
            bool chunked=false;
            blogi::SQL        sql;
            blogi::DBResult   dbres;

            int rlen=0;

            sql << "select channelname,apikey,channelkey from youtube_channels where id='" << channel_id <<"'";

            int count = Args->database->exec(&sql,dbres);

            if(count < 1)
                return;

            try{
                std::shared_ptr<netplus::ssl> ysock=std::make_shared<netplus::ssl>("www.googleapis.com",443,1,0,GOOGLECA,1966);
                std::shared_ptr<netplus::ssl> ycsock=std::make_shared<netplus::ssl>();
@@ -147,11 +145,10 @@ namespace blogi {
                ysock->connect(ycsock);
                ycsock->setnonblocking();

                for(auto channel : channels){
                libhttppp::HttpRequest nreq;
                nreq.setRequestType(GETREQUEST);
                    std::string churl="/youtube/v3/search?key="; churl+= channel.ApiKey.c_str();
                    churl+="&channelId="; churl+=channel.Key; churl+="&part=snippet,id&order=date&maxResults=20";
                std::string churl="/youtube/v3/search?key="; churl+= dbres[0][0];
                churl+="&channelId="; churl+=dbres[0][2]; churl+="&part=snippet,id&order=date&maxResults=20";
                nreq.setRequestURL(churl.c_str());
                nreq.setRequestVersion(HTTPVERSION(1.1));
                *nreq.setData("connection") << "keep-alive";
@@ -274,7 +271,6 @@ namespace blogi {
                        }
                    };
                }
                }
            }catch(netplus::NetException &e){
                libhttppp::HTTPException ee;
                ee[libhttppp::HTTPException::Error] << e.what();
@@ -295,8 +291,6 @@ namespace blogi {
                throw e;
            }

            libhtmlpp::HtmlString cHtml;

            try{
                enum json_type type = json_object_get_type(youindex);

@@ -313,35 +307,71 @@ namespace blogi {
                                struct json_object *yid;
                                yid = json_object_object_get(ytems,"videoID");
                                if(yid){
                                    cHtml << "<li><iframe id=\"ytplayer\" type=\"text/html\" width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/"
                                    sql.clear();
                                    sql << "<li><iframe id=\"ytplayer\" type=\"text/html\" width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/"
                                    << json_object_get_string(yid)  << "?autoplay=1\" frameborder=\"0\" allowFullscreen> </iframe></li>";
                                }
                            }
                        }
                    }

                    if(!cHtml.empty()){
                        libhtmlpp::HtmlElement *fel=cHtml.parse();

                        if(fel)
                            youmain.appendChild(fel);
                    }

                    out << "<span>" << fcount << " videos added !" << "</span>";
                }
            }catch(libhtmlpp::HTMLException &e){
                libhttppp::HTTPException ee;
                ee[libhttppp::HTTPException::Error] << e.what();
                throw ee;
            }
        }


        bool Controller(libhttppp::HttpRequest * req,libhtmlpp::HtmlElement *page){
            char url[512];
            libhtmlpp::HtmlString out;
            std::string sid;

            libhtmlpp::HtmlElement *hmain, youdiv("div");

            youdiv.setAttribute("id","youtube-pl");

            youdiv.appendChild(&youmain);
            std::string sid;
            if(strncmp(req->getRequestURL(),Args->config->buildurl("youtube",url,512),strlen(Args->config->buildurl("youtube",url,512)))!=0){
                return false;
            }

            if(strcmp(req->getRequestURL(),Args->config->buildurl("youtube/sync/",url,512))>0){
                SyncYoutube( atoi( req->getRequestURL()+strlen(Args->config->buildurl("youtube/sync/",url,512)) ),out);

                youdiv.appendChild(out.parse());

                if( (hmain=page->getElementbyID("main")) )
                    hmain->appendChild(&youdiv);

                Args->theme->printSite(out,page,req->getRequestURL(),Args->auth->isLoggedIn(req,sid));
                libhttppp::HttpResponse curres;
                curres.setVersion(HTTPVERSION(1.1));
                curres.setContentType("text/html");
                curres.setState(HTTP200);
                curres.send(req, out.c_str(),out.size());
                return true;
            }

            blogi::SQL        sql;
            blogi::DBResult   dbres;

            sql << "SELECT youtube_channels.channelname,youtube_videos.youtube_id "
                << "FROM youtube_channels LEFT JOIN youtube_videos ON youtube_channels.id = youtube_videos.id;";

            int count = Args->database->exec(&sql,dbres);

            if(count < 1){
                libhttppp::HTTPException ee;
                ee[libhttppp::HTTPException::Error] << "no youtube channels or videos found please sync!";
                throw ee;
            }

            for(int i=0; i<count; ++i){
                libhtmlpp::HtmlString youmain;
                youmain << "<li><iframe id=\"ytplayer\" type=\"text/html\" width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/"
                        << dbres[0][1]  << "?autoplay=1\" frameborder=\"0\" allowFullscreen> </iframe></li>";
                youdiv.appendChild(youmain.parse());
            }

            if( (hmain=page->getElementbyID("main")) )
                hmain->appendChild(&youdiv);
@@ -386,14 +416,6 @@ namespace blogi {

            return result;
        }

        struct Channel {
            std::string Name;
            std::string ApiKey;
            std::string Key;
        };

        std::vector<Channel> channels;
    };
};