youtube_stream.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #if defined _youtube_included
  2. #endinput
  3. #endif
  4. #define _youtube_included
  5. /**
  6. * Play an youtube 'audio stream'
  7. *
  8. * @note usually the API fails in newly videos...
  9. *
  10. * @version 1.0.2
  11. * @author Dayvison
  12. * @date 05-10-2016
  13. */
  14. //#define YOUTUBE_USE_TEXTDRAW
  15. #define USE_REGEX
  16. #include <a_http>
  17. #if defined USE_REGEX
  18. #tryinclude <regex> // [Plugin] Regular expression by Fro1sha: http://forum.sa-mp.com/showthread.php?t=247893
  19. #if !defined _regex_included && !defined regex_match_exid
  20. #undef USE_REGEX
  21. #error "Can't find regex plugin"
  22. #endif
  23. #endif
  24. #if defined YOUTUBE_USE_TEXTDRAW
  25. #define USE_TIMER
  26. #endif
  27. #if !defined USE_TIMER
  28. #if defined OnFinishYoutubeUrl
  29. #define USE_TIMER
  30. #endif
  31. #endif
  32. static
  33. #if defined YOUTUBE_USE_TEXTDRAW
  34. PlayerText:youtube_textdraw[MAX_PLAYERS],
  35. #endif
  36. #if defined USE_TIMER
  37. youtube_timer[MAX_PLAYERS],
  38. #endif
  39. youtube_title[MAX_PLAYERS][128 char],
  40. youtube_len[MAX_PLAYERS],
  41. Float:youtube_posX[MAX_PLAYERS],
  42. Float:youtube_posY[MAX_PLAYERS],
  43. Float:youtube_posZ[MAX_PLAYERS],
  44. Float:youtube_distance[MAX_PLAYERS],
  45. youtube_usepos[MAX_PLAYERS]
  46. ;
  47. #if defined OnPlayYoutubeUrl
  48. /**
  49. * Called when youtube url an played.
  50. *
  51. * @param playerid The playerid
  52. * @param title The title of url(if invalid \0)
  53. * @param len The length of url(if invalid 0)
  54. * @param response The response of url
  55. * Response codes:
  56. * 1 Sucess
  57. * -1 Invalid url
  58. * -2 Invalid page
  59. */
  60. forward OnPlayYoutubeUrl(playerid, title[], len, response);
  61. #endif
  62. #if defined OnPlayYoutubeUrl
  63. /**
  64. * Called when youtube url as finished.
  65. *
  66. * @param playerid The playerid
  67. * @param title The title of url
  68. * @param len The length of url
  69. */
  70. forward OnFinishYoutubeUrl(playerid, title[], len);
  71. #endif
  72. #if defined USE_REGEX
  73. /**
  74. * Determines if valid youtube url.
  75. *
  76. * @param url The url
  77. *
  78. * @author Dayvison
  79. * @date 04-10-2016
  80. * @return True if is a valid url, false not.
  81. */
  82. stock IsValidYoutubeUrl(const url[])
  83. {
  84. static RegEx:rHex;
  85. if(!rHex)
  86. {
  87. rHex = regex_build(\"((http(s)?:\/\/)?)(www\.)?((youtube\.com\/)|(youtu.be\/))[\S]+");
  88. }
  89. return regex_match_exid(url, rHex);
  90. }
  91. #endif
  92. /**
  93. * Converts an youtu.be url to youtube.com
  94. *
  95. * @param Url The url to convert
  96. * @param size The size
  97. *
  98. * @author Dayvison
  99. * @date 04-10-2016
  100. * @return True on convert have sucess, false not.
  101. */
  102. stock ConvertYoutubeUrl(Url[], size = sizeof(Url))
  103. {
  104. new find;
  105. if((find = strfind(Url, "youtu.be/")) == -1)
  106. return false;
  107. strdel(Url, 0, find + 4);
  108. format(Url, size, "https://www.youtube.com/watch?v=", Url);
  109. return true;
  110. }
  111. /**
  112. * Play an youtube 'audio stream' for a player.
  113. *
  114. * @param playerid The ID of the player to play the audio for.
  115. * @param url The url to play.
  116. * @param posX The X position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
  117. * @param posY The Y position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
  118. * @param posZ The Z position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
  119. * @param distance The distance over which the audio will be heard. Has no effect unless usepos is set to 1.
  120. * @param usepos Use the positions and distance specified. Default disabled (0).
  121. *
  122. * @author Dayvison
  123. * @date 04-10-2016
  124. * @return True on sucess, or false on failure. if USE_REGEX active is more efficient
  125. */
  126. stock PlayYoutubeStreamForPlayer(playerid, url[], Float:posX = 0.0, Float:posY = 0.0, Float:posZ = 0.0, Float:distance = 50.0, usepos = 0)
  127. {
  128. if(!IsPlayerConnected(playerid))
  129. return false;
  130. #if defined USE_REGEX
  131. if(!IsValidYoutubeUrl(url))
  132. {
  133. if(!ConvertYoutubeUrl(url, 256))
  134. return false;
  135. }
  136. #else
  137. if(strfind(url, "youtu.be/") != -1)
  138. if(!ConvertYoutubeUrl(url, 256))
  139. return false;
  140. #endif
  141. static string[256];
  142. format(string, sizeof(string), "www.youtubeinmp3.com/fetch/?video=%s&autostart=1", url);
  143. PlayAudioStreamForPlayer(playerid, string, posX, posY, posZ, distance, usepos);
  144. youtube_posX[playerid] = posX;
  145. youtube_posY[playerid] = posY;
  146. youtube_posZ[playerid] = posZ;
  147. youtube_distance[playerid] = distance;
  148. youtube_usepos[playerid] = usepos;
  149. youtube_title[playerid]{0} = '\0';
  150. youtube_len[playerid] = 0;
  151. return true;
  152. }
  153. /**
  154. * Stop an youtube 'audio stream' for a player.
  155. *
  156. * @param playerid The ID of the player to play the audio for.
  157. *
  158. * @author Dayvison
  159. * @date 05-10-2016
  160. * @return True on sucess, or false on failure.
  161. */
  162. stock StopYoutubeStreamForPlayer(playerid)
  163. return StopAudioStreamForPlayer(playerid);
  164. /**
  165. * Gets the youtube stream title.
  166. *
  167. * @param playerid The playerid
  168. * @param dest The destination
  169. * @param size The size
  170. *
  171. * @author Dayvison
  172. * @date 05-10-2016
  173. * @return 1 on sucess, 0 on fails
  174. */
  175. stock GetYoutubeStreamTitle(playerid, dest[], size = sizeof(dest))
  176. {
  177. if(!IsPlayerConnected(playerid))
  178. return 0;
  179. strunpack(dest, youtube_title[playerid], size);
  180. return 1;
  181. }
  182. /**
  183. * Gets the youtube stream length.
  184. *
  185. * @param playerid The playerid
  186. *
  187. * @author Dayvison
  188. * @date 05-10-2016
  189. * @return The length of stream
  190. */
  191. stock GetYoutubeStreamLen(playerid)
  192. {
  193. if(!IsPlayerConnected(playerid))
  194. return 0;
  195. return youtube_len[playerid];
  196. }
  197. /**
  198. * Gets the youtube stream position x.
  199. *
  200. * @param playerid The playerid
  201. *
  202. * @author Dayvison
  203. * @date 05-10-2016
  204. * @return The position x
  205. */
  206. stock GetYoutubeStreamPosX(playerid)
  207. {
  208. if(!IsPlayerConnected(playerid))
  209. return 0.0;
  210. return youtube_posX[playerid];
  211. }
  212. /**
  213. * Gets the youtube stream position y.
  214. *
  215. * @param playerid The playerid
  216. *
  217. * @author Dayvison
  218. * @date 05-10-2016
  219. * @return The position y
  220. */
  221. stock GetYoutubeStreamPosY(playerid)
  222. {
  223. if(!IsPlayerConnected(playerid))
  224. return 0.0;
  225. return youtube_posY[playerid];
  226. }
  227. /**
  228. * Gets the youtube stream position z.
  229. *
  230. * @param playerid The playerid
  231. *
  232. * @author Dayvison
  233. * @date 05-10-2016
  234. * @return The position z
  235. */
  236. stock GetYoutubeStreamPosZ(playerid)
  237. {
  238. if(!IsPlayerConnected(playerid))
  239. return 0.0;
  240. return youtube_posZ[playerid];
  241. }
  242. /**
  243. * Gets the youtube stream distance.
  244. *
  245. * @param playerid The playerid
  246. *
  247. * @author Dayvison
  248. * @date 05-10-2016
  249. * @return The stream distance
  250. */
  251. stock GetYoutubeStreamDistance(playerid)
  252. {
  253. if(!IsPlayerConnected(playerid))
  254. return 0.0;
  255. return youtube_distance[playerid];
  256. }
  257. /**
  258. * Determines if youtube stream use position.
  259. *
  260. * @param playerid The playerid
  261. *
  262. * @author Dayvison
  263. * @date 05-10-2016
  264. * @return The usepos
  265. */
  266. stock bool:IsYoutubeStreamUsePos(playerid)
  267. {
  268. if(!IsPlayerConnected(playerid))
  269. return false;
  270. return youtube_usepos[playerid];
  271. }
  272. /*public YoutubeResponse(playerid, response, data[])
  273. {
  274. if(response == 200)
  275. {
  276. if(strfind(data, "No video was found") != -1 || strfind(data, "<meta http-equiv=") != -1)
  277. {
  278. #if defined OnPlayYoutubeUrl
  279. OnPlayYoutubeUrl(playerid, "\0", 0, -1);
  280. #endif
  281. return;
  282. }
  283. new
  284. title[60],
  285. len[5];
  286. unformat(params, "P<</br>>is[32]f", a, b, c);
  287. youtube_len[playerid] = strval(len);
  288. PlayAudioStreamForPlayer(playerid, data, youtube_posX[playerid], youtube_posY[playerid], youtube_posZ[playerid], youtube_distance[playerid], youtube_usepos[playerid]);
  289. }
  290. }*/