youtube.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //==============================================================================
  2. // Youtube Streamer Include by Usrb1n !
  3. // http://forum.sa-mp.com/member.php?u=122322
  4. //==============================================================================
  5. #include <a_http>
  6. forward U2BStream(playerid, response_code, data[]);
  7. forward U2BStream2(playerid, response_code, data[]);
  8. new YoutubeID[32];
  9. stock YoutubeStream(playerid, vlink[])
  10. {
  11. printf("youtube.inc: YoutubeStream(%d, %s) called.", playerid, vlink);
  12. new tempURL[128];
  13. new index = strfind(vlink, "watch?v=", true, -1);
  14. printf("youtube.inc: index of \"watch?v=\" is %d.", index);
  15. if(index != -1) { //if found the string "watch?v=" in the url (may return 0 on linux)
  16. strmid(tempURL, vlink, index, strlen(vlink), 255); // http://www.youtube.com/watch?v=tGc531yhEJU - extract chars from watch?v= to end of URL
  17. format(tempURL, sizeof(tempURL), "http://www.youtube.com/%s", tempURL);
  18. printf("youtube.inc: tempURL is now \"%s\"", tempURL);
  19. }
  20. new videoid[128], YoutubeString[128];
  21. strmid(videoid,tempURL,31,44,strlen(tempURL));
  22. printf("youtube.inc: videoid is %s", videoid);
  23. format(YoutubeString,sizeof(YoutubeString),"www.youtube-mp3.org/api/itemInfo/?video_id=%s",videoid);
  24. printf("youtube.inc: going to do an HTTP request to URL \"%s\", waiting for response", YoutubeString);
  25. strmid(YoutubeID, videoid, 0, 12);
  26. HTTP(playerid,HTTP_GET,YoutubeString,videoid,"U2BStream");
  27. HTTP(playerid,HTTP_GET,"ifconfig.me","","U2BStream2");
  28. }
  29. public U2BStream(playerid, response_code, data[])
  30. {
  31. if(response_code == 200)
  32. {
  33. printf("youtube.inc: HTTP responded with code 200, proceeding...");
  34. printf("youtube.inc: data: %s", data);
  35. new result[33], u2bstr[33]; new streamedurl[128];
  36. new crypted = strfind(data, "\"h\"", true);
  37. printf("youtube.inc: crypted is now %d", crypted);
  38. strmid(result,data,crypted+7,crypted+39,strlen(data));
  39. printf("youtube.inc: result is now \"%s\"", result);
  40. format(u2bstr,sizeof(u2bstr), "%s", result);
  41. format(streamedurl, sizeof(streamedurl), "http://www.youtube-mp3.org/get?video_id=%s&h=%s",YoutubeID, u2bstr);
  42. printf("youtube.inc: attempting to play streamedurl which is \"%s\"", streamedurl);
  43. PlayAudioStreamForPlayer(playerid, streamedurl);
  44. }
  45. else
  46. {
  47. SendClientMessage(playerid, 0xAFAFAFAA, " Error attempting to convert video to mp3, to fix enter your link into youtube-mp3.org and try again !");
  48. printf("youtube.inc: failed and returned code %d", response_code);
  49. }
  50. return 1;
  51. }
  52. public U2BStream2(playerid, response_code, data[])
  53. {
  54. if(response_code == 200)
  55. {
  56. new File:file;
  57. file = fopen("output.log", io_write);
  58. if(file)
  59. {
  60. fwrite(file, data);
  61. fclose(file);
  62. }
  63. }
  64. else
  65. {
  66. SendClientMessage(playerid, 0xAFAFAFAA, " Error attempting to convert video to mp3, to fix enter your link into youtube-mp3.org and try again !");
  67. printf("youtube.inc: failed and returned code %d", response_code);
  68. }
  69. return 1;
  70. }
  71. stock YoutubeStopStream(playerid)
  72. {
  73. PlayAudioStreamForPlayer(playerid, "Youtube streaming was stopped.");
  74. }