-
Notifications
You must be signed in to change notification settings - Fork 4
/
WorkerThread.cpp
99 lines (84 loc) · 2.15 KB
/
WorkerThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2014. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, [email protected]
*
* based on ideas of YAVTD for Haiku
* Version 1.0 by Leszek Lesner (C) 2011
*/
#include "WorkerThread.h"
WorkerThread::WorkerThread(const BMessenger& owner)
:
BLooper("title_getter"),
fOwner(owner)
{
Run();
}
void
WorkerThread::MessageReceived(BMessage* message)
{
switch (message->what) {
case msgGETTITLE:
{
BString url;
if (message->FindString("url", &url) == B_OK) {
BMessage msg(msgTITLE);
msg.AddString("title", _GetTitle(url));
fOwner.SendMessage(&msg);
}
break;
}
case msgGETCLIP:
{
BString url;
if (message->FindString("url", &url) == B_OK)
_GetClip(url);
break;
}
default:
BLooper::MessageReceived(message);
}
}
BString
WorkerThread::_GetTitle(BString url)
{
BString command("youtube-dl --get-title %URL% 2>&1 | tail -n 1"); // also get output from error out
command.ReplaceAll("%URL%", url.String());
char title[1000];
FILE* pget_title;
pget_title = popen(command.String(),"r");
fread(title, 1, sizeof(title), pget_title);
fclose(pget_title);
/* strip trailing newline */
for (int i = 0; (unsigned) i < strlen(title); i++) {
if (title[i] == '\n' || title[i] == '\r' )
title[i] = '\0';
}
return title;
}
bool
WorkerThread::_GetClip(BString url)
{
BString* command = new BString(
"mkdir -p /tmp/ubertuber ; "
"cd /tmp/ubertuber ; "
"hey application/x-vnd.UberTuber down ; "
"youtube-dl --continue --restrict-filenames --no-part --no-cache-dir --format best %URL% ; "
"while [ -n \"$(%TEST%)\" ] ; do " // wait for script to finish/aborted
"sleep 2 ; "
"done ; "
"FILE=$(youtube-dl --restrict-filenames --get-filename %URL% 2>&1 | tail -n 1) ; "
"addattr -t string META:url %URL% \"$FILE\" ; "
"if [ -e \"$FILE\" ] ; then "
"hey application/x-vnd.UberTuber gfin ; "
"else hey application/x-vnd.UberTuber erro ; "
"fi ; "
"exit");
BString threadtest("ps | grep python | grep \"youtube-dl --con\"");
command->ReplaceAll("%TEST%", threadtest.String());
command->ReplaceAll("%URL%", url.String());
system(command->String());
return true;
}