Guest User

[FS] Youtube Reproducer v2 by Riccor

a guest
Sep 6th, 2012
2,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_http>
  3. #include <zcmd> //http://forum.sa-mp.com/showthread.php?t=91354
  4. #include <sscanf2> //http://forum.sa-mp.com/showthread.php?t=120356
  5.  
  6. #undef MAX_PLAYERS
  7. #define MAX_PLAYERS 50
  8.  
  9. #define MAX_RESULTS 16
  10. #define YDIALOG 18450
  11.  
  12. #define YOUR_WEBSITE "yourwebsite.com" //You need to change this.
  13.  
  14. #define COLOR_RED 0xFF0000FF
  15. #define COLOR_YELLOW 0xFFDD00FF
  16. #define COLOR_ORANGE 0xFFA500FF
  17. #define COLOR_BLUE 0x0000FFFF
  18.  
  19. new Results_Links[MAX_PLAYERS][MAX_RESULTS][128];
  20. new Results_Names[MAX_PLAYERS][MAX_RESULTS][128];
  21. new Videos_Found[MAX_PLAYERS];
  22. new Dialog_String[MAX_RESULTS * 128];
  23. new Last_Search[MAX_PLAYERS][128];
  24. new Request_Type[MAX_PLAYERS];
  25.  
  26. CMD:y(playerid, params[]) return cmd_youtube(playerid, params);
  27. CMD:youtube(playerid, params[])
  28. {
  29. if(sscanf(params, "s[128]", params))
  30. {
  31. SendClientMessage(playerid, COLOR_BLUE, "Usage: /youtube < Name of video to search >");
  32. SendClientMessage(playerid, COLOR_ORANGE, "Function: It will search and reproduce the specified youtube song.");
  33. return 1;
  34. }
  35. Request_Type[playerid] = 0;
  36. if(strcmp(params, Last_Search[playerid], true))
  37. {
  38. format(Last_Search[playerid], 128, "%s", params);
  39. new assist[256];
  40. format(assist, sizeof(assist), ""#YOUR_WEBSITE"/curl.php?limit="#MAX_RESULTS"&linkid=http://www.youtube.com/results?search_query=%s", params);
  41. for(new i = 35; i < strlen(assist); i++) if(assist[i] == ' ') assist[i] = '?';
  42. HTTP(playerid, HTTP_GET, assist, "", "MyYoutubeHttpResponse");
  43. return 1;
  44. }
  45. ShowResultsWindow(playerid);
  46. return 1;
  47. }
  48.  
  49. CMD:yall(playerid, params[])
  50. {
  51. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, COLOR_RED, "Error: Only RCON admins can use this command.");
  52. if(sscanf(params, "s[128]", params))
  53. {
  54. SendClientMessage(playerid, COLOR_BLUE, "Usage: /yall < Name of video to search >");
  55. SendClientMessage(playerid, COLOR_ORANGE, "Function: It will search and reproduce the specified youtube song for all players.");
  56. return 1;
  57. }
  58. Request_Type[playerid] = 1;
  59. if(strcmp(params, Last_Search[playerid], true))
  60. {
  61. format(Last_Search[playerid], 128, "%s", params);
  62. new assist[256];
  63. format(assist, sizeof(assist), ""#YOUR_WEBSITE"/curl.php?limit="#MAX_RESULTS"&linkid=http://www.youtube.com/results?search_query=%s", params);
  64. for(new i = 35; i < strlen(assist); i++) if(assist[i] == ' ') assist[i] = '?';
  65. HTTP(playerid, HTTP_GET, assist, "", "MyYoutubeHttpResponse");
  66. return 1;
  67. }
  68. ShowResultsWindow(playerid);
  69. return 1;
  70. }
  71.  
  72. stock ShowResultsWindow(playerid)
  73. {
  74. if(Videos_Found[playerid] == 0) return SendClientMessage(playerid, COLOR_RED, "- No videos were found with the specified name!");
  75. new c = 0;
  76. for(new l = 0; l < Videos_Found[playerid]; l++)
  77. {
  78. if(l == 0) format(Dialog_String, sizeof(Dialog_String), "{FFFFFF}%s", Results_Names[playerid][l]);
  79. else
  80. {
  81. if(c == 0)
  82. {
  83. format(Dialog_String, sizeof(Dialog_String), "%s\n{AFAFAF}%s", Dialog_String, Results_Names[playerid][l]);
  84. c = 1;
  85. }
  86. else
  87. {
  88. format(Dialog_String, sizeof(Dialog_String), "%s\n{FFFFFF}%s", Dialog_String, Results_Names[playerid][l]);
  89. c = 0;
  90. }
  91. }
  92. }
  93. new title[64]; format(title, 64, "- Results for {80FF00}\"%s\":", Last_Search[playerid]);
  94. if(Request_Type[playerid] == 1) ShowPlayerDialog(playerid, YDIALOG + 1, DIALOG_STYLE_LIST, title, Dialog_String, "Play!", "Close");
  95. else ShowPlayerDialog(playerid, YDIALOG, DIALOG_STYLE_LIST, title, Dialog_String, "Play!", "Close");
  96. return 1;
  97. }
  98.  
  99. forward MyYoutubeHttpResponse(index, response_code, data[]);
  100. public MyYoutubeHttpResponse(index, response_code, data[])
  101. {
  102. if(response_code == 200)
  103. {
  104. new assist[128];
  105. new f = strfind(data, ">", true, 0), t = 0, offset = 0;
  106. while(f != -1 && t < MAX_RESULTS)
  107. {
  108. strmid(assist, data, f + 1, f + 12);
  109.  
  110. //by Nightmare[TR] (http://forum.sa-mp.com/showthread.php?t=370450) - You can change this address to your own website host:
  111. format(Results_Links[index][t], 128, "http://element-samp.net/youtube.php?videoid=%s", assist);
  112.  
  113. strmid(assist, data, f + 15, strfind(data, "<", true, f + 15));
  114. format(Results_Names[index][t], 128, "%s", assist);
  115. t++;
  116. offset = f + 16;
  117. f = strfind(data, ">", true, offset);
  118. }
  119. Videos_Found[index] = t;
  120. ShowResultsWindow(index);
  121. }
  122. else
  123. {
  124. format(Dialog_String, sizeof(Dialog_String), "Error: Request failed! Try again later. [Response code: %d]", response_code);
  125. SendClientMessage(index, COLOR_RED, Dialog_String);
  126. }
  127. return 1;
  128. }
  129.  
  130. public OnFilterScriptInit()
  131. {
  132. for(new i = 0; i < MAX_PLAYERS; i++) format(Last_Search[i], 128, " - ");
  133. return 1;
  134. }
  135.  
  136. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  137. {
  138. if(dialogid == YDIALOG && response)
  139. {
  140. PlayAudioStreamForPlayer(playerid, Results_Links[playerid][listitem]);
  141. SendClientMessage(playerid, COLOR_YELLOW, "- Press N to stop the music.");
  142. return 1;
  143. }
  144.  
  145. if(dialogid == YDIALOG + 1) if(response)
  146. {
  147. for(new i = 0; i < MAX_PLAYERS; i++) if(IsPlayerConnected(i)) PlayAudioStreamForPlayer(i, Results_Links[playerid][listitem]);
  148. new pName[MAX_PLAYER_NAME]; GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
  149. format(Dialog_String, sizeof(Dialog_String), "Server: Admin %s(%d) is playing youtube audio for all players!", pName, playerid);
  150. SendClientMessageToAll(COLOR_ORANGE, Dialog_String);
  151. SendClientMessage(playerid, COLOR_YELLOW, "- Press N to stop the music.");
  152. return 1;
  153. }
  154. return 0;
  155. }
  156.  
  157. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  158. {
  159. if(newkeys & KEY_NO) StopAudioStreamForPlayer(playerid);
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment