-
Notifications
You must be signed in to change notification settings - Fork 2
/
youtube_time_url.py
52 lines (40 loc) · 1.64 KB
/
youtube_time_url.py
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
# coding: utf-8
## Takes a youtube URL and time code and copies time-coded URL to clipboard
import appex, requests, clipboard, console, dialogs, collections, sys
# Thanks to @cclauss
# from https://github.com/cclauss/Ten-lines-or-less/blob/master/form_dialog_from_fields_dict.py
def form_dialog_from_fields_dict(title, fields_dict):
return dialogs.form_dialog(title, [{'title': k, 'type': v} for k, v in fields_dict.items()])
my_fields_dict1 = collections.OrderedDict((
('URL', 'url'), ('Time', 'text')
))
my_fields_dict2 = collections.OrderedDict((
('Time', 'text'),
))
if appex.is_running_extension():
url = appex.get_url()
d = form_dialog_from_fields_dict("Enter timestamp as mm:ss", my_fields_dict2)
if d is None: sys.exit()
ts = d['Time']
else:
d = form_dialog_from_fields_dict("Enter YouTube URL and time (mm:ss)", my_fields_dict1)
if d is None: sys.exit()
url = d['URL']
ts = d['Time']
def main():
assert any([x in url for x in ("youtube", "youtu.be")]), "{0} is not a YouTube URL!".format(url)
assert ":" in ts, "timestamp must be written as (hh:)mm:ss"
if ts.count(":") == 1:
mins, secs = map(int, ts.split(":"))
hrs = 0
elif ts.count(":") == 2:
hrs, mins, secs = map(int, ts.split(":"))
else:
sys.stderr.write("Bad timestamp (too many ':')")
sys.exit(0)
seconds = hrs*(60**2) + mins*(60**1) + secs*(60**0)
newurl = "{url}?t={seconds}".format(url=url, seconds=seconds)
clipboard.set(newurl)
dialogs.hud_alert("{0} copied to clipboard".format(newurl))
return newurl
main()