/Youtube/youtube-search.cpp

https://gitlab.com/captain4ever/PiAUISuite · C++ · 150 lines · 120 code · 24 blank · 6 comment · 25 complexity · 06711cd6a0d111b7de02286529076817 MD5 · raw file

  1. // Youtube Script Created Steven Hickson
  2. #define VERSION "v1.0.1"
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <boost/regex.hpp>
  7. #include "youtube-search.h"
  8. using namespace std;
  9. using namespace boost;
  10. void PrintUsage(void);
  11. int main(int argc, char *argv[]) {
  12. Youtube tube;
  13. if(argc < 2) {
  14. PrintUsage();
  15. return -1;
  16. }
  17. tube.debug = 1;
  18. string search = "";
  19. for(int i = 1; i < argc; ++i) {
  20. if(i != 1)
  21. search += "+";
  22. search += string(argv[i]);
  23. }
  24. if (tube.Init()) {
  25. cout << "Curl failed to initialize. Dying.\n";
  26. return -1;
  27. }
  28. string link;
  29. tube.Search(search, &link, true);
  30. tube.PlayVideo(link, true);
  31. return 0;
  32. }
  33. void PrintUsage(void) {
  34. printf("Just type what you want it to search just like on the website.\nEx: youtube-search Somebody that I used to know\n");
  35. printf("Copyright GPL v3 Steven Hickson 2013\n");
  36. }
  37. Youtube::Youtube() {
  38. hcurl = NULL;
  39. debug = 0;
  40. version = VERSION;
  41. }
  42. Youtube::~Youtube() {
  43. if (hcurl) curl_easy_cleanup(hcurl);
  44. }
  45. //Searches for 1st download link on piratebay.gl
  46. int Youtube::Search(string query, string *link, bool verify) {
  47. if(!init)
  48. Init();
  49. //technically this should never happen now.
  50. if (!hcurl) {
  51. cout << "hcurl is NULL. Did you forget to call Init()?\n";
  52. return -1;
  53. }
  54. string search = "http://www.youtube.com/results?search_query=";
  55. search += query;
  56. curl_easy_setopt(hcurl, CURLOPT_URL, search.c_str());
  57. curlbuf.clear();
  58. cr = curl_easy_perform(hcurl);
  59. if (cr != CURLE_OK) {
  60. cout << "curl() error on getting link: " << errorbuf << endl;
  61. return -3;
  62. }
  63. if (debug & 2) cout << "\nLink curlbuf: [" << curlbuf << "]\n";
  64. regex rexp("\"/watch\\?([a-z0-9A-Z=]+)\" class"); cmatch m;
  65. if (regex_search(curlbuf.c_str(), m, rexp)) {
  66. string t = "http://youtube.com/watch?";
  67. t += string(m[1]);
  68. if (verify)
  69. printf("%s\n", t.c_str());
  70. *link = t;
  71. return 0;
  72. } else {
  73. cout << "Could not find video. Try again.\n";
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. int Youtube::PlayVideo(string video, bool verify) {
  79. if(!init)
  80. Init();
  81. FILE *pf;
  82. //This seems like a lot of space to use to format the string but it keeps it well organized for me
  83. string command = "youtube-safe ";
  84. command += "\"";
  85. command += video;
  86. command += "\"";
  87. if(verify) printf("%s\n",command.c_str());
  88. pf = popen(command.c_str(), "r");
  89. if (!pf) {
  90. cout << "Could not open command pipe. Dying\n";
  91. return -1;
  92. }
  93. //Grab data from process execution
  94. char buffer[DATA_SIZE];
  95. string message = "";
  96. while (!feof(pf)) {
  97. if (fgets(buffer, DATA_SIZE, pf) != NULL) {
  98. message += string(buffer);
  99. }
  100. }
  101. printf("%s\n",message.c_str());
  102. if (pclose(pf) != 0) {
  103. cout << "Could not close command pipe. Dying\n";
  104. return -1;
  105. }
  106. }
  107. int Youtube::Init(void) {
  108. hcurl = curl_easy_init();
  109. if (!hcurl) return -1;
  110. curl_easy_setopt(hcurl, CURLOPT_ERRORBUFFER, errorbuf);
  111. curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, CurlWriter);
  112. curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, &curlbuf);
  113. curl_easy_setopt(hcurl, CURLOPT_HEADER, 0);
  114. curl_easy_setopt(hcurl, CURLOPT_FOLLOWLOCATION, 1);
  115. curl_easy_setopt(hcurl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8) Gecko/20100804 Gentoo Firefox/3.6.8");
  116. init = true;
  117. //curl_easy_setopt(hcurl, CURLOPT_COOKIEJAR, "cookie.txt");
  118. return 0;
  119. }
  120. int Youtube::CurlWriter(char *data, size_t size, size_t nmemb, string *buffer) {
  121. if (buffer != NULL) {
  122. buffer->append(data, size * nmemb);
  123. return size*nmemb;
  124. }
  125. return 0;
  126. }