Instantly share code, notes, and snippets.

Embed
What would you like to do?
moc-2.6.0-svn-r2935 youtube-dl patch
diff --git a/files.c b/files.c
index 4d2b24d..53c3639 100644
--- a/files.c
+++ b/files.c
@@ -23,6 +23,7 @@
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
+#include <regex.h>
#ifdef HAVE_LIBMAGIC
#include <magic.h>
@@ -85,6 +86,7 @@ void files_cleanup ()
inline int is_url (const char *str)
{
return !strncasecmp (str, "http://", sizeof ("http://") - 1)
+ || !strncasecmp (str, "https://", sizeof ("https://") - 1)
|| !strncasecmp (str, "ftp://", sizeof ("ftp://") - 1);
}
@@ -710,14 +712,96 @@ char *absolute_path (const char *path, const char *cwd)
result = (char *)xmalloc (sizeof(char) * (strlen(tmp)+1));
strcpy (result, tmp);
- }
- else {
- result = (char *)xmalloc (sizeof(char) * (strlen(path)+1));
- strcpy (result, path);
+ } else {
+ if (is_yt_url(path)) {
+ /* do conversion of link with youtube-dl */
+ result = parse_yt_link(path);
+ } else {
+ result = (char *)xmalloc (sizeof(char) * (strlen(path)+1));
+ strcpy (result, path);
+ }
}
return result;
}
+/* use a regex
+ * This will allow to identify if the link is a youtube link.
+ * */
+bool is_yt_url(const char *str)
+{
+ regex_t regex;
+ int regrc;
+ bool ret;
+ char error[256];
+
+ /* Build regex to check for youtube link. */
+ regrc = regcomp(&regex, "https?://(www[.]|m[.])?(youtube[.]com|youtu[.]be)/",REG_EXTENDED);
+ if (regrc) {
+ /* we could not compile regex return false.*/
+ regerror(regrc,(const regex_t *) &regex, error, sizeof(error));
+ //fprintf(stderr,"DEBUG: no compile:\n%s\n%s\n",str,error);
+ assert(0);
+ return false;
+ }
+
+ regrc = regexec(&regex,str,0,NULL,0);
+ if (!regrc) {
+ /* yeah youtube link */
+ ret = true;
+ } else {
+ /* something went wrong so no youtubelink */
+ ret = false;
+ }
+
+ regfree(&regex);
+ return ret;
+}
+
+char * parse_yt_link(const char * url)
+{
+ char cmd[256] = {0x0};
+ char result[4096] = {0x0};
+ char * tmp;
+ FILE *in=NULL;
+ long total=0;
+ int ret;
+ size_t size;
+
+#ifdef NDEBUG
+#define OUTPUT "/dev/null"
+#else
+#define OUTPUT "/tmp/mocp-youtubedl-log"
+#endif
+ snprintf(cmd, sizeof(cmd),"/usr/local/bin/youtube-dl --no-call-home --format 'bestaudio' -g %s 2>%s",url,OUTPUT);
+ in=popen(cmd,"r");
+ if(in == NULL) {
+ perror("Shell execution error");
+ return url;
+ }
+ while(fgets(result,4096,in) != NULL) {
+ total+=atol(result);
+ }
+ if(!feof(in)) {
+ perror("Input Stream error");
+ return url;
+ }
+ pclose(in);
+
+ /* remove newline from fgets */
+ char * pos;
+ if ((pos=strchr(result, '\n')) != NULL)
+ *pos = '\0';
+
+ /* allocate memory for link */
+ size = strlen(result);
+ tmp = (char *)xmalloc (sizeof(char) * (strlen(result)+1));
+ strcpy (tmp, result);
+ if (tmp != NULL) {
+ return tmp;
+ } else {
+ return url;
+ }
+}
/* Check that a file which may cause other applications to be invoked
* is secure against tampering. */
diff --git a/files.h b/files.h
index e7ecd79..6d0724a 100644
--- a/files.h
+++ b/files.h
@@ -36,6 +36,8 @@ int is_dir (const char *file);
int can_read_file (const char *file);
char *absolute_path (const char *path, const char *cwd);
bool is_secure (const char *file);
+bool is_yt_url (const char *file);
+char * parse_yt_link(const char * url);
#ifdef __cplusplus
}
diff --git a/io_curl.c b/io_curl.c
index fbf8e45..3b85feb 100644
--- a/io_curl.c
+++ b/io_curl.c
@@ -258,6 +258,7 @@ void io_curl_open (struct io_stream *s, const char *url)
curl_easy_setopt (s->curl.handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt (s->curl.handle, CURLOPT_FAILONERROR, 1);
curl_easy_setopt (s->curl.handle, CURLOPT_MAXREDIRS, 15);
+ curl_easy_setopt (s->curl.handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (s->curl.handle, CURLOPT_HTTP200ALIASES,
s->curl.http200_aliases);
curl_easy_setopt (s->curl.handle, CURLOPT_HTTPHEADER,
diff --git a/player.c b/player.c
index 7074493..9f6de4d 100644
--- a/player.c
+++ b/player.c
@@ -851,7 +851,14 @@ void player (const char *file, const char *next_file, struct out_buf *out_buf)
status_msg ("Connecting...");
LOCK (decoder_stream_mtx);
- decoder_stream = io_open (file, 1);
+ char *url;
+ if(is_yt_url(file)) {
+ url = parse_yt_link(file);
+ logit("[YT]: converted %s to %s",file,url);
+ } else {
+ url = file;
+ }
+ decoder_stream = io_open (url, 1);
if (!io_ok(decoder_stream)) {
error ("Could not open URL: %s", io_strerror(decoder_stream));
io_close (decoder_stream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment