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.0
11
+ * @author Dayvison
12
+ * @date 04-10-2016
13
+ */
14
+
15
+ #include < a_http>
16
+ #tryinclude < regex> // [Plugin] Regular expression by Fro1sha: http://forum.sa-mp.com/showthread.php?t=247893
17
+ #if defined _regex_included
18
+ # define USE_REGEX
19
+ #endif
20
+ static
21
+ Float: youtube_posX[MAX_PLAYERS],
22
+ Float: youtube_posY[MAX_PLAYERS],
23
+ Float: youtube_posZ[MAX_PLAYERS],
24
+ Float: youtube_distance[MAX_PLAYERS],
25
+ youtube_usepos[MAX_PLAYERS]
26
+ ;
27
+ #if defined OnPlayYoutubeUrl
28
+ /* *
29
+ * Called when youtube url an played.
30
+ *
31
+ * @param playerid The playerid
32
+ * @param title The title of url(if invalid \0)
33
+ * @param len The length of url(if invalid 0)
34
+ * @param response The response of url
35
+ * url codes:
36
+ * 1 Sucess
37
+ * -1 Invalid url
38
+ * -2 Invalid page
39
+ */
40
+ forward OnPlayYoutubeUrl (playerid, title[], len, response);
41
+ #endif
42
+
43
+ forward YoutubeResponse (playerid, response, data[]);
44
+ public YoutubeResponse (playerid, response, data[])
45
+ {
46
+ if (response == 200 )
47
+ {
48
+ if (strfind (data, " No video was found" ) != - 1 || strfind (data, " <meta http-equiv=" ) != - 1 )
49
+ {
50
+ #if defined OnPlayYoutubeUrl
51
+ OnPlayYoutubeUrl (playerid, " \0 " , 0 , - 1 );
52
+ #endif
53
+ return ;
54
+ }
55
+ new
56
+ title[60 ],
57
+ len[5 ],
58
+ Find
59
+ ;
60
+ Find = strfind (data, " <br/>" );
61
+ strmid (title, data, 7 , Find- 1 );
62
+ strdel (data, 0 , Find+ 5 );
63
+
64
+ Find = strfind (data, " <br/>" );
65
+ strmid (len, data, 8 , Find- 1 );
66
+ strdel (data, 0 , Find+ 11 );
67
+ PlayAudioStreamForPlayer (playerid, data, youtube_posX[playerid], youtube_posY[playerid], youtube_posZ[playerid], youtube_distance[playerid], youtube_usepos[playerid]);
68
+ #if defined OnPlayYoutubeUrl
69
+ OnPlayYoutubeUrl (playerid, title, strval (len), 1 );
70
+ #endif
71
+ }
72
+ else
73
+ {
74
+ #if defined OnPlayYoutubeUrl
75
+ OnPlayYoutubeUrl (playerid, " \0 " , 0 , - 2 );
76
+ #endif
77
+ }
78
+ }
79
+ #if defined USE_REGEX
80
+ /* *
81
+ * Determines if valid youtube url.
82
+ *
83
+ * @param url The url
84
+ *
85
+ * @author Dayvison
86
+ * @date 04-10-2016
87
+ * @return True if is a valid url, false not.
88
+ */
89
+ stock IsValidYoutubeUrl (const url[])
90
+ {
91
+ static RegEx: rHex;
92
+ if (! rHex)
93
+ {
94
+ rHex = regex_build (\" ((http(s)?:\/\/ )?)(www\. )?((youtube\. com\/ )|(youtu.be\/ ))[\S ]+" );
95
+ }
96
+ return regex_match_exid (url, rHex);
97
+ }
98
+ #endif
99
+ /* *
100
+ * Converts an youtu.be url to youtube.com
101
+ *
102
+ * @param Url The url to convert
103
+ * @param size The size
104
+ *
105
+ * @author Dayvison
106
+ * @date 04-10-2016
107
+ * @return True if is conver have sucess, false not.
108
+ */
109
+ stock ConvertYoutubeUrl (Url[], size = sizeof (Url))
110
+ {
111
+ new find;
112
+ if ((find = strfind (Url, " youtu.be/" )) == - 1 )
113
+ return false ;
114
+ strdel (Url, 0 , find + 4 );
115
+ format (Url, size, " https://www.youtube.com/watch?v=" , Url);
116
+ return true ;
117
+ }
118
+
119
+ /* *
120
+ * Play an youtube 'audio stream' for a player.
121
+ *
122
+ * @param playerid The ID of the player to play the audio for.
123
+ * @param url The url to play.
124
+ * @param posX The X position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
125
+ * @param posY The Y position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
126
+ * @param posZ The Z position at which to play the audio. Default 0.0. Has no effect unless usepos is set to 1.
127
+ * @param distance The distance over which the audio will be heard. Has no effect unless usepos is set to 1.
128
+ * @param usepos Use the positions and distance specified. Default disabled (0).
129
+ *
130
+ * @author Dayvison
131
+ * @date 04-10-2016
132
+ * @return True on sucess, or false on failure. if USE_REGEX active is more efficient
133
+ */
134
+ stock PlayYoutubeForPlayer (playerid, url[], Float: posX = 0 .0 , Float: posY = 0 .0 , Float: posZ = 0 .0 , Float: distance = 50 .0 , usepos = 0 )
135
+ {
136
+ if (! IsPlayerConnected (playerid))
137
+ return false ;
138
+ #if defined USE_REGEX
139
+ if (! IsValidYoutubeUrl (url))
140
+ {
141
+ if (! ConvertYoutubeUrl (url, 256 ))
142
+ return false ;
143
+ }
144
+ #else
145
+ if (strfind (url, " youtu.be/" ) != - 1 )
146
+ if (! ConvertYoutubeUrl (url, 256 ))
147
+ return false ;
148
+ #endif
149
+ static string[256 ];
150
+ format (string, sizeof (string), " youtubeinmp3.com/fetch/?format=text&video=%s " , url);
151
+ HTTP (playerid, HTTP_GET, string, " " , " YoutubeResponse" );
152
+ youtube_posX[playerid] = posX;
153
+ youtube_posY[playerid] = posY;
154
+ youtube_posZ[playerid] = posZ;
155
+ youtube_distance[playerid] = distance;
156
+ youtube_usepos[playerid] = usepos;
157
+ return true ;
158
+ }
0 commit comments