aboutsummaryrefslogtreecommitdiff
path: root/watch.c
diff options
context:
space:
mode:
Diffstat (limited to 'watch.c')
-rw-r--r--watch.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/watch.c b/watch.c
new file mode 100644
index 0000000..9d9169c
--- /dev/null
+++ b/watch.c
@@ -0,0 +1,101 @@
+#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
+#error Systems other than Windows are not supported.
+#endif
+
+#include <windows.h>
+#include <shellapi.h>
+#pragma comment(lib, "Shell32")
+#include <shlwapi.h>
+#pragma comment(lib, "Shlwapi.lib")
+
+#include <stdio.h>
+
+#define err(code, string) do { fprintf(stderr, "%s: %s: %s\n", string, strerror(errno)); exit(code); } while (0)
+#define die(...) do { fprintf(stderr, __VA_ARGS__); system("pause"); exit(1); } while (0)
+#define bool int
+#define true 1
+#define false 0
+
+#define MAX_PATH 260
+#define SR_ERR_NOASSOC (HINSTANCE)31
+#define USAGE "usage: %s URL\n", argv[0]
+
+int main(int argc, char *argv[]) {
+ bool new;
+ char *cmd, out[MAX_PATH], temp[MAX_PATH];
+ FILE *f, *p;
+ HINSTANCE r;
+ int c, i, linecount;
+ ULONGLONG last;
+
+ /* construct temporary playlist file path */
+
+ if (GetTempPath(1 + MAX_PATH, temp) > 0 && strlen(temp) + 1 + 16 <= MAX_PATH) {
+ strcpy(out, temp);
+ if (temp[strlen(temp)-1] != '\\') strcat(out, "\\");
+ }
+ strcat(out, "watch.tmp.mpcpl");
+
+ if (argc == 1) goto open; /* open previous link */
+ if (argc > 2) die(USAGE);
+
+ if (strncmp(argv[1], "watch:", 6) == 0) /* support watch: urls */
+ argv[1] += 6;
+
+ for (i = 0; i < strlen(argv[1]); i++)
+ if (argv[1][i] == '"') die("URL cannot contain quotes (\")\n");
+
+ /* start youtube-dl */
+
+ cmd = malloc(sizeof("youtube-dl -g \"\"") + strlen(argv[1]) * sizeof(char));
+ if (cmd == NULL) err(1, "malloc");
+ if (sprintf(cmd, "youtube-dl -g \"%s\"", argv[1]) < 0) err(1, "sprintf");
+
+ p = _popen(cmd, "r");
+ if (p == NULL) err(1, "popen");
+
+ /* open temporary playlist file */
+
+ f = fopen(out, "w");
+ if (f == NULL) err(1, "fopen");
+
+ /* read/write stream urls */
+
+ linecount = 0;
+ new = true;
+ last = GetTickCount();
+
+ c = fgetc(p);
+ if (c == EOF) goto end;
+ fprintf(f, "MPCPLAYLIST\n");
+ goto loop;
+
+ while ((c = fgetc(p)) != EOF) {
+loop:
+ if (new) {
+ /* if new line immediately follows last one, it is only audio */
+ if (GetTickCount() - last > 20) {
+ fprintf(stderr, "Retrieved video #%d\n", linecount);
+ linecount++;
+ fprintf(f, "%d,type,0\n", linecount);
+ }
+ fprintf(f, "%d,filename,", linecount, linecount);
+ new = false;
+ }
+ fputc(c, f);
+ if (c == '\n') {
+ last = GetTickCount();
+ new = true;
+ }
+ }
+
+end:
+ if (i = _pclose(p))
+ die("youtube-dl exited with %d\n", i);
+
+ /* open playlist */
+open:
+ if (ShellExecute(NULL, "open", out, NULL, NULL, 5) <= (HINSTANCE)32)
+ die("failed to open %s\n", out);
+ return 0;
+} \ No newline at end of file