Skip to content

Commit 4773fab

Browse files
committed
Add files, and version 1.0.0
0 parents  commit 4773fab

File tree

4 files changed

+296
-0
lines changed

4 files changed

+296
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Dayvison
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SAMP youtube-stream
2+
Include to allow you reproduce an youtube url
3+
4+
| Total downloads | Latest release |
5+
| :---: | :---: |
6+
[![All Releases](https://img.shields.io/github/downloads/Dayvison/SAMP-youtube-stream/total.svg?maxAge=86400)]() | [![latest release](https://img.shields.io/github/release/Dayvison/SAMP-youtube-stream.svg?maxAge=86400)](https://github.com/Dayvison/SAMP-youtube-stream/releases) <br> [![Github Releases](https://img.shields.io/github/downloads/Dayvison/SAMP-youtube-stream/latest/total.svg?maxAge=86400)](https://github.com/Dayvison/SAMP-youtube-stream/releases) |
7+
8+
# Todo: functions documentation here in markdown

tests.pwn

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <a_samp>
2+
#include <core>
3+
#include <audio>
4+
#include <youtube_stream>
5+
6+
#pragma tabsize 0
7+
8+
main()
9+
{
10+
print("\n----------------------------------");
11+
print(" Bare Script\n");
12+
print("----------------------------------\n");
13+
}
14+
15+
public OnPlayerConnect(playerid)
16+
{
17+
GameTextForPlayer(playerid,"~w~SA-MP: ~r~Bare Script",5000,5);
18+
return 1;
19+
}
20+
const Dialog_3 = 1;
21+
public OnPlayerCommandText(playerid, cmdtext[])
22+
{
23+
new idx;
24+
new str[256];
25+
26+
str = strtok(cmdtext, idx);
27+
if(strcmp(str, "/play", true) == 0) {
28+
str = strtok(cmdtext, idx);
29+
if(PlayYoutubeForPlayer(playerid, str))
30+
SendClientMessage(playerid, -1, "wait for the music will start.");
31+
else
32+
SendClientMessage(playerid, -1, "Invalid url.");
33+
return 1;
34+
}
35+
36+
return 0;
37+
}
38+
public OnPlayYoutubeUrl(playerid, title[], len, response)
39+
{
40+
new string[156];
41+
if(response == 1)
42+
{
43+
format(string, sizeof(string), "A music %s, as started with %d seconds.", title, len);
44+
SendClientMessage(playerid, -1, string);
45+
}
46+
else
47+
{
48+
SendClientMessage(playerid, -1, "Error: have a fail with your music.");
49+
}
50+
}
51+
public OnPlayerSpawn(playerid)
52+
{
53+
SetPlayerInterior(playerid,0);
54+
TogglePlayerClock(playerid,0);
55+
SendClientMessage(playerid, -1, "To test this include use:");
56+
SendClientMessage(playerid, -1, "{0000FF}/play <youtube-url>{FFFFFF} an youtube url will be played to you.");
57+
return 1;
58+
}
59+
60+
public OnPlayerDeath(playerid, killerid, reason)
61+
{
62+
return 1;
63+
}
64+
65+
SetupPlayerForClassSelection(playerid)
66+
{
67+
SetPlayerInterior(playerid,14);
68+
SetPlayerPos(playerid,258.4893,-41.4008,1002.0234);
69+
SetPlayerFacingAngle(playerid, 270.0);
70+
SetPlayerCameraPos(playerid,256.0815,-43.0475,1004.0234);
71+
SetPlayerCameraLookAt(playerid,258.4893,-41.4008,1002.0234);
72+
}
73+
74+
public OnPlayerRequestClass(playerid, classid)
75+
{
76+
SetupPlayerForClassSelection(playerid);
77+
return 1;
78+
}
79+
80+
public OnGameModeInit()
81+
{
82+
SetGameModeText("Bare Script");
83+
ShowPlayerMarkers(1);
84+
ShowNameTags(1);
85+
AllowAdminTeleport(1);
86+
87+
AddPlayerClass(265,1958.3783,1343.1572,15.3746,270.1425,0,0,0,0,-1,-1);
88+
89+
return 1;
90+
}
91+
92+
strtok(const string[], &index)
93+
{
94+
new length = strlen(string);
95+
while ((index < length) && (string[index] <= ' '))
96+
{
97+
index++;
98+
}
99+
100+
new offset = index;
101+
new result[156];
102+
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
103+
{
104+
result[index - offset] = string[index];
105+
index++;
106+
}
107+
result[index - offset] = EOS;
108+
return result;
109+
}

youtube_stream.inc

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)