]> gitweb.factorcode.org Git - factor.git/blob - extra/youtube/youtube.factor
use reject instead of [ ... not ] filter.
[factor.git] / extra / youtube / youtube.factor
1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: assocs http.client kernel make math.order sequences
5 splitting urls urls.encoding ;
6
7 IN: youtube
8
9 TUPLE: encoding extension resolution video-codec profile
10 video-bitrate audio-codec audio-bitrate ;
11
12 CONSTANT: encodings H{
13
14     ! Flash Video
15     { 5 T{ encoding f "flv" "240p" "Sorenson H.263" f "0.25" "MP3" 64 } }
16     { 6 T{ encoding f "flv" "270p" "Sorenson H.263" f "0.8" "MP3" 64 } }
17     { 34 T{ encoding f "flv" "360p" "H.264" "Main" "0.5" "AAC" 128 } }
18     { 35 T{ encoding f "flv" "480p" "H.264" "Main" "0.8-1" "AAC" 128 } }
19
20     ! 3GP
21     { 36 T{ encoding f "3gp" "240p" "MPEG-4 Visual" "Simple" "0.17" "AAC" 38 } }
22     { 13 T{ encoding f "3gp" f "MPEG-4 Visual" f "0.5" "AAC" f } }
23     { 17 T{ encoding f "3gp" "144p" "MPEG-4 Visual" "Simple" "0.05" "AAC" 24 } }
24
25     ! MPEG-4
26     { 18 T{ encoding f "mp4" "360p" "H.264" "Baseline" "0.5" "AAC" 96 } }
27     { 22 T{ encoding f "mp4" "720p" "H.264" "High" "2-2.9" "AAC" 192 } }
28     { 37 T{ encoding f "mp4" "1080p" "H.264" "High" "3-4.3" "AAC" 192 } }
29     { 38 T{ encoding f "mp4" "3072p" "H.264" "High" "3.5-5" "AAC" 192 } }
30     { 82 T{ encoding f "mp4" "360p" "H.264" "3D" "0.5" "AAC" 96 } }
31     { 83 T{ encoding f "mp4" "240p" "H.264" "3D" "0.5" "AAC" 96 } }
32     { 84 T{ encoding f "mp4" "720p" "H.264" "3D" "2-2.9" "AAC" 152 } }
33     { 85 T{ encoding f "mp4" "520p" "H.264" "3D" "2-2.9" "AAC" 152 } }
34
35     ! WebM
36     { 43 T{ encoding f "webm" "360p" "VP8" f "0.5" "Vorbis" 128 } }
37     { 44 T{ encoding f "webm" "480p" "VP8" f "1" "Vorbis" 128 } }
38     { 45 T{ encoding f "webm" "720p" "VP8" f "2" "Vorbis" 192 } }
39     { 46 T{ encoding f "webm" "1080p" "VP8" f f "Vorbis" 192 } }
40     { 100 T{ encoding f "webm" "360p" "VP8" "3D" f "Vorbis" 128 } }
41     { 101 T{ encoding f "webm" "360p" "VP8" "3D" f "Vorbis" 192 } }
42     { 102 T{ encoding f "webm" "720p" "VP8" "3D" f "Vorbis" 192 } }
43 }
44
45 CONSTANT: video-info-url URL" http://www.youtube.com/get_video_info"
46
47 : get-video-info ( video-id -- video-info )
48     video-info-url clone
49         3 "asv" set-query-param
50         "detailpage" "el" set-query-param
51         "en_US" "hl" set-query-param
52         swap "video_id" set-query-param
53     http-get nip query>assoc ;
54
55 : video-formats ( video-info -- video-formats )
56     "url_encoded_fmt_stream_map" of "," split
57     [ query>assoc ] map ;
58
59 : video-download-url ( video-format -- url )
60     [ "url" of ] [ "sig" of ] bi "&signature=" glue ;
61
62 : sanitize ( title -- title' )
63     [ 0 31 between? ] reject
64     [ "\"#$%'*,./:;<>?^|~\\" member? ] reject
65     200 short head ;
66
67 : download-video ( video-id -- )
68     get-video-info [
69         video-formats [ "type" of "video/mp4" head? ] find nip
70         video-download-url
71     ] [
72         "title" of sanitize ".mp4" append download-to
73     ] bi ;