├── .gitignore
├── LICENSE
├── README.md
├── domain-whitelist.conf
├── i3blocks.conf
├── youtubed
├── youtubed_controller
└── youtubed_validate-url.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.out
2 | *.swp
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | youtubed - A bash daemon for controlling youtube-dl
2 |
3 |
4 |
5 | Development copy, expect bugs.
6 |
7 | - [INSTALLATION] (#installation)
8 | - [USE] (#use)
9 | - [USE WITH TOR] (#use-with-tor)
10 | - [OPTIONS] (#options)
11 | - [BUGS] (#bugs)
12 |
13 | # INSTALLATION
14 |
15 | These scripts were written to be used with [i3](http://www.i3wm.org) and [i3blocks](https://github.com/vivien/i3blocks), so we assume you have a working installation of each.
16 | It is possible to use youtubed with other status line displayers besides i3blocks, but we will not go into detail about how to do so.
17 | With that in mind, let's get started.
18 |
19 | Install the dependencies:
20 |
21 | ```ShellSession
22 | sudo aptitude install youtube-dl inotify-tools xclip
23 | ```
24 |
25 | Clone the repo:
26 |
27 | ```ShellSession
28 | git clone https://github.com/kb100/youtubed.git
29 | ```
30 |
31 | No need to compile anything, just put the scripts somewhere in your path:
32 |
33 | ```ShellSession
34 | sudo cp youtubed/{youtubed,youtubed_controller,youtubed_validate-url.py} /usr/local/bin
35 | ```
36 |
37 | You only need the url validator if you plan on using the `--domain-whitelist` option.
38 |
39 |
40 | Edit your i3blocks config (e.g. `$HOME/.config/i3blocks/config`):
41 |
42 | ```INI
43 | [youtube_controller]
44 | command=$SCRIPT_DIR/youtubed_controller
45 | interval=once
46 | markup=pango
47 | signal=1
48 | # use pkill -RTMIN+1 i3blocks to trigger a block update
49 | ```
50 |
51 | Run the youtubed script:
52 |
53 | ```ShellSession
54 | youtubed --download-dir=$HOME/.youtubed --media-cmd="i3-msg exec mpv"
55 | ```
56 |
57 | where you can replace mpv with your favorite media player, or use `--media-cmd=""` to disable automatic opening of files after download completes.
58 | If you specify `--daemon-dir`, you must also specify the same directory in the i3blocks blocklet command:
59 |
60 | ```INI
61 | command=$SCRIPT_DIR/youtubed_controller $DAEMON_DIR
62 | ```
63 |
64 | After you have setup your i3blocks configuration, restart i3 inplace:
65 |
66 | ```ShellSession
67 | i3-msg restart
68 | ```
69 |
70 | Your youtubed setup is now running!
71 | You should now see `[DL]` in your status line.
72 |
73 | 
74 |
75 | # USE
76 |
77 | Left click the blocklet to trigger a download of the video from your clipboard.
78 | Right click the blocklet to cancel a download of the video from your clipboard.
79 |
80 | 
81 |
82 | These can also be done by sending SIGUSR1 and SIGUSR2 respectively, e.g.:
83 |
84 | ```ShellSession
85 | pkill -SIGUSR1 youtubed
86 | ```
87 |
88 | or by sending commands directly to youtubed's command fifo:
89 |
90 | ```ShellSession
91 | echo "download $URL" > $DAEMON_DIR/youtubed.fifo
92 | ```
93 |
94 | A simple "download" or "cancel" command with no other arguments will use the clipboard's content's
95 | as the URL to download or cancel.
96 | You can specify alternative download and cancel signals with `--download-signal` and `--cancel-signal`.
97 | If youtubed is in the middle of a download, left clicking will either begin downloading the next video in parallel,
98 | or gracefully restart youtube-dl and resume the download if the video URL matches one that is already downloading.
99 | This is useful because sometimes youtube decides to throttle your download speed, and simply restarting the download
100 | may significantly increase your speeds.
101 | Multiple simultaneous downloads are supported, just copy URL, click, copy URL, click.
102 | Terminating youtubed can be done with a SIGTERM, with a SIGINT (CTRL-C) if youtubed is running in the foreground, or by sending the message "die" to the fifo.
103 |
104 | By default, youtubed will tell youtube-dl to get the best quality video and audio to download.
105 | You can use your mouse wheel and scroll up over the blocklet to change between: bestvideo+bestaudio, bestaudio, worstvideo+worstaudio, and worstaudio.
106 | You can also specify a custom quality with the `--default-quality` flag or by sending "quality FORMAT" to the fifo.
107 | The format must be a valid youtube-dl format string (see the `--format` option in youtube-dl's man page).
108 | You can also send "toggle_quality" to the fifo to automatically switch between the formats listed above.
109 |
110 | # USE WITH TOR
111 |
112 | Due to an unknown change in youtube-dl, torsocks no longer works with youtube-dl on some systems.
113 | In the event that your system is not compatible, attempting to run youtube-dl with torsocks will cause your cpus to max out and ram will be malloced at over 1GB per second.
114 |
115 | If compatible, we recommend the use of [tor](https://www.torproject.org/) and torsocks in combination with youtubed.
116 | Of course you will need to install them first:
117 |
118 | ```ShellSession
119 | sudo aptitude install tor torsocks
120 | ```
121 |
122 | Prepending `torsocks` to your youtubed call should allow you to use youtubed over tor:
123 |
124 | ```ShellSession
125 | torsocks youtubed [usual options] [--default-quality="worstvideo+worstaudio"] [--domain-whitelist="domain-whitelist.conf"]
126 | ```
127 |
128 | Please be mindful that the tor network is not (yet) equipped for high bandwidth users, so be considerate and set the download quality to worstvideo+worstaudio or worstaudio when using tor.
129 | This can be accomplished with the `--default-quality` option as above.
130 |
131 | **WARNING: by nature of how youtubed works, the contents of your clipboard may be sent over the tor network upon executing a youtubed command. If your clipboard contains sensitive or identifying information, this could be a serious problem for you. We strongly suggest using the `--domain whitelist` option as above to prevent yourself from revealing your clipboard should you accidentally click the blocklet.**
132 |
133 | Whitelist files should be lines with domain names one per line. Lines beginning with '#' are ignored.
134 |
135 | # OPTIONS
136 |
137 | -h --help Print this help message.
138 |
139 | --download-signal The signal that will trigger a download.
140 | Default: SIGUSR1
141 |
142 | --cancel-signal The signal that will cancel a download.
143 | Default: SIGUSR2
144 |
145 | --download-dir The directory to save downloads in.
146 | Default: $XDG_DATA_DIR/youtubed
147 |
148 | --exec-after-write A command to execute after youtube-dl writes a status
149 | line.
150 | Default: pkill -RTMIN+1 i3blocks
151 |
152 | --daemon-dir The directory that youtubed keeps its state files
153 | (.pid files, progress files, youtubed-fifo, status text
154 | files).
155 | Default: $XDG_RUNTIME_DIR/youtubed
156 |
157 | --format-str The format string to supply to youtube-dl.
158 | Default: %(title)s.%(id)s.%(resolution)s.%(ext)s
159 |
160 | --default-quality Set the default quality of videos to download.
161 | Default: bestvideo+bestaudio
162 |
163 | --media-cmd Use "media-cmd file" to open a file after download.
164 | Default: i3-msg exec mpv
165 |
166 | --domain-whitelist Reject url unless its domain is in the given file.
167 | Default:
168 |
169 | --run-in-foreground Tells youtubed to stay in the foreground.
170 |
171 | # BUGS
172 |
173 | Report bugs and suggestions at the [issues](https://github.com/kb100/youtubed/issues) page.
174 | Try running youtubed with the `--run-in-foreground` flag for some helpful output, and be sure to include this output when reporting a bug.
175 | Fixes and other contributions are welcome.
176 |
--------------------------------------------------------------------------------
/domain-whitelist.conf:
--------------------------------------------------------------------------------
1 | # These domains are allowed
2 | www.youtube.com
3 | youtube.com
4 |
--------------------------------------------------------------------------------
/i3blocks.conf:
--------------------------------------------------------------------------------
1 | # Copyright (C) 2015 James Murphy
2 | # Licensed under the terms of the GNU GPL v2 only.
3 |
4 | # An example i3blocks blocklet config
5 | # trigger updates with pkill -RTMIN+1 i3blocks
6 | [youtube_controller]
7 | command=$SCRIPT_DIR/youtube_controller
8 | interval=once
9 | markup=pango
10 | signal=1
11 |
--------------------------------------------------------------------------------
/youtubed:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2015 James Murphy
3 | # Licensed under the terms of the GNU GPL v2 only.
4 | #
5 | # Daemon for downloading and playing the youtube URL
6 | # in your clipboard. (Should work with any youtube-dl compatible URL.)
7 | #
8 | # Depends: youtube-dl, inotify-tools, xclip
9 | # Recommends: mpv | vlc, i3, i3blocks, python3
10 | # Suggests: tor, torsocks
11 | #
12 | # ./youtubed --download-dir=/data/videos/youtube
13 | #
14 | # Your ~/.config/i3blocks/config:
15 | # [youtubed_controller]
16 | # command=$SCRIPT_DIR/youtubed_controller DAEMON_DIR
17 | # interval=once
18 | # signal=1
19 |
20 | # User configurables
21 | DOWNLOAD_DIR="$XDG_DATA_DIR/youtubed"
22 | DAEMON_DIR="$XDG_RUNTIME_DIR/youtubed"
23 | DOWNLOAD_SIGNAL="SIGUSR1"
24 | CANCEL_SIGNAL="SIGUSR2"
25 | FIFO="youtubed.fifo"
26 | FORMAT_STR="%(title)s.%(id)s.%(resolution)s.%(ext)s"
27 | ID_ONLY_FMT="%(id)s"
28 | MERGE_OUTPUT_FORMAT="mkv"
29 | EXEC_AFTER_WRITE="pkill -RTMIN+1 i3blocks"
30 | MEDIA_CMD="i3-msg exec mpv"
31 | DOMAIN_WHITELIST=""
32 | QUALITY_STR="bestvideo+bestaudio"
33 |
34 | function setup_options {
35 | HELP_TEXT="Usage: youtubed options\n\n"
36 |
37 | function append_option {
38 | local SHORT=$1
39 | local LONG=$2
40 | local HELP=$3
41 | HELP=$(sed "s/%/%%/g" <<<$HELP)
42 | local HELP_LINE=""
43 | SHORTOPTS="$SHORTOPTS$SHORT"
44 | if [ -n "$LONGOPTS" ]; then
45 | LONGOPTS="$LONGOPTS,"
46 | fi
47 | LONGOPTS="$LONGOPTS$LONG"
48 | if [ -n "$SHORT" ]; then
49 | HELP_LINE="-$SHORT "
50 | fi
51 | if [ -n "$LONG" ]; then
52 | i=$((${#LONG}-1))
53 | if [ ${LONG:$i:1} == ":" ]; then
54 | LONG="${2%?}"
55 | fi
56 | LONG="--$LONG"
57 | HELP_LINE="$HELP_LINE$LONG "
58 | fi
59 | HELP_LINE="$HELP_LINE$HELP"
60 | HELP_TEXT="$HELP_TEXT$HELP_LINE\n"
61 | }
62 |
63 | append_option h help "\t\tPrint this help message.\n"
64 | append_option "" "download-signal:" "\tThe signal that will trigger a "\
65 | "download.\n\t\t\tDefault: $DOWNLOAD_SIGNAL\n"
66 | append_option "" "cancel-signal:" "\tThe signal that will cancel a "\
67 | "download.\n\t\t\tDefault: $CANCEL_SIGNAL\n"
68 | append_option "" "download-dir:" "\t\tThe directory to save downloads in."\
69 | "\n\t\t\tDefault: \$XDG_DATA_DIR/youtubed\n"
70 | append_option "" "exec-after-write:" "\tA command to execute after "\
71 | "youtube-dl writes a status\n\t\t\tline.\n\t\t\tDefault: $EXEC_AFTER_WRITE\n"
72 | append_option "" "daemon-dir:" "\t\tThe directory that youtubed keeps its "\
73 | "state files (.pid\n\t\t\tfiles, progress files, youtubed-fifo, status text"\
74 | "\n\t\t\tfiles).\n\t\t\tDefault: \$XDG_RUNTIME_DIR/youtubed\n"
75 | append_option "" "format-str:" "\t\tThe format string to supply to "\
76 | "youtube-dl.\n\t\t\tDefault: $FORMAT_STR\n"
77 | append_option "" "default-quality:" "\tSet the default quality of videos "\
78 | "to download.\n\t\t\tDefault: $QUALITY_STR\n"
79 | append_option "" "media-cmd:" "\t\tUse \"media-cmd file\" to open a file "\
80 | "after download.\n\t\t\tDefault: $MEDIA_CMD\n"
81 | append_option "" "domain-whitelist:" "\tReject url unless its domain is in "\
82 | "the given file.\n\t\t\tDefault: $DOMAIN_WHITELIST\n"
83 | append_option "" "run-in-foreground" "\tTells youtubed to stay in the "\
84 | "foreground."
85 | }
86 | setup_options
87 |
88 | function usage {
89 | printf "$HELP_TEXT";
90 | }
91 |
92 | function error_and_exit {
93 | echo "[youtubed error] $1" >&2
94 | echo "[youtubed] exiting" >&2
95 | exit 1
96 | }
97 |
98 | function error {
99 | echo "[youtubed error] $1" >&2
100 | }
101 |
102 | function info {
103 | echo "[youtubed] $1"
104 | }
105 |
106 | function pango_escape {
107 | local escaped="$1"
108 | escaped=${escaped//<}
109 | escaped=${escaped//>/>}
110 | escaped=${escaped//&/&}
111 | echo "$escaped"
112 | }
113 |
114 | function pango {
115 | echo "$(pango_escape "$2")"
116 | }
117 |
118 | function read_user_options {
119 | OPTS=`getopt -o $SHORTOPTS -l $LONGOPTS -- "$@"`
120 | if [ $? != 0 ]; then
121 | usage
122 | exit 1
123 | fi
124 |
125 | eval set -- "$OPTS"
126 |
127 | while true; do
128 | case "$1" in
129 | --download-signal) DOWNLOAD_SIGNAL="$2"; shift 2 ;;
130 | --cancel-signal) CANCEL_SIGNAL="$2"; shift 2 ;;
131 | --download-dir) DOWNLOAD_DIR="$2"; shift 2 ;;
132 | --exec-after-write) EXEC_AFTER_WRITE="$2"; shift 2 ;;
133 | --daemon-dir) DAEMON_DIR="$2"; shift 2 ;;
134 | --format-str) FORMAT_STR="$2"; shift 2 ;;
135 | --default-quality) QUALITY_STR="$2"; shift 2 ;;
136 | --media-cmd) MEDIA_CMD="$2"; shift 2 ;;
137 | --domain-whitelist) DOMAIN_WHITELIST="$2"; shift 2 ;;
138 | --run-in-foreground) RUN_IN_FOREGROUND=1; shift ;;
139 | -h|--help) usage; exit 0; ;;
140 | --) shift; break;;
141 | esac
142 | done
143 | }
144 | read_user_options "$@"
145 |
146 | function setup_sane_running_conditions {
147 | test -z "$DOWNLOAD_DIR" && error_and_exit \
148 | "Empty download-dir. Specify with the --download-dir option."
149 | mkdir -p "$DOWNLOAD_DIR" || error_and_exit \
150 | "Failed to create download-dir: $DOWNLOAD_DIR. "\
151 | "Try a different one with the --download-dir option."
152 |
153 | test -z "$DAEMON_DIR" && error_and_exit \
154 | "Empty daemon-dir. Specify with the --daemon-dir option."
155 | mkdir -p "$DAEMON_DIR" || error_and_exit \
156 | "Failed to create daemon-dir: $DAEMON_DIR. "\
157 | "Try a different one with the --daemon-dir option."
158 |
159 | test -f "$DAEMON_DIR"/"youtubed.dpid" && error_and_exit \
160 | "Found youtubed.dpid. Is youtubed already running? "\
161 | "Expert users may run multiple youtubed by specifying a unique "\
162 | "--daemon-dir (and --download-dir, unless you are sure you won't be "\
163 | "attempting to download the same video in more than one daemon."
164 |
165 | if [ -n "$DOMAIN_WHITELIST" ]; then
166 | command -v python3 &>/dev/null || error_and_exit \
167 | "Dependency not found: python3"
168 | test -f "$DOMAIN_WHITELIST" || error_and_exit \
169 | "Domain whitelist file $DOMAIN_WHITELIST not found."
170 | fi
171 |
172 | command -v youtube-dl &>/dev/null || error_and_exit \
173 | "Dependency not found: youtube-dl (typically in package youtube-dl)."
174 |
175 | command -v xclip &>/dev/null || error_and_exit \
176 | "Dependency not found: xclip (typically in package xclip)."
177 |
178 | command -v inotifywait &>/dev/null || error_and_exit \
179 | "Dependency not found: inotifywait (typically in package "\
180 | "inotify-tools)."
181 |
182 | mkfifo -m=600 "$DAEMON_DIR"/"$FIFO" || error_and_exit \
183 | "Failed to make fifo: ""$DAEMON_DIR"/"$FIFO."
184 | }
185 | setup_sane_running_conditions
186 |
187 | function write_immediate {
188 | local OUT="$1"
189 | local FULL_TEXT="$2"
190 | local SHORT_TEXT=${3:-$2}
191 | echo "$FULL_TEXT" >"$OUT"
192 | echo "$SHORT_TEXT" >>"$OUT"
193 | $EXEC_AFTER_WRITE
194 | }
195 |
196 | function write_immediate_temp {
197 | local TMP_I3TEXT=$(mktemp -p "$DAEMON_DIR" "tmp.XXXXXXXX.i3text")
198 | write_immediate "$TMP_I3TEXT" "$1"
199 |
200 | # auto remove after $2 seconds or echo name of temp for manual deletion
201 | if [ -n "$2" ]; then
202 | sleep "$2" && rm "$TMP_I3TEXT" && $EXEC_AFTER_WRITE &
203 | else
204 | echo "$TMP_I3TEXT"
205 | fi
206 | }
207 |
208 | function valid_URL {
209 | local URL=$1
210 | if [ -n "$DOMAIN_WHITELIST" ]; then
211 | python3 validate-url.py "$URL" "$DOMAIN_WHITELIST"
212 | return $?
213 | fi
214 | return 0
215 | }
216 |
217 |
218 | function download {
219 | local URL=${1:-$(xclip -out 2>/dev/null | tr -cd "[:print:]")}
220 | info "Set URL: $URL"
221 | if ! valid_URL "$URL"; then
222 | info "$URL: bad URL"
223 | write_immediate_temp "$(pango red "Bad URL: $URL")" "2"
224 | return
225 | fi
226 | local TMP_I3TEXT=$(write_immediate_temp \
227 | "$(pango yellow "$URL: getting filename")")
228 | local FILENAME=${2:-$(youtube-dl --get-filename --restrict-filenames \
229 | -f "$QUALITY_STR" -o "$ID_ONLY_FMT" \
230 | --merge-output-format "$MERGE_FORMAT_STR" "$URL")}
231 | local YT_EXIT=$?
232 | info "Set FILENAME: $FILENAME"
233 |
234 | rm "$TMP_I3TEXT" 2> /dev/null
235 | $EXEC_AFTER_WRITE
236 |
237 | # Don't do anything if the name lookup failed
238 | if [ "$YT_EXIT" -ne 0 ] || [ -z "$FILENAME" ]; then
239 | error "Couldn't download: $URL"
240 | write_immediate_temp "$(pango red "Couldn't download: $URL")" "2"
241 | return
242 | fi
243 |
244 | if [ -f "$DAEMON_DIR"/"$FILENAME".pid ]; then
245 | info "Found ""$DAEMON_DIR"/"$FILENAME"".pid, "\
246 | "attempting to cancel it first."
247 | cancel "$URL" "$FILENAME"
248 | fi
249 |
250 | youtube-dl --newline --restrict-filenames -f "$QUALITY_STR" \
251 | -o "$DOWNLOAD_DIR"/"$FILENAME" \
252 | --merge-output-format="$MERGE_OUTPUT_FORMAT" \
253 | "$URL" > \
254 | "$DAEMON_DIR"/"$FILENAME".progress &
255 | local PID=$!
256 | info "Spawned youtube-dl with PID: $PID"
257 | echo $PID > "$DAEMON_DIR"/"$FILENAME".pid
258 |
259 | function spawn_notifier {
260 | local PROGRESS=""
261 | local FILENAME="$1"
262 | local PID="$2"
263 | while [ -n "$(ps --no-headers -p $PID)" ]; do
264 | inotifywait -q -q -t 1 -e modify "$DAEMON_DIR"/"$FILENAME".progress
265 | PROGRESS=$(tail -n1 "$DAEMON_DIR"/"$FILENAME".progress 2> /dev/null)
266 | #TODO make prettier progress
267 | if [ -n "$PROGRESS" ]; then
268 | info "$FILENAME: $PROGRESS"
269 | write_immediate "$DAEMON_DIR"/"$FILENAME".i3text \
270 | "$(pango yellow "$FILENAME: $PROGRESS")" \
271 | "$(pango yellow "$FILENAME: $PROGRESS")"
272 | fi
273 | done
274 |
275 | local MERGE_REQUIRED=1
276 | local MERGE_NAME="$FILENAME"
277 | if [ ! -f "$DOWNLOAD_DIR"/"$FILENAME" ]; then
278 | info "$DOWNLOAD_DIR/$FILENAME not found"
279 | if [ -f "$DOWNLOAD_DIR"/"$FILENAME.$MERGE_OUTPUT_FORMAT" ]; then
280 | info "$DOWNLOAD_DIR/$FILENAME.$MERGE_OUTPUT_FORMAT found"
281 | info "$FILENAME: $MERGE_OUTPUT_FORMAT merge was required"
282 | MERGE_NAME="$FILENAME.$MERGE_OUTPUT_FORMAT"
283 | MERGE_REQUIRED=0
284 | else
285 | info "$DOWNLOAD_DIR/$FILENAME.$MERGE_OUTPUT_FORMAT not found"
286 | fi
287 |
288 | fi
289 |
290 | # If download was successful, rename and watch your video
291 | if [ -f "$DOWNLOAD_DIR"/"$MERGE_NAME" ]; then
292 | info "$MERGE_NAME: Download appears to be successful"
293 | info "$MERGE_NAME: getting FULL_FILENAME"
294 | write_immediate "$DAEMON_DIR"/"$FILENAME".i3text \
295 | "$(pango yellow "$FILENAME: Getting full filename")"
296 | # youtube-dl (or rather ffmpeg) will fail at the merge step if
297 | # filename is too long, hence the need for FULL_FILENAME
298 | FULL_FILENAME=$(youtube-dl --get-filename --restrict-filenames \
299 | -f "$QUALITY_STR" -o "$FORMAT_STR" "$URL")
300 | if [ $MERGE_REQUIRED -eq "0" ]; then
301 | FULL_FILENAME="$FULL_FILENAME.$MERGE_OUTPUT_FORMAT"
302 | fi
303 | mv "$DOWNLOAD_DIR"/{"$MERGE_NAME","$FULL_FILENAME"}
304 | write_immediate_temp "$(pango "#00FF00" "$FULL_FILENAME: done")" "2"
305 | if [ -n "$MEDIA_CMD" ]; then
306 | info "Notifier for FILENAME=$FILENAME, PID=$PID: "\
307 | "Found media command, executing"
308 | bash -c "$MEDIA_CMD ""$DOWNLOAD_DIR"/"$FULL_FILENAME"
309 | fi
310 | else
311 | info "$FILENAME: Download appears to be unsuccessful, "\
312 | "possibly because another thread cancelled it."
313 | fi
314 |
315 | info "Notifier for FILENAME=$FILENAME, PID=$PID: done notifying"
316 |
317 | rm "$DAEMON_DIR"/"$FILENAME".{pid,progress,i3text} 2> /dev/null
318 | $EXEC_AFTER_WRITE
319 | }
320 | spawn_notifier "$FILENAME" "$PID" &
321 | info "Spawned notifier for FILENAME=$FILENAME, PID=$PID, notifier PID=$!"
322 | }
323 |
324 | function cancel {
325 | local URL=${1:-$(xclip -out 2>/dev/null | tr -cd "[:print:]")}
326 | info "Set URL: $URL"
327 | if ! valid_URL "$URL"; then
328 | info "$URL: bad URL"
329 | write_immediate_temp "$(pango red "Bad URL: $URL")" "2"
330 | return
331 | fi
332 | local TMP_I3TEXT=$(write_immediate_temp "$(pango red "$URL: Canceling")")
333 | local FILENAME=${2:-$(youtube-dl --get-filename --restrict-filenames \
334 | -f "$QUALITY_STR" -o "$ID_ONLY_FMT" "$URL")}
335 | local YT_EXIT=$?
336 | info "Set FILENAME: $FILENAME"
337 |
338 | rm "$TMP_I3TEXT" 2> /dev/null
339 | $EXEC_AFTER_WRITE
340 |
341 | # Don't do anything if the name lookup failed
342 | if [ "$YT_EXIT" -ne 0 ] || [ -z "$FILENAME" ]; then
343 | error "Couldn't cancel: $URL"
344 | write_immediate_temp "$(pango red "Couldn't cancel: $URL")" "2"
345 | return
346 | fi
347 |
348 | if [ -f "$DAEMON_DIR"/"$FILENAME".pid ]; then
349 | kill $(cat "$DAEMON_DIR"/"$FILENAME".pid)
350 | fi
351 |
352 | # trigger inotifywait of other thread so it will kill itself
353 | echo " " > "$DAEMON_DIR"/"$FILENAME".progress
354 |
355 | # wait for other thread to kill itself
356 | if [ -f "$DAEMON_DIR"/"$FILENAME".pid ]; then
357 | inotifywait -q -q -t 5 -e delete_self "$DAEMON_DIR"/"$FILENAME".pid
358 | fi
359 |
360 | rm "$DAEMON_DIR"/"$FILENAME".{pid,progress,i3text} 2> /dev/null
361 | info "$FILENAME: Cancel complete"
362 | }
363 |
364 | function quality {
365 | QUALITY_STR="$1"
366 | echo "$QUALITY_STR">"$DAEMON_DIR"/"quality"
367 | write_immediate_temp "$(pango white "Quality set to $QUALITY_STR")" "2"
368 | info "Quality set to $QUALITY_STR"
369 | }
370 |
371 | function toggle_quality {
372 | case "$QUALITY_STR" in
373 | bestvideo+bestaudio) quality "bestaudio";;
374 | bestaudio) quality "worstvideo+worstaudio";;
375 | worstvideo+worstaudio) quality "worstaudio";;
376 | worstaudio) quality "bestvideo+bestaudio";;
377 | *) quality "bestvideo+bestaudio";;
378 | esac
379 | }
380 |
381 | function die {
382 | shopt -s nullglob
383 | for pidfile in "$DAEMON_DIR"/*.pid; do
384 | kill $(cat $pidfile)
385 | done
386 | pkill -SIGTERM -P $BASHPID
387 | rm "$DAEMON_DIR"/youtubed.dpid 2>/dev/null
388 | rm "$DAEMON_DIR"/"$FIFO" 2>/dev/null
389 | rm "$DAEMON_DIR"/"quality" 2>/dev/null
390 | rm "$DAEMON_DIR"/*.i3text 2>/dev/null
391 | rm "$DAEMON_DIR"/*.progress 2>/dev/null
392 | rm "$DAEMON_DIR"/*.pid 2>/dev/null
393 | $EXEC_AFTER_WRITE
394 | exit 0
395 | }
396 |
397 | function command_loop {
398 | trap download "$DOWNLOAD_SIGNAL"
399 | trap cancel "$CANCEL_SIGNAL"
400 | trap die SIGTERM SIGINT
401 |
402 | function dispatch_command {
403 | info "Received command: $1"
404 | local CMD=$(cut -d ' ' -f 1 <<< "$1")
405 | local ARGS=$(cut -d ' ' -sf 2- <<< "$1")
406 | case "$CMD" in
407 | download) download "$ARGS" &;;
408 | cancel) cancel "$ARGS" &;;
409 | toggle_quality) toggle_quality;;
410 | quality) quality "$ARGS";;
411 | die) die;;
412 | esac
413 | }
414 |
415 | while true; do
416 | read line <"$DAEMON_DIR"/"$FIFO" && dispatch_command $line &
417 | wait $!
418 | if [ -f "$DAEMON_DIR"/"quality" ]; then
419 | QUALITY_STR=$(cat "$DAEMON_DIR"/"quality")
420 | rm "$DAEMON_DIR"/"quality"
421 | fi
422 | done
423 | }
424 |
425 | echo "$$" > "$DAEMON_DIR"/youtubed.dpid
426 | $EXEC_AFTER_WRITE
427 | if [ "$RUN_IN_FOREGROUND" == "1" ]; then
428 | echo "$$" > "$DAEMON_DIR"/youtubed.dpid
429 | $EXEC_AFTER_WRITE
430 | command_loop
431 | else
432 | command_loop &> /dev/null &
433 | echo "$!" > "$DAEMON_DIR"/youtubed.dpid
434 | disown
435 | $EXEC_AFTER_WRITE
436 | fi
437 |
438 |
--------------------------------------------------------------------------------
/youtubed_controller:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2015 James Murphy
3 | # Licensed under the terms of the GNU GPL v2 only.
4 | #
5 | # Display script to read .i3text files in a directory.
6 | # Intended to be signal triggered by the youtubed script.
7 | #
8 | # Left click to trigger download of video from your clipboard.
9 | # Right click to cancel download of video from your clipboard.
10 | # If already running, left clicking will gracefully restart youtube-dl
11 | # and resume the download.
12 | # Handles multiple downloads simultaneously, just copy URL, click, copy URL,
13 | # click.
14 | #
15 | # Your ~/.config/i3blocks/config:
16 | #
17 | # [youtubed_controller]
18 | # command=$SCRIPT_DIR/youtubed_controller DAEMON_DIR
19 | # interval=once
20 | # signal=1
21 |
22 | DAEMON_DIR=${1:-"$XDG_RUNTIME_DIR"/"youtubed"}
23 | FIFO="$DAEMON_DIR"/"youtubed.fifo"
24 |
25 | shopt -s nullglob
26 |
27 | BAD_SETUP=1
28 | if [ -d "$DAEMON_DIR" ]; then
29 | PIDS=$(pgrep youtubed)
30 | dpid=$(cat "$DAEMON_DIR"/"youtubed.dpid" 2>/dev/null)
31 | for pid in $PIDS; do
32 | [ "$pid" == "$dpid" ] && BAD_SETUP=0
33 | done
34 | fi
35 |
36 | if [ "$BAD_SETUP" == "1" ]; then
37 | echo "No youtubed running in $DAEMON_DIR"
38 | echo "No youtubed running in $DAEMON_DIR"
39 | exit 0
40 | fi
41 |
42 | FULL_TEXT=""
43 | SHORT_TEXT=""
44 | SEPARATOR="|"
45 | STATUS_STR="[DL]"
46 |
47 | function send_command {
48 | if [ ! -p "$FIFO" ]; then
49 | echo "fifo not found: $FIFO"
50 | echo "fifo not found: $FIFO"
51 | exit 0
52 | fi
53 |
54 | echo "$1" > "$FIFO"
55 | }
56 |
57 | case "$BLOCK_BUTTON" in
58 | 1) send_command download ;;
59 | 3) send_command cancel ;;
60 | 4) send_command toggle_quality ;;
61 | esac
62 |
63 | function append_to_with_sep {
64 | local TEXT="$2"
65 | local TO_APPEND="$1"
66 | if [ -n "$TEXT" ]; then
67 | TEXT="$TEXT $SEPARATOR $TO_APPEND"
68 | else
69 | TEXT="$TO_APPEND"
70 | fi
71 | echo "$TEXT"
72 | }
73 |
74 | for f in "$DAEMON_DIR"/*.i3text; do
75 | TEXT=$(head -n 1 "$f")
76 | FULL_TEXT=$(append_to_with_sep "$TEXT" "$FULL_TEXT")
77 |
78 | STEXT=$(tail -n 1 "$f")
79 | SHORT_TEXT=$(append_to_with_sep "$STEXT" "$SHORT_TEXT")
80 | done
81 |
82 | FULL_TEXT=$(append_to_with_sep "$STATUS_STR" "$FULL_TEXT")
83 | SHORT_TEXT=$(append_to_with_sep "$STATUS_STR" "$SHORT_TEXT")
84 |
85 | echo "$FULL_TEXT"
86 | echo "$SHORT_TEXT"
87 | exit 0
88 |
--------------------------------------------------------------------------------
/youtubed_validate-url.py:
--------------------------------------------------------------------------------
1 | from sys import argv, exit
2 | from urllib.parse import urlparse
3 |
4 | if len(argv) != 3:
5 | exit(1)
6 |
7 | url = urlparse(argv[1])
8 | if url.scheme not in ["https", "http"]:
9 | exit(2)
10 |
11 | whitelist = []
12 | try:
13 | with open(argv[2]) as f:
14 | line = f.readline().strip()
15 | while line:
16 | if line[0] != "#":
17 | whitelist.append(line)
18 | line = f.readline().strip()
19 | except IOError:
20 | exit(3)
21 |
22 | if url.netloc not in whitelist:
23 | exit(4)
24 |
25 | exit(0)
26 |
--------------------------------------------------------------------------------