├── .gitignore
├── LICENSE
├── Procfile
├── README.md
├── bot.py
├── const.py
├── databasehandler.py
├── helper.py
├── image
├── demo.gif
├── icon.jpg
└── icon.png
├── logger.py
├── main.py
├── pafy
├── __init__.py
├── backend_internal.py
├── backend_shared.py
├── backend_youtube_dl.py
├── channel.py
├── g.py
├── jsinterp.py
├── pafy.py
├── playlist.py
└── util.py
├── requirements.txt
└── ytadllib.py
/.gitignore:
--------------------------------------------------------------------------------
1 | /venv/
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Procfile:
--------------------------------------------------------------------------------
1 | web: python main.py
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Youtube Audio Downloader Bot
2 |
3 | [](https://t.me/ytadlbot)
4 | 
5 |
6 | 
7 |
8 | A Telegram bot written in python that downloads music from YouTube and send it into the chat.
9 |
10 | ## Working
11 |
12 | The bot filter out all the YouTube urls from the received message and send the downloaded audio back to the user.
13 |
14 |
15 | 
16 |
17 |
18 | ## Requirements
19 | 1. [Pafy](https://pythonhosted.org/Pafy/)
20 | 2. [python-telegram-bot](https://python-telegram-bot.org/)
21 | 3. urlextract
22 | 4. requests
23 | 5. hashlib
24 | 6. [psycopg2](https://www.psycopg.org/)
25 |
26 | ## Deploy the bot
27 | Create a bot on telegram using [@botfather](t.me/botfather)
28 | Create an app on [heroku](https://dashboard.heroku.com/)
29 | Create a public channel on telegram
30 | Set environment variables in heroku app
31 | - BOT_TOKEN = your bot API token
32 | - HEROKU_APP_NAME = name of your heroku app
33 | - POLLING : if you're running this on your local machine, please set POLLING = True else leave this if you're deploying on heroku
34 | - AUDIO_DB : PostgreSQL DB link for audio management
35 | - USER_DB : PostgreSQL DB link for user management
36 | - OPEN_CHANNEL_USERNAME : Universal upload channel username, bot will upload files here and forward it to the user
37 |
38 | Deploy the bot [read here](https://devcenter.heroku.com/articles/getting-started-with-python)
39 |
40 | ## Contributions
41 |
42 | 1. [Icon by Alpha Designer](https://instagram.com/alpha_designer18)
43 |
44 | ## Future plans
45 |
46 | 1. Making a desktop application that can be used by everyone on their machine by providing their bot API token
47 |
--------------------------------------------------------------------------------
/bot.py:
--------------------------------------------------------------------------------
1 | from telegram import Update
2 | from telegram.ext import (CallbackContext, CommandHandler, Filters,
3 | MessageHandler, Updater)
4 |
5 | from const import (DBHANDLER, HEROKU_APP_NAME, LOGGER, OPEN_CHANNEL_USERNAME,
6 | POLLING, PORT, TOKEN)
7 | from databasehandler import add_to_db, check_in_db
8 | from helper import *
9 | from logger import log
10 | from ytadllib import (YTADL, FileDownloadError, FileSizeExceeded,
11 | UnableToDownload)
12 |
13 | OWNER_CHAT_ID = 0
14 |
15 |
16 | def start(update: Update, context: CallbackContext) -> None:
17 | """Command handler to send start message"""
18 | log(update, LOGGER)
19 | update.message.reply_text(
20 | f"Hello {update.effective_user.first_name}, I'm youtube audio downloader bot! "
21 | f"send me any youtube url and I'll download it for you!"
22 | )
23 |
24 |
25 | def help_bot(update: Update, context: CallbackContext) -> None:
26 | """Command handler for sending help message"""
27 | log(update, LOGGER)
28 | update.message.reply_text(
29 | f"Use telegram inline bot @vid to search youtube video and send me the link\n"
30 | f"Type '@vid Video name' in msg and click on appropriate youtube video\n"
31 | )
32 |
33 |
34 | def donate(update: Update, context: CallbackContext) -> None:
35 | """Command handler for sending help message"""
36 | log(update, LOGGER)
37 | update.message.reply_text(f"Visit @donateatamaka to donate me")
38 |
39 |
40 | def download_url(update: Update, context: CallbackContext, url: str) -> None:
41 | """Function to download and send single youtube url downloaded audio file. Sends appropriate error message to
42 | user"""
43 | log(update, LOGGER)
44 | audio = None
45 | title = ""
46 | artist = ""
47 | try:
48 | audio = YTADL(url, url_only=False)
49 | if "-" in audio.pafy_obj.title:
50 | title = audio.pafy_obj.title.split("-")[1].strip()
51 | artist = audio.pafy_obj.title.split("-")[0].strip()
52 |
53 | except ValueError:
54 | update.message.reply_text("Invalid URL")
55 |
56 | if audio:
57 | if OPEN_CHANNEL_USERNAME:
58 | db_status = check_in_db(audio.url, DBHANDLER)
59 | if db_status:
60 | try:
61 | context.bot.forward_message(
62 | chat_id=update.effective_chat.id,
63 | from_chat_id=OPEN_CHANNEL_USERNAME,
64 | message_id=db_status,
65 | )
66 | return
67 | except Exception as e:
68 | print("Msg not found on channel")
69 | try:
70 | audio.processor_url()
71 | except FileSizeExceeded:
72 | update.message.reply_text("File size limit exceeded")
73 | return
74 |
75 | try:
76 | audio.download()
77 | except FileDownloadError:
78 | update.message.reply_text("Unable to download file")
79 | return
80 | except UnableToDownload:
81 | update.message.reply_text("Unable to download file")
82 | return
83 |
84 | if OPEN_CHANNEL_USERNAME:
85 | try:
86 | msg = context.bot.send_audio(
87 | chat_id=OPEN_CHANNEL_USERNAME,
88 | audio=audio.audio_file,
89 | title=title,
90 | thumb=audio.thumbnail,
91 | performer=artist,
92 | duration=get_sec(audio.pafy_obj.duration),
93 | timeout=60,
94 | )
95 | context.bot.forward_message(
96 | chat_id=update.effective_chat.id,
97 | from_chat_id=OPEN_CHANNEL_USERNAME,
98 | message_id=msg.message_id,
99 | )
100 | try:
101 | add_to_db(audio.url, msg.message_id, DBHANDLER)
102 | except Exception as e:
103 | print(e)
104 | print("DB not working")
105 | except Exception as e:
106 | print(e)
107 | print("Upload to open channel failed")
108 | context.bot.send_audio(
109 | chat_id=update.message.chat_id,
110 | audio=audio.audio_file,
111 | title=title,
112 | thumb=audio.thumbnail,
113 | performer=artist,
114 | duration=get_sec(audio.pafy_obj.duration),
115 | timeout=60,
116 | )
117 | else:
118 | context.bot.send_audio(
119 | chat_id=update.message.chat_id,
120 | audio=audio.audio_file,
121 | title=title,
122 | thumb=audio.thumbnail,
123 | performer=artist,
124 | duration=get_sec(audio.pafy_obj.duration),
125 | timeout=60,
126 | )
127 |
128 |
129 | def extract_url_download(update: Update, context: CallbackContext) -> None:
130 | """Extract YouTube urls from the random text send to the bot and starts downloading and sending from url"""
131 | received_text = update.message.text
132 | yt_urls = get_links_from_text(received_text)
133 | yt_urls_msg = update.message.reply_text(
134 | pretty_url_string(yt_urls), disable_web_page_preview=True
135 | )
136 | if len(yt_urls) > 0:
137 | for url in yt_urls:
138 | if "list=" in url:
139 | download_playlist_url(update, context, url)
140 | else:
141 | try:
142 | download_url(update, context, url)
143 | except FileSizeExceeded:
144 | update.message.reply_text("File size exceeded")
145 | context.bot.delete_message(
146 | message_id=yt_urls_msg.message_id, chat_id=yt_urls_msg.chat_id
147 | )
148 |
149 |
150 | def download_playlist_url(
151 | update: Update, context: CallbackContext, pl_link: str
152 | ) -> None:
153 | """Extract YouTube urls from the playlist url send to the bot and starts downloading and sending each file"""
154 | pl_link = get_pl_link_from_url(pl_link)
155 | yt_urls = get_yt_links_from_pl(pl_link)
156 | yt_urls_msg = update.message.reply_text(
157 | pretty_url_string(yt_urls), disable_web_page_preview=True
158 | )
159 | if len(yt_urls) > 0:
160 | for url in yt_urls:
161 | download_url(update, context, url)
162 | context.bot.delete_message(
163 | message_id=yt_urls_msg.message_id, chat_id=yt_urls_msg.chat_id
164 | )
165 |
166 |
167 | def main() -> None:
168 | updater = None
169 | try:
170 | updater = Updater(TOKEN)
171 | except Exception:
172 | print("Token is incorrect")
173 | exit(1)
174 | # Command handlers
175 | updater.dispatcher.add_handler(
176 | CommandHandler("start", start, run_async=True))
177 | updater.dispatcher.add_handler(
178 | CommandHandler("help", help_bot, run_async=True))
179 | updater.dispatcher.add_handler(
180 | CommandHandler("donate", donate, run_async=True))
181 | # Message handler
182 | updater.dispatcher.add_handler(
183 | MessageHandler(
184 | Filters.text & ~Filters.command, extract_url_download, run_async=True
185 | )
186 | )
187 | # Polling method to test on local machine
188 | if POLLING:
189 | updater.start_polling()
190 | else:
191 | # webhook method for heroku
192 | updater.start_webhook(
193 | listen="0.0.0.0",
194 | port=PORT,
195 | url_path=TOKEN,
196 | webhook_url="https://{}.herokuapp.com/{}".format(
197 | HEROKU_APP_NAME, TOKEN),
198 | )
199 | updater.idle()
200 | LOGGER.close()
201 | DBHANDLER.close()
202 |
203 |
204 | if __name__ == "__main__":
205 | main()
206 |
--------------------------------------------------------------------------------
/const.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | from databasehandler import start_dbhandler
4 | from logger import start_logger
5 |
6 | TOKEN = os.environ.get("BOT_TOKEN", None)
7 | PORT = int(os.environ.get("PORT", "8443"))
8 | HEROKU_APP_NAME = os.environ.get("HEROKU_APP_NAME", None)
9 | PASS_HASH = os.environ.get("PASS_HASH", None)
10 | # only enable pooling for testing on local machine, Heroku will only work on webhook method
11 | POLLING = os.environ.get("POOLING", False)
12 | OPEN_CHANNEL_USERNAME = os.environ.get("OPEN_CHANNEL_USERNAME", None)
13 | # Database connection urls
14 | USER_DB = os.environ.get("USER_DB", None)
15 | AUDIO_DB = os.environ.get("AUDIO_DB", None)
16 |
17 | # start the logger
18 | LOGGER = start_logger(user_db=USER_DB)
19 | DBHANDLER = start_dbhandler(audio_db=AUDIO_DB)
20 |
--------------------------------------------------------------------------------
/databasehandler.py:
--------------------------------------------------------------------------------
1 | import urllib.parse as urlparse
2 |
3 | import psycopg2
4 |
5 | STATUS = False
6 |
7 |
8 | def start_dbhandler(audio_db: str):
9 | global STATUS
10 | try:
11 | result = urlparse.urlparse(audio_db)
12 | username = result.username
13 | password = result.password
14 | database = result.path[1:]
15 | hostname = result.hostname
16 | port = result.port
17 | except Exception as e:
18 | print(e)
19 | print("DB url is not valid")
20 | STATUS = False
21 | return None
22 | try:
23 | conn = psycopg2.connect(
24 | database=database,
25 | user=username,
26 | password=password,
27 | host=hostname,
28 | port=port,
29 | )
30 | STATUS = True
31 | except Exception as e:
32 | print(e)
33 | print("Connection establishment failed")
34 | STATUS = False
35 | return None
36 | if conn:
37 | cur = conn.cursor()
38 | try:
39 | cur.execute(
40 | "CREATE TABLE IF NOT EXISTS Audios("
41 | "yt_link TEXT PRIMARY KEY,"
42 | "msg_id INTEGER)"
43 | )
44 | except Exception as e:
45 | print(e)
46 | print("Table creation failed")
47 | STATUS = False
48 | return None
49 | conn.commit()
50 | return conn
51 | return False
52 |
53 |
54 | def check_in_db(url, conn) -> int or None:
55 | if STATUS:
56 | cur = conn.cursor()
57 | row = []
58 | try:
59 | cur.execute(
60 | "SELECT msg_id from Audios where yt_link = {}".format(
61 | "'" + url + "'")
62 | )
63 | row = cur.fetchall()
64 | except Exception as e:
65 | print(e)
66 | print("Can't run find command!")
67 |
68 | if len(row) > 0:
69 | return int(row[0][0])
70 | else:
71 | return None
72 | return None
73 |
74 |
75 | def add_to_db(url: str, msg_id: int, conn):
76 | if STATUS:
77 | cur = conn.cursor()
78 | cur.execute(
79 | "INSERT INTO Audios(yt_link,msg_id)"
80 | "VALUES ({},{}) ON CONFLICT UPDATE msg_id = {}".format(
81 | "'" + url + "'", msg_id, msg_id
82 | )
83 | )
84 | conn.commit()
85 | else:
86 | print("DB is not active cannot add the entry!")
87 |
--------------------------------------------------------------------------------
/helper.py:
--------------------------------------------------------------------------------
1 | import re
2 | from urllib import error, request
3 | from urllib.parse import parse_qs, urlparse
4 |
5 | import requests
6 | from urlextract import URLExtract
7 |
8 |
9 | def get_vid_id(url: str) -> str:
10 | query = urlparse(url)
11 | if query.hostname == "youtu.be":
12 | return query.path[1:]
13 | if query.hostname in ("www.youtube.com", "youtube.com"):
14 | if query.path == "/watch":
15 | p = parse_qs(query.query)
16 | return p["v"][0]
17 | if query.path[:7] == "/embed/":
18 | return query.path.split("/")[2]
19 | if query.path[:3] == "/v/":
20 | return query.path.split("/")[2]
21 | return ""
22 |
23 |
24 | def is_yt_url(video_id: str) -> bool:
25 | checker_url = "https://www.youtube.com/oembed?url="
26 | video_url = checker_url + video_id
27 | req = requests.get(video_url)
28 | return req.status_code == 200
29 |
30 |
31 | def is_pl_url(url: str):
32 | if "list=" in url and "youtu" in url.lower():
33 | return True
34 | return False
35 |
36 |
37 | def get_links_from_text(text: str) -> list:
38 | urls = URLExtract().find_urls(text)
39 | yt_urls = []
40 | for url in urls:
41 | if is_yt_url(url) or is_pl_url(url):
42 | yt_urls.append(url)
43 | return yt_urls
44 |
45 |
46 | def pretty_url_string(urls: list) -> str:
47 | pretty_url = ""
48 | if len(urls) == 0:
49 | return "No valid url found"
50 | elif len(urls) > 1:
51 | pretty_url += f"Received {len(urls)} links\n"
52 | i = 1
53 | for url in urls:
54 | pretty_url += f"{i}. {url}\n"
55 | i += 1
56 | pretty_url += "Downloading ..."
57 |
58 | return pretty_url[:4096]
59 |
60 |
61 | def is_yt_playlist(url: str) -> bool:
62 | return url.startswith("https://www.youtube.com/playlist?list=")
63 |
64 |
65 | def get_pl_link_from_url(url: str) -> str:
66 | pl_id = ""
67 | if "watch?v=" in url and "&" in url:
68 | idx = url.find("list=")
69 | pl_id = url[idx + 5:]
70 | elif "list=" in url:
71 | eq_idx = url.index("=") + 1
72 | pl_id = url[eq_idx:]
73 | if "&" in url:
74 | amp = url.index("&")
75 | pl_id = url[eq_idx:amp]
76 | else:
77 | return ""
78 | if "&" in pl_id:
79 | idx = pl_id.find("&")
80 | pl_id = pl_id[:idx]
81 | return "https://www.youtube.com/playlist?list=" + pl_id
82 |
83 |
84 | def get_yt_links_from_pl(url: str) -> list:
85 | page = None
86 | urls = []
87 | try:
88 | page = request.urlopen(url).read()
89 | page = str(page)
90 | except error.URLError as e:
91 | print(e.reason)
92 | idx = url.find("list=")
93 | playlist_id = url[idx + 5:]
94 | if page:
95 | vid_url_pat = re.compile(r"watch\?v=\S+?list=" + playlist_id)
96 | vid_url_matches = list(set(re.findall(vid_url_pat, page)))
97 | if vid_url_matches:
98 | for vid_url in vid_url_matches:
99 | urls.append("https://www.youtube.com/" + vid_url[:19])
100 | return urls
101 |
102 |
103 | def get_sec(time_str) -> int:
104 | """Get Seconds from time."""
105 | h, m, s = time_str.split(":")
106 | return int(h) * 3600 + int(m) * 60 + int(s)
107 |
--------------------------------------------------------------------------------
/image/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atamakahere-git/ytadlbot/2005001a6738f6be094dc9105e15e2afc755f9ba/image/demo.gif
--------------------------------------------------------------------------------
/image/icon.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atamakahere-git/ytadlbot/2005001a6738f6be094dc9105e15e2afc755f9ba/image/icon.jpg
--------------------------------------------------------------------------------
/image/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atamakahere-git/ytadlbot/2005001a6738f6be094dc9105e15e2afc755f9ba/image/icon.png
--------------------------------------------------------------------------------
/logger.py:
--------------------------------------------------------------------------------
1 | import urllib.parse as urlparse
2 |
3 | import psycopg2
4 | from telegram import Update
5 |
6 | STATUS = False
7 | SQL_BASE_CMD = """ INSERT INTO Users(chat_id,first_name,last_name,username,usage)
8 | VALUES({},{},{},{},{}) """
9 | SQL_UPDATE_CMD = """UPDATE Users
10 | SET usage = usage + 1
11 | WHERE chat_id = {};"""
12 |
13 |
14 | def start_logger(user_db: str):
15 | global STATUS
16 | result = urlparse.urlparse(user_db)
17 | username = result.username
18 | password = result.password
19 | database = result.path[1:]
20 | hostname = result.hostname
21 | port = result.port
22 | try:
23 | conn = psycopg2.connect(
24 | database=database,
25 | user=username,
26 | password=password,
27 | host=hostname,
28 | port=port,
29 | )
30 | except Exception as e:
31 | print(e)
32 | print("Connection establishment failed")
33 | STATUS = False
34 | return None
35 | if conn:
36 | STATUS = True
37 | cur = conn.cursor()
38 | try:
39 | cur.execute(
40 | "CREATE TABLE IF NOT EXISTS Users("
41 | "chat_id INTEGER PRIMARY KEY,"
42 | "first_name TEXT,"
43 | "last_name TEXT,"
44 | "username TEXT,"
45 | "usage INTEGER DEFAULT 0)"
46 | )
47 | except Exception as e:
48 | print(e)
49 | print("Table creation failed")
50 | STATUS = False
51 | return None
52 | conn.commit()
53 | return conn
54 | return None
55 |
56 |
57 | def log(update: Update, conn):
58 | global STATUS
59 | if not STATUS:
60 | print("DB is not connected, cannot log!")
61 | return
62 | global SQL_BASE_CMD, SQL_UPDATE_CMD
63 | chat_id = update.effective_chat.id
64 | first_name = update.effective_chat.first_name
65 | last_name = update.effective_chat.last_name
66 | username = update.effective_chat.username
67 | cur = conn.cursor()
68 | try:
69 | cur.execute(
70 | SQL_BASE_CMD.format(
71 | chat_id,
72 | "'" + first_name + "'",
73 | "'" + last_name + "'",
74 | "'" + username + "'",
75 | 0,
76 | )
77 | )
78 | except Exception:
79 | try:
80 | cur.execute(SQL_UPDATE_CMD.format(chat_id))
81 | except Exception as e:
82 | print(e)
83 | print(f"Update failed for {chat_id}")
84 |
85 | conn.commit()
86 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import bot
2 | from const import HEROKU_APP_NAME, POLLING, PORT, TOKEN
3 |
4 | if __name__ == "__main__":
5 | if TOKEN is None:
6 | print("Token not found")
7 | elif PORT is None and not POLLING:
8 | print("Port not found")
9 | elif HEROKU_APP_NAME is None and not POLLING:
10 | print("App name not found")
11 | else:
12 | bot.main()
13 |
--------------------------------------------------------------------------------
/pafy/__init__.py:
--------------------------------------------------------------------------------
1 | __version__ = "0.5.5"
2 | __author__ = "np1"
3 | __license__ = "LGPLv3"
4 |
5 |
6 | # External api
7 | from .pafy import new
8 | from .pafy import set_api_key
9 | from .pafy import load_cache, dump_cache
10 | from .pafy import get_categoryname
11 | from .pafy import backend
12 | from .util import GdataError, call_gdata
13 | from .playlist import get_playlist, get_playlist2
14 | from .channel import get_channel
15 |
--------------------------------------------------------------------------------
/pafy/backend_internal.py:
--------------------------------------------------------------------------------
1 | import os
2 | import hashlib
3 | import tempfile
4 | import json
5 | import re
6 | import sys
7 | import time
8 | import logging
9 | from xml.etree import ElementTree
10 |
11 | if sys.version_info[:2] >= (3, 0):
12 | # pylint: disable=E0611,F0401,I0011
13 | from urllib.parse import parse_qs, unquote_plus
14 | uni, pyver = str, 3
15 |
16 | else:
17 | from urllib import unquote_plus
18 | from urlparse import parse_qs
19 | uni, pyver = unicode, 2
20 |
21 | early_py_version = sys.version_info[:2] < (2, 7)
22 |
23 | from . import g
24 | from .pafy import fetch_decode, dbg, get_categoryname
25 | from .backend_shared import BasePafy, BaseStream
26 | from .jsinterp import JSInterpreter
27 |
28 |
29 | funcmap = {}
30 |
31 |
32 | class InternPafy(BasePafy):
33 | def __init__(self, *args, **kwargs):
34 | self.sm = []
35 | self.asm = []
36 | self.dash = []
37 | self.js_url = None # if js_url is set then has new stream map
38 | self._dashurl = None
39 | self.age_ver = False
40 | self._formats = None
41 | self.ciphertag = None # used by Stream class in url property def
42 | super(InternPafy, self).__init__(*args, **kwargs)
43 |
44 |
45 | def _fetch_basic(self):
46 | """ Fetch basic data and streams. """
47 | if self._have_basic:
48 | return
49 |
50 | allinfo = get_video_info(self.videoid, self.callback)
51 |
52 | if self.callback:
53 | self.callback("Fetched video info")
54 |
55 | def _get_lst(key, default="unknown", dic=allinfo):
56 | """ Dict get function, returns first index. """
57 | retval = dic.get(key, default)
58 | return retval[0] if retval != default else default
59 |
60 | self._title = _get_lst('title')
61 | self._dashurl = _get_lst('dashmpd')
62 | self._author = _get_lst('author')
63 | self._rating = float(_get_lst('avg_rating', 0.0))
64 | self._length = int(_get_lst('length_seconds', 0))
65 | self._viewcount = int(_get_lst('view_count', 0))
66 | self._thumb = unquote_plus(_get_lst('thumbnail_url', ""))
67 | self._formats = [x.split("/") for x in _get_lst('fmt_list').split(",")]
68 | self._keywords = _get_lst('keywords', "").split(',')
69 | self._bigthumb = _get_lst('iurlsd', "")
70 | self._bigthumbhd = _get_lst('iurlsdmaxres', "")
71 | self.ciphertag = _get_lst("use_cipher_signature") == "True"
72 | self.sm = _extract_smap(g.UEFSM, allinfo, True)
73 | self.asm = _extract_smap(g.AF, allinfo, True)
74 | dbg("extracted stream maps")
75 |
76 | sm_ciphertag = "s" in self.sm[0]
77 |
78 | if self.ciphertag != sm_ciphertag:
79 | dbg("ciphertag mismatch")
80 | self.ciphertag = not self.ciphertag
81 |
82 | watch_url = g.urls['watchv'] % self.videoid
83 | if self.callback:
84 | self.callback("Fetching watch page")
85 | watchinfo = fetch_decode(watch_url) # unicode
86 | dbg("Fetched watch page")
87 | if self.callback:
88 | self.callback("Fetched watch page")
89 | self.age_ver = re.search(r'player-age-gate-content">', watchinfo) is not None
90 |
91 | if self.ciphertag:
92 | dbg("Encrypted signature detected.")
93 |
94 | if not self.age_ver:
95 | smaps, js_url, mainfunc = get_js_sm(watchinfo, self.callback)
96 | funcmap[js_url] = mainfunc
97 | self.sm, self.asm = smaps
98 | self.js_url = js_url
99 | dashsig = re.search(r"/s/([\w\.]+)", self._dashurl).group(1)
100 | dbg("decrypting dash sig")
101 | goodsig = _decodesig(dashsig, js_url, self.callback)
102 | self._dashurl = re.sub(r"/s/[\w\.]+",
103 | "/signature/%s" % goodsig, self._dashurl)
104 |
105 | else:
106 | s = re.search(r"/s/([\w\.]+)", self._dashurl).group(1)
107 | s = s[2:63] + s[82] + s[64:82] + s[63]
108 | self._dashurl = re.sub(r"/s/[\w\.]+",
109 | "/signature/%s" % s, self._dashurl)
110 |
111 | if self._dashurl != 'unknown':
112 | self.dash = _extract_dash(self._dashurl)
113 | self._have_basic = 1
114 | self._process_streams()
115 | self.expiry = time.time() + g.lifespan
116 |
117 |
118 | def _fetch_gdata(self):
119 | """ Extract gdata values, fetch gdata if necessary. """
120 | if self._have_gdata:
121 | return
122 |
123 | item = self._get_video_gdata(self.videoid)['items'][0]
124 | snippet = item['snippet']
125 | self._published = uni(snippet['publishedAt'])
126 | self._description = uni(snippet["description"])
127 | self._category = get_categoryname(snippet['categoryId'])
128 | # TODO: Make sure actual usename is not available through the api
129 | self._username = uni(snippet['channelTitle'])
130 | statistics = item["statistics"]
131 | self._likes = int(statistics["likeCount"])
132 | self._dislikes = int(statistics["dislikeCount"])
133 | self._have_gdata = 1
134 |
135 |
136 | def _process_streams(self):
137 | """ Create Stream object lists from internal stream maps. """
138 | if not self._have_basic:
139 | self._fetch_basic()
140 |
141 | streams = [InternStream(z, self) for z in self.sm]
142 | streams = [x for x in streams if x.itag in g.itags]
143 | adpt_streams = [InternStream(z, self) for z in self.asm]
144 | adpt_streams = [x for x in adpt_streams if x.itag in g.itags]
145 | dash_streams = [InternStream(z, self) for z in self.dash]
146 | dash_streams = [x for x in dash_streams if x.itag in g.itags]
147 | audiostreams = [x for x in adpt_streams if x.bitrate]
148 | videostreams = [x for x in adpt_streams if not x.bitrate]
149 | dash_itags = [x.itag for x in dash_streams]
150 | audiostreams = [x for x in audiostreams if x.itag not in dash_itags]
151 | videostreams = [x for x in videostreams if x.itag not in dash_itags]
152 | audiostreams += [x for x in dash_streams if x.mediatype == "audio"]
153 | videostreams += [x for x in dash_streams if x.mediatype != "audio"]
154 | audiostreams = sorted(audiostreams, key=lambda x: x.rawbitrate,
155 | reverse=True)
156 | videostreams = sorted(videostreams, key=lambda x: x.dimensions,
157 | reverse=True)
158 | m4astreams = [x for x in audiostreams if x.extension == "m4a"]
159 | oggstreams = [x for x in audiostreams if x.extension == "ogg"]
160 | self._streams = streams
161 | self._audiostreams = audiostreams
162 | self._videostreams = videostreams
163 | self._m4astreams, self._oggstreams = m4astreams, oggstreams
164 | self._allstreams = streams + videostreams + audiostreams
165 |
166 |
167 | class InternStream(BaseStream):
168 | def __init__(self, sm, parent):
169 | super(InternStream, self).__init__(parent)
170 |
171 | self._itag = sm['itag']
172 | # is_dash = "width" in sm and "height" in sm
173 | is_dash = "dash" in sm
174 |
175 | if self._itag not in g.itags:
176 | logging.warning("Unknown itag: %s", self._itag)
177 | return
178 |
179 | self._mediatype = g.itags[self.itag][2]
180 | self._threed = 'stereo3d' in sm and sm['stereo3d'] == '1'
181 |
182 | if is_dash:
183 | if sm['width'] != "None": # dash video
184 | self._resolution = "%sx%s" % (sm['width'], sm['height'])
185 | self._quality = self._resolution
186 | self._dimensions = (int(sm['width']), int(sm['height']))
187 |
188 | else: # dash audio
189 | self._resolution = "0x0"
190 | self._dimensions = (0, 0)
191 | self._rawbitrate = int(sm['bitrate'])
192 | # self._bitrate = uni(int(sm['bitrate']) // 1024) + "k"
193 | self._bitrate = g.itags[self.itag][0]
194 | self._quality = self._bitrate
195 |
196 | self._fsize = int(sm['size'] or 0)
197 | # self._bitrate = sm['bitrate']
198 | # self._rawbitrate = uni(int(self._bitrate) // 1024) + "k"
199 |
200 | else: # not dash
201 | self._resolution = g.itags[self.itag][0]
202 | self._dimensions = tuple(self.resolution.split("-")[0].split("x"))
203 | self._dimensions = tuple([int(x) if x.isdigit() else x for x in
204 | self._dimensions])
205 | self._quality = self.resolution
206 |
207 | self._extension = g.itags[self.itag][1]
208 | self._title = parent.title
209 | self.encrypted = 's' in sm
210 | self._parent = parent
211 | self._filename = self.generate_filename()
212 | self._notes = g.itags[self.itag][3]
213 | self._rawurl = sm['url']
214 | self._sig = sm['s'] if self.encrypted else sm.get("sig")
215 | self._active = False
216 |
217 | if self.mediatype == "audio" and not is_dash:
218 | self._dimensions = (0, 0)
219 | self._bitrate = self.resolution
220 | self._quality = self.bitrate
221 | self._resolution = "0x0"
222 | self._rawbitrate = int(sm["bitrate"])
223 |
224 | @property
225 | def url(self):
226 | """ Return the url, decrypt if required. """
227 | if not self._url:
228 |
229 | if self._parent.age_ver:
230 |
231 | if self._sig:
232 | s = self._sig
233 | self._sig = s[2:63] + s[82] + s[64:82] + s[63]
234 |
235 | elif self.encrypted:
236 | self._sig = _decodesig(self._sig, self._parent.js_url,
237 | self._parent.callback)
238 |
239 | self._url = _make_url(self._rawurl, self._sig)
240 |
241 | return self._url
242 |
243 |
244 | def parseqs(data):
245 | """ parse_qs, return unicode. """
246 | if type(data) == uni:
247 | return parse_qs(data)
248 |
249 | elif pyver == 3:
250 | data = data.decode("utf8")
251 | data = parse_qs(data)
252 |
253 | else:
254 | data = parse_qs(data)
255 | out = {}
256 |
257 | for k, v in data.items():
258 | k = k.decode("utf8")
259 | out[k] = [x.decode("utf8") for x in v]
260 | data = out
261 |
262 | return data
263 |
264 |
265 | def get_video_info(video_id, callback, newurl=None):
266 | """ Return info for video_id. Returns dict. """
267 | # TODO: see if there is a way to avoid retrieving the embed page
268 | # just for this, or to use it for more. This was coppied from
269 | # youtube-dl.
270 | embed_webpage = fetch_decode(g.urls['embed'])
271 | sts = re.search(r'sts"\s*:\s*(\d+)', embed_webpage).group(1)
272 |
273 | url = g.urls['vidinfo'] % (video_id, video_id, sts)
274 | url = newurl if newurl else url
275 | info = fetch_decode(url) # bytes
276 | info = parseqs(info) # unicode dict
277 | dbg("Fetched video info%s", " (age ver)" if newurl else "")
278 |
279 | if info['status'][0] == "fail":
280 | reason = info['reason'][0] or "Bad video argument"
281 | raise IOError("Youtube says: %s [%s]" % (reason, video_id))
282 |
283 | return info
284 |
285 |
286 | def _extract_smap(map_name, dic, zero_idx=True):
287 | """ Extract stream map, returns list of dicts. """
288 | if map_name in dic:
289 | smap = dic.get(map_name)
290 | smap = smap[0] if zero_idx else smap
291 | smap = smap.split(",")
292 | smap = [parseqs(x) for x in smap]
293 | return [dict((k, v[0]) for k, v in x.items()) for x in smap]
294 |
295 | return []
296 |
297 |
298 | def _extract_dash(dashurl):
299 | """ Download dash url and extract some data. """
300 | # pylint: disable = R0914
301 | dbg("Fetching dash page")
302 | dashdata = fetch_decode(dashurl)
303 | dbg("DASH list fetched")
304 | ns = "{urn:mpeg:DASH:schema:MPD:2011}"
305 | ytns = "{http://youtube.com/yt/2012/10/10}"
306 | tree = ElementTree.fromstring(dashdata)
307 | tlist = tree.findall(".//%sRepresentation" % ns)
308 | dashmap = []
309 |
310 | for x in tlist:
311 | baseurl = x.find("%sBaseURL" % ns)
312 | url = baseurl.text
313 | size = baseurl.get("%scontentLength" % ytns)
314 | bitrate = x.get("bandwidth")
315 | itag = uni(x.get("id"))
316 | width = uni(x.get("width"))
317 | height = uni(x.get("height"))
318 | dashmap.append(dict(bitrate=bitrate,
319 | dash=True,
320 | itag=itag,
321 | width=width,
322 | height=height,
323 | url=url,
324 | size=size))
325 | return dashmap
326 |
327 |
328 | def _get_mainfunc_from_js(js):
329 | """ Return main signature decryption function from javascript as dict. """
330 | dbg("Scanning js for main function.")
331 | m = re.search(r'\.sig\|\|([a-zA-Z0-9$]+)\(', js)
332 | funcname = m.group(1)
333 | dbg("Found main function: %s", funcname)
334 | jsi = JSInterpreter(js)
335 | return jsi.extract_function(funcname)
336 |
337 |
338 | def _decodesig(sig, js_url, callback):
339 | """ Return decrypted sig given an encrypted sig and js_url key. """
340 | # lookup main function in funcmap dict
341 | mainfunction = funcmap[js_url]
342 |
343 | # fill in function argument with signature
344 | if callback:
345 | callback("Decrypting signature")
346 | solved = mainfunction([sig])
347 | dbg("Decrypted sig = %s...", solved[:30])
348 | if callback:
349 | callback("Decrypted signature")
350 | return solved
351 |
352 |
353 | def fetch_cached(url, callback, encoding=None, dbg_ref="", file_prefix=""):
354 | """ Fetch url - from tmpdir if already retrieved. """
355 | tmpdir = os.path.join(tempfile.gettempdir(), "pafy")
356 |
357 | if not os.path.exists(tmpdir):
358 | os.makedirs(tmpdir)
359 |
360 | url_md5 = hashlib.md5(url.encode("utf8")).hexdigest()
361 | cached_filename = os.path.join(tmpdir, file_prefix + url_md5)
362 |
363 | if os.path.exists(cached_filename):
364 | dbg("fetched %s from cache", dbg_ref)
365 |
366 | with open(cached_filename) as f:
367 | retval = f.read()
368 |
369 | return retval
370 |
371 | else:
372 | data = fetch_decode(url, "utf8") # unicode
373 | dbg("Fetched %s", dbg_ref)
374 | if callback:
375 | callback("Fetched %s" % dbg_ref)
376 |
377 | with open(cached_filename, "w") as f:
378 | f.write(data)
379 |
380 | # prune files after write
381 | prune_files(tmpdir, file_prefix)
382 | return data
383 |
384 |
385 | def prune_files(path, prefix="", age_max=3600 * 24 * 14, count_max=4):
386 | """ Remove oldest files from path that start with prefix.
387 |
388 | remove files older than age_max, leave maximum of count_max files.
389 | """
390 | tempfiles = []
391 |
392 | if not os.path.isdir(path):
393 | return
394 |
395 | for f in os.listdir(path):
396 | filepath = os.path.join(path, f)
397 |
398 | if os.path.isfile(filepath) and f.startswith(prefix):
399 | age = time.time() - os.path.getmtime(filepath)
400 |
401 | if age > age_max:
402 | os.unlink(filepath)
403 |
404 | else:
405 | tempfiles.append((filepath, age))
406 |
407 | tempfiles = sorted(tempfiles, key=lambda x: x[1], reverse=True)
408 |
409 | for f in tempfiles[:-count_max]:
410 | os.unlink(f[0])
411 |
412 |
413 | def get_js_sm(watchinfo, callback):
414 | """ Fetch watchinfo page and extract stream map and js funcs if not known.
415 |
416 | This function is needed by videos with encrypted signatures.
417 | If the js url referred to in the watchv page is not a key in funcmap,
418 | the javascript is fetched and functions extracted.
419 |
420 | Returns streammap (list of dicts), js url (str) and funcs (dict)
421 |
422 | """
423 | m = re.search(g.jsplayer, watchinfo)
424 | myjson = json.loads(m.group(1))
425 | stream_info = myjson['args']
426 | sm = _extract_smap(g.UEFSM, stream_info, False)
427 | asm = _extract_smap(g.AF, stream_info, False)
428 | js_url = myjson['assets']['js']
429 | js_url = "https:" + js_url if js_url.startswith("//") else js_url
430 | mainfunc = funcmap.get(js_url)
431 |
432 | if not mainfunc:
433 | dbg("Fetching javascript")
434 | if callback:
435 | callback("Fetching javascript")
436 | javascript = fetch_cached(js_url, callback, encoding="utf8",
437 | dbg_ref="javascript", file_prefix="js-")
438 | mainfunc = _get_mainfunc_from_js(javascript)
439 |
440 | elif mainfunc:
441 | dbg("Using functions in memory extracted from %s", js_url)
442 | dbg("Mem contains %s js func sets", len(funcmap))
443 |
444 | return (sm, asm), js_url, mainfunc
445 |
446 |
447 | def _make_url(raw, sig, quick=True):
448 | """ Return usable url. Set quick=False to disable ratebypass override. """
449 | if quick and "ratebypass=" not in raw:
450 | raw += "&ratebypass=yes"
451 |
452 | if "signature=" not in raw:
453 |
454 | if sig is None:
455 | raise IOError("Error retrieving url")
456 |
457 | raw += "&signature=" + sig
458 |
459 | return raw
460 |
--------------------------------------------------------------------------------
/pafy/backend_shared.py:
--------------------------------------------------------------------------------
1 | import os
2 | import re
3 | import sys
4 | import time
5 | import logging
6 | import subprocess
7 |
8 | if sys.version_info[:2] >= (3, 0):
9 | # pylint: disable=E0611,F0401,I0011
10 | from urllib.request import urlopen, build_opener
11 | from urllib.error import HTTPError, URLError
12 | from urllib.parse import parse_qs, urlparse
13 | uni, pyver = str, 3
14 |
15 | else:
16 | from urllib2 import urlopen, build_opener, HTTPError, URLError
17 | from urlparse import parse_qs, urlparse
18 | uni, pyver = unicode, 2
19 |
20 | early_py_version = sys.version_info[:2] < (2, 7)
21 |
22 | from . import __version__, g
23 | from .pafy import call_gdata
24 | from .playlist import get_playlist2
25 | from .util import xenc
26 |
27 | dbg = logging.debug
28 |
29 |
30 | def extract_video_id(url):
31 | """ Extract the video id from a url, return video id as str. """
32 | idregx = re.compile(r'[\w-]{11}$')
33 | url = str(url).strip()
34 |
35 | if idregx.match(url):
36 | return url # ID of video
37 |
38 | if '://' not in url:
39 | url = '//' + url
40 | parsedurl = urlparse(url)
41 | if parsedurl.netloc in ('youtube.com', 'www.youtube.com', 'm.youtube.com', 'gaming.youtube.com'):
42 | query = parse_qs(parsedurl.query)
43 | if 'v' in query and idregx.match(query['v'][0]):
44 | return query['v'][0]
45 | elif parsedurl.netloc in ('youtu.be', 'www.youtu.be'):
46 | vidid = parsedurl.path.split('/')[-1] if parsedurl.path else ''
47 | if idregx.match(vidid):
48 | return vidid
49 |
50 | err = "Need 11 character video id or the URL of the video. Got %s"
51 | raise ValueError(err % url)
52 |
53 |
54 | class BasePafy(object):
55 |
56 | """ Class to represent a YouTube video. """
57 |
58 | def __init__(self, video_url, basic=True, gdata=False,
59 | size=False, callback=None, ydl_opts=None):
60 | """ Set initial values. """
61 | self.version = __version__
62 | self.videoid = extract_video_id(video_url)
63 | self.watchv_url = g.urls['watchv'] % self.videoid
64 |
65 | self.callback = callback
66 | self._have_basic = False
67 | self._have_gdata = False
68 |
69 | self._description = None
70 | self._likes = None
71 | self._dislikes = None
72 | self._category = None
73 | self._published = None
74 | self._username = None
75 |
76 | self._streams = []
77 | self._oggstreams = []
78 | self._m4astreams = []
79 | self._allstreams = []
80 | self._videostreams = []
81 | self._audiostreams = []
82 |
83 | self._title = None
84 | self._rating = None
85 | self._length = None
86 | self._author = None
87 | self._duration = None
88 | self._keywords = None
89 | self._bigthumb = None
90 | self._viewcount = None
91 | self._bigthumbhd = None
92 | self._bestthumb = None
93 | self._mix_pl = None
94 | self.expiry = None
95 |
96 | if basic:
97 | self._fetch_basic()
98 |
99 | if gdata:
100 | self._fetch_gdata()
101 |
102 | if size:
103 | for s in self.allstreams:
104 | # pylint: disable=W0104
105 | s.get_filesize()
106 |
107 |
108 | def _fetch_basic(self):
109 | """ Fetch basic data and streams. """
110 | raise NotImplementedError
111 |
112 |
113 | def _fetch_gdata(self):
114 | """ Extract gdata values, fetch gdata if necessary. """
115 | raise NotImplementedError
116 |
117 |
118 | def _get_video_gdata(self, video_id):
119 | """ Return json string containing video metadata from gdata api. """
120 | if self.callback:
121 | self.callback("Fetching video gdata")
122 | query = {'part': 'id,snippet,statistics',
123 | 'maxResults': 1,
124 | 'id': video_id}
125 | gdata = call_gdata('videos', query)
126 | dbg("Fetched video gdata")
127 | if self.callback:
128 | self.callback("Fetched video gdata")
129 | return gdata
130 |
131 |
132 | def _process_streams(self):
133 | """ Create Stream object lists from internal stream maps. """
134 | raise NotImplementedError
135 |
136 |
137 | def __repr__(self):
138 | """ Print video metadata. Return utf8 string. """
139 | if self._have_basic:
140 | info = [("Title", self.title),
141 | ("Author", self.author),
142 | ("ID", self.videoid),
143 | ("Duration", self.duration),
144 | ("Rating", self.rating),
145 | ("Views", self.viewcount),
146 | ("Thumbnail", self.thumb)]
147 |
148 | nfo = "\n".join(["%s: %s" % i for i in info])
149 |
150 | else:
151 | nfo = "Pafy object: %s [%s]" % (self.videoid,
152 | self.title[:45] + "..")
153 |
154 | return nfo.encode("utf8", "replace") if pyver == 2 else nfo
155 |
156 | @property
157 | def streams(self):
158 | """ The streams for a video. Returns list."""
159 | if not self._streams:
160 | self._process_streams()
161 |
162 | return self._streams
163 |
164 | @property
165 | def allstreams(self):
166 | """ All stream types for a video. Returns list. """
167 | if not self._allstreams:
168 | self._process_streams()
169 |
170 | return self._allstreams
171 |
172 | @property
173 | def audiostreams(self):
174 | """ Return a list of audio Stream objects. """
175 | if not self._audiostreams:
176 | self._process_streams()
177 |
178 | return self._audiostreams
179 |
180 | @property
181 | def videostreams(self):
182 | """ The video streams for a video. Returns list. """
183 | if not self._videostreams:
184 | self._process_streams()
185 |
186 | return self._videostreams
187 |
188 | @property
189 | def oggstreams(self):
190 | """ Return a list of ogg encoded Stream objects. """
191 | if not self._oggstreams:
192 | self._process_streams()
193 |
194 | return self._oggstreams
195 |
196 | @property
197 | def m4astreams(self):
198 | """ Return a list of m4a encoded Stream objects. """
199 | if not self._m4astreams:
200 | self._process_streams()
201 |
202 | return self._m4astreams
203 |
204 | @property
205 | def title(self):
206 | """ Return YouTube video title as a string. """
207 | if not self._title:
208 | self._fetch_basic()
209 |
210 | return self._title
211 |
212 | @property
213 | def author(self):
214 | """ The uploader of the video. Returns str. """
215 | if not self._author:
216 | self._fetch_basic()
217 |
218 | return self._author
219 |
220 | @property
221 | def rating(self):
222 | """ Rating for a video. Returns float. """
223 | if not self._rating:
224 | self._fetch_basic()
225 |
226 | return self._rating
227 |
228 | @property
229 | def length(self):
230 | """ Length of a video in seconds. Returns int. """
231 | if not self._length:
232 | self._fetch_basic()
233 |
234 | return self._length
235 |
236 | @property
237 | def viewcount(self):
238 | """ Number of views for a video. Returns int. """
239 | if not self._viewcount:
240 | self._fetch_basic()
241 |
242 | return self._viewcount
243 |
244 | @property
245 | def bigthumb(self):
246 | """ Large thumbnail image url. Returns str. """
247 | self._fetch_basic()
248 | return self._bigthumb
249 |
250 | @property
251 | def bigthumbhd(self):
252 | """ Extra large thumbnail image url. Returns str. """
253 | self._fetch_basic()
254 | return self._bigthumbhd
255 |
256 | @property
257 | def thumb(self):
258 | """ Thumbnail image url. Returns str. """
259 | return g.urls['thumb'] % self.videoid
260 |
261 | @property
262 | def duration(self):
263 | """ Duration of a video (HH:MM:SS). Returns str. """
264 | if not self._length:
265 | self._fetch_basic()
266 |
267 | self._duration = time.strftime('%H:%M:%S', time.gmtime(self._length))
268 | self._duration = uni(self._duration)
269 |
270 | return self._duration
271 |
272 | @property
273 | def keywords(self):
274 | """ Return keywords as list of str. """
275 | if not self._keywords:
276 | self._fetch_gdata()
277 |
278 | return self._keywords
279 |
280 | @property
281 | def category(self):
282 | """ YouTube category of the video. Returns string. """
283 | if not self._category:
284 | self._fetch_gdata()
285 |
286 | return self._category
287 |
288 | @property
289 | def description(self):
290 | """ Description of the video. Returns string. """
291 | if not self._description:
292 | self._fetch_gdata()
293 |
294 | return self._description
295 |
296 | @property
297 | def username(self):
298 | """ Return the username of the uploader. """
299 | if not self._username:
300 | self._fetch_basic()
301 |
302 | return self._username
303 |
304 | @property
305 | def published(self):
306 | """ The upload date and time of the video. Returns string. """
307 | if not self._published:
308 | self._fetch_gdata()
309 |
310 | return self._published.replace(".000Z", "").replace("T", " ")
311 |
312 | @property
313 | def likes(self):
314 | """ The number of likes for the video. Returns int. """
315 | if not self._likes:
316 | self._fetch_basic()
317 |
318 | return self._likes
319 |
320 | @property
321 | def dislikes(self):
322 | """ The number of dislikes for the video. Returns int. """
323 | if not self._dislikes:
324 | self._fetch_basic()
325 |
326 | return self._dislikes
327 |
328 | @property
329 | def mix(self):
330 | """ The playlist for the related YouTube mix. Returns a Playlist object. """
331 | if self._mix_pl is None:
332 | try:
333 | self._mix_pl = get_playlist2("RD" + self.videoid)
334 | except IOError:
335 | return None
336 | return self._mix_pl
337 |
338 | def _getbest(self, preftype="any", ftypestrict=True, vidonly=False):
339 | """
340 | Return the highest resolution video available.
341 |
342 | Select from video-only streams if vidonly is True
343 | """
344 | streams = self.videostreams if vidonly else self.streams
345 |
346 | if not streams:
347 | return None
348 |
349 | def _sortkey(x, key3d=0, keyres=0, keyftype=0):
350 | """ sort function for max(). """
351 | key3d = "3D" not in x.resolution
352 | keyres = int(x.resolution.split("x")[0])
353 | keyftype = preftype == x.extension
354 | strict = (key3d, keyftype, keyres)
355 | nonstrict = (key3d, keyres, keyftype)
356 | return strict if ftypestrict else nonstrict
357 |
358 | r = max(streams, key=_sortkey)
359 |
360 | if ftypestrict and preftype != "any" and r.extension != preftype:
361 | return None
362 |
363 | else:
364 | return r
365 |
366 | def getbestvideo(self, preftype="any", ftypestrict=True):
367 | """
368 | Return the best resolution video-only stream.
369 |
370 | set ftypestrict to False to return a non-preferred format if that
371 | has a higher resolution
372 | """
373 | return self._getbest(preftype, ftypestrict, vidonly=True)
374 |
375 | def getbest(self, preftype="any", ftypestrict=True):
376 | """
377 | Return the highest resolution video+audio stream.
378 |
379 | set ftypestrict to False to return a non-preferred format if that
380 | has a higher resolution
381 | """
382 | return self._getbest(preftype, ftypestrict, vidonly=False)
383 |
384 | def getbestaudio(self, preftype="any", ftypestrict=True):
385 | """ Return the highest bitrate audio Stream object."""
386 | if not self.audiostreams:
387 | return None
388 |
389 | def _sortkey(x, keybitrate=0, keyftype=0):
390 | """ Sort function for max(). """
391 | keybitrate = int(x.rawbitrate)
392 | keyftype = preftype == x.extension
393 | strict, nonstrict = (keyftype, keybitrate), (keybitrate, keyftype)
394 | return strict if ftypestrict else nonstrict
395 |
396 | r = max(self.audiostreams, key=_sortkey)
397 |
398 | if ftypestrict and preftype != "any" and r.extension != preftype:
399 | return None
400 |
401 | else:
402 | return r
403 |
404 | @classmethod
405 | def _content_available(cls, url):
406 | try:
407 | response = urlopen(url)
408 | except HTTPError:
409 | return False
410 | else:
411 | return response.getcode() < 300
412 |
413 | def getbestthumb(self):
414 | """ Return the best available thumbnail."""
415 | if not self._bestthumb:
416 | part_url = "http://i.ytimg.com/vi/%s/" % self.videoid
417 | # Thumbnail resolution sorted in descending order
418 | thumbs = ("maxresdefault.jpg",
419 | "sddefault.jpg",
420 | "hqdefault.jpg",
421 | "mqdefault.jpg",
422 | "default.jpg")
423 | for thumb in thumbs:
424 | url = part_url + thumb
425 | if self._content_available(url):
426 | return url
427 |
428 | return self._bestthumb
429 |
430 | def populate_from_playlist(self, pl_data):
431 | """ Populate Pafy object with items fetched from playlist data. """
432 | self._title = pl_data.get("title")
433 | self._author = pl_data.get("author")
434 | self._length = int(pl_data.get("length_seconds", 0))
435 | self._rating = pl_data.get("rating", 0.0)
436 | self._viewcount = "".join(re.findall(r"\d", "{0}".format(pl_data.get("views", "0"))))
437 | self._viewcount = int(self._viewcount)
438 | self._description = pl_data.get("description")
439 |
440 |
441 | class BaseStream(object):
442 |
443 | """ YouTube video stream class. """
444 |
445 | def __init__(self, parent):
446 | """ Set initial values. """
447 | self._itag = None
448 | self._mediatype = None
449 | self._threed = None
450 | self._rawbitrate = None
451 | self._resolution = None
452 | self._quality = None
453 | self._dimensions = None
454 | self._bitrate = None
455 | self._extension = None
456 | self.encrypted = None
457 | self._notes = None
458 | self._url = None
459 | self._rawurl = None
460 |
461 | self._parent = parent
462 | self._filename = None
463 | self._fsize = None
464 | self._active = False
465 |
466 | def generate_filename(self, meta=False, max_length=None):
467 | """ Generate filename. """
468 | ok = re.compile(r'[^/]')
469 |
470 | if os.name == "nt":
471 | ok = re.compile(r'[^\\/:*?"<>|]')
472 |
473 | filename = "".join(x if ok.match(x) else "_" for x in self.title)
474 |
475 | if meta:
476 | filename += " - %s - %s" % (self._parent.videoid, self.itag)
477 |
478 | if max_length:
479 | max_length = max_length + 1 + len(self.extension)
480 | if len(filename) > max_length:
481 | filename = filename[:max_length-3] + '...'
482 |
483 | filename += "." + self.extension
484 | return xenc(filename)
485 |
486 | @property
487 | def rawbitrate(self):
488 | """ Return raw bitrate value. """
489 | return self._rawbitrate
490 |
491 | @property
492 | def threed(self):
493 | """ Return bool, True if stream is 3D. """
494 | return self._threed
495 |
496 | @property
497 | def itag(self):
498 | """ Return itag value of stream. """
499 | return self._itag
500 |
501 | @property
502 | def resolution(self):
503 | """ Return resolution of stream as str. 0x0 if audio. """
504 | return self._resolution
505 |
506 | @property
507 | def dimensions(self):
508 | """ Return dimensions of stream as tuple. (0, 0) if audio. """
509 | return self._dimensions
510 |
511 | @property
512 | def quality(self):
513 | """ Return quality of stream (bitrate or resolution).
514 |
515 | eg, 128k or 640x480 (str)
516 | """
517 | return self._quality
518 |
519 | @property
520 | def title(self):
521 | """ Return YouTube video title as a string. """
522 | return self._parent.title
523 |
524 | @property
525 | def extension(self):
526 | """ Return appropriate file extension for stream (str).
527 |
528 | Possible values are: 3gp, m4a, m4v, mp4, webm, ogg
529 | """
530 | return self._extension
531 |
532 | @property
533 | def bitrate(self):
534 | """ Return bitrate of an audio stream. """
535 | return self._bitrate
536 |
537 | @property
538 | def mediatype(self):
539 | """ Return mediatype string (normal, audio or video).
540 |
541 | (normal means a stream containing both video and audio.)
542 | """
543 | return self._mediatype
544 |
545 | @property
546 | def notes(self):
547 | """ Return additional notes regarding the stream format. """
548 | return self._notes
549 |
550 | @property
551 | def filename(self):
552 | """ Return filename of stream; derived from title and extension. """
553 | if not self._filename:
554 | self._filename = self.generate_filename()
555 | return self._filename
556 |
557 | @property
558 | def url(self):
559 | """ Return the url, decrypt if required. """
560 | return self._url
561 |
562 | @property
563 | def url_https(self):
564 | """ Return https url. """
565 | return self.url.replace("http://", "https://")
566 |
567 | def __repr__(self):
568 | """ Return string representation. """
569 | out = "%s:%s@%s" % (self.mediatype, self.extension, self.quality)
570 | return out
571 |
572 | def get_filesize(self):
573 | """ Return filesize of the stream in bytes. Set member variable. """
574 | if not self._fsize:
575 |
576 | try:
577 | dbg("Getting stream size")
578 | cl = "content-length"
579 | self._fsize = int(g.opener.open(self.url).headers[cl])
580 | dbg("Got stream size")
581 |
582 | except (AttributeError, HTTPError, URLError):
583 | self._fsize = 0
584 |
585 | return self._fsize
586 |
587 | def cancel(self):
588 | """ Cancel an active download. """
589 | if self._active:
590 | self._active = False
591 | return True
592 |
593 | def download(self, filepath="", quiet=False, progress="Bytes",
594 | callback=None, meta=False, remux_audio=False):
595 | """ Download. Use quiet=True to supress output. Return filename.
596 |
597 | Use meta=True to append video id and itag to generated filename
598 | Use remax_audio=True to remux audio file downloads
599 |
600 | """
601 | # pylint: disable=R0912,R0914
602 | # Too many branches, too many local vars
603 | savedir = filename = ""
604 |
605 | if filepath and os.path.isdir(filepath):
606 | savedir, filename = filepath, self.generate_filename(max_length=256-len('.temp'))
607 |
608 | elif filepath:
609 | savedir, filename = os.path.split(filepath)
610 |
611 | else:
612 | filename = self.generate_filename(meta=meta, max_length=256-len('.temp'))
613 |
614 | filepath = os.path.join(savedir, filename)
615 | temp_filepath = filepath + ".temp"
616 |
617 | progress_available = ["KB", "MB", "GB"]
618 | if progress not in progress_available:
619 | progress = "Bytes"
620 |
621 | status_string = get_status_string(progress)
622 |
623 | response = g.opener.open(self.url)
624 | total = int(response.info()['Content-Length'].strip())
625 | chunksize, bytesdone, t0 = 16384, 0, time.time()
626 |
627 | fmode, offset = "wb", 0
628 |
629 | if os.path.exists(temp_filepath):
630 | if os.stat(temp_filepath).st_size < total:
631 |
632 | offset = os.stat(temp_filepath).st_size
633 | fmode = "ab"
634 |
635 | outfh = open(temp_filepath, fmode)
636 |
637 | if offset:
638 | # partial file exists, resume download
639 | resuming_opener = build_opener()
640 | resuming_opener.addheaders = [('User-Agent', g.user_agent),
641 | ("Range", "bytes=%s-" % offset)]
642 | response = resuming_opener.open(self.url)
643 | bytesdone = offset
644 |
645 | self._active = True
646 |
647 | while self._active:
648 | chunk = response.read(chunksize)
649 | outfh.write(chunk)
650 | elapsed = time.time() - t0
651 | bytesdone += len(chunk)
652 | if elapsed:
653 | rate = ((float(bytesdone) - float(offset)) / 1024.0) / elapsed
654 | eta = (total - bytesdone) / (rate * 1024)
655 | else: # Avoid ZeroDivisionError
656 | rate = 0
657 | eta = 0
658 |
659 | progress_stats = (get_size_done(bytesdone, progress),
660 | bytesdone * 1.0 / total, rate, eta)
661 |
662 | if not chunk:
663 | outfh.close()
664 | break
665 |
666 | if not quiet:
667 | status = status_string.format(*progress_stats)
668 | sys.stdout.write("\r" + status + ' ' * 4 + "\r")
669 | sys.stdout.flush()
670 |
671 | if callback:
672 | callback(total, *progress_stats)
673 |
674 | if self._active:
675 |
676 | if remux_audio and self.mediatype == "audio":
677 | remux(temp_filepath, filepath, quiet=quiet, muxer=remux_audio)
678 |
679 | else:
680 | os.rename(temp_filepath, filepath)
681 |
682 | return filepath
683 |
684 | else: # download incomplete, return temp filepath
685 | outfh.close()
686 | return temp_filepath
687 |
688 |
689 | def remux(infile, outfile, quiet=False, muxer="ffmpeg"):
690 | """ Remux audio. """
691 | muxer = muxer if isinstance(muxer, str) else "ffmpeg"
692 |
693 | for tool in set([muxer, "ffmpeg", "avconv"]):
694 | cmd = [tool, "-y", "-i", infile, "-acodec", "copy", "-vn", outfile]
695 |
696 | try:
697 | with open(os.devnull, "w") as devnull:
698 | subprocess.call(cmd, stdout=devnull, stderr=subprocess.STDOUT)
699 |
700 | except OSError:
701 | dbg("Failed to remux audio using %s", tool)
702 |
703 | else:
704 | os.unlink(infile)
705 | dbg("remuxed audio file using %s" % tool)
706 |
707 | if not quiet:
708 | sys.stdout.write("\nAudio remuxed.\n")
709 |
710 | break
711 |
712 | else:
713 | logging.warning("audio remux failed")
714 | os.rename(infile, outfile)
715 |
716 |
717 | def get_size_done(bytesdone, progress):
718 | _progress_dict = {'KB': 1024.0, 'MB': 1048576.0, 'GB': 1073741824.0}
719 | return round(bytesdone/_progress_dict.get(progress, 1.0), 2)
720 |
721 |
722 | def get_status_string(progress):
723 | status_string = (' {:,} ' + progress + ' [{:.2%}] received. Rate: [{:4.0f} '
724 | 'KB/s]. ETA: [{:.0f} secs]')
725 |
726 | if early_py_version:
727 | status_string = (' {0:} ' + progress + ' [{1:.2%}] received. Rate:'
728 | ' [{2:4.0f} KB/s]. ETA: [{3:.0f} secs]')
729 |
730 | return status_string
731 |
--------------------------------------------------------------------------------
/pafy/backend_youtube_dl.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import time
3 | import logging
4 | import os
5 | import subprocess
6 |
7 | if sys.version_info[:2] >= (3, 0):
8 | # pylint: disable=E0611,F0401,I0011
9 | uni = str
10 | else:
11 | uni = unicode
12 |
13 | import youtube_dl
14 |
15 | from . import g
16 | from .backend_shared import BasePafy, BaseStream, remux, get_status_string, get_size_done
17 |
18 | dbg = logging.debug
19 |
20 |
21 | early_py_version = sys.version_info[:2] < (2, 7)
22 |
23 |
24 | class YtdlPafy(BasePafy):
25 | def __init__(self, *args, **kwargs):
26 | self._ydl_info = None
27 | self._ydl_opts = g.def_ydl_opts
28 | ydl_opts = kwargs.get("ydl_opts")
29 | if ydl_opts:
30 | self._ydl_opts.update(ydl_opts)
31 | super(YtdlPafy, self).__init__(*args, **kwargs)
32 |
33 | def _fetch_basic(self):
34 | """ Fetch basic data and streams. """
35 | if self._have_basic:
36 | return
37 |
38 | with youtube_dl.YoutubeDL(self._ydl_opts) as ydl:
39 | try:
40 | self._ydl_info = ydl.extract_info(self.videoid, download=False)
41 | # Turn into an IOError since that is what pafy previously raised
42 | except youtube_dl.utils.DownloadError as e:
43 | raise IOError(str(e).replace('YouTube said', 'Youtube says'))
44 |
45 | if self.callback:
46 | self.callback("Fetched video info")
47 |
48 | self._title = self._ydl_info['title']
49 | self._author = self._ydl_info['uploader']
50 | self._rating = self._ydl_info['average_rating']
51 | self._length = self._ydl_info['duration']
52 | self._viewcount = self._ydl_info['view_count']
53 | self._likes = self._ydl_info['like_count']
54 | self._dislikes = self._ydl_info.get('dislike_count',0)
55 | self._username = self._ydl_info['uploader_id']
56 | self._category = self._ydl_info['categories'][0] if self._ydl_info['categories'] else ''
57 | self._bestthumb = self._ydl_info['thumbnails'][0]['url']
58 | self._bigthumb = g.urls['bigthumb'] % self.videoid
59 | self._bigthumbhd = g.urls['bigthumbhd'] % self.videoid
60 | self.expiry = time.time() + g.lifespan
61 |
62 | self._have_basic = True
63 |
64 | def _fetch_gdata(self):
65 | """ Extract gdata values, fetch gdata if necessary. """
66 | if self._have_gdata:
67 | return
68 |
69 | item = self._get_video_gdata(self.videoid)['items'][0]
70 | snippet = item['snippet']
71 | self._published = uni(snippet['publishedAt'])
72 | self._description = uni(snippet["description"])
73 | # Note: using snippet.get since some videos have no tags object
74 | self._keywords = [uni(i) for i in snippet.get('tags', ())]
75 | self._have_gdata = True
76 |
77 | def _process_streams(self):
78 | """ Create Stream object lists from internal stream maps. """
79 |
80 | if not self._have_basic:
81 | self._fetch_basic()
82 |
83 | allstreams = [YtdlStream(z, self) for z in self._ydl_info['formats']]
84 | self._streams = [i for i in allstreams if i.mediatype == 'normal']
85 | self._audiostreams = [i for i in allstreams if i.mediatype == 'audio']
86 | self._videostreams = [i for i in allstreams if i.mediatype == 'video']
87 | self._m4astreams = [i for i in allstreams if i.extension == 'm4a']
88 | self._oggstreams = [i for i in allstreams if i.extension == 'ogg']
89 | self._allstreams = allstreams
90 |
91 |
92 | class YtdlStream(BaseStream):
93 | def __init__(self, info, parent):
94 | super(YtdlStream, self).__init__(parent)
95 | self._itag = info['format_id']
96 |
97 | if (info.get('acodec') != 'none' and
98 | info.get('vcodec') == 'none'):
99 | self._mediatype = 'audio'
100 | elif (info.get('acodec') == 'none' and
101 | info.get('vcodec') != 'none'):
102 | self._mediatype = 'video'
103 | else:
104 | self._mediatype = 'normal'
105 |
106 | self._threed = info.get('format_note') == '3D'
107 | self._rawbitrate = info.get('abr', 0) * 1024
108 |
109 | height = info.get('height') or 0
110 | width = info.get('width') or 0
111 | self._resolution = str(width) + 'x' + str(height)
112 | self._dimensions = width, height
113 | self._bitrate = str(info.get('abr', 0)) + 'k'
114 | self._quality = self._bitrate if self._mediatype == 'audio' else self._resolution
115 |
116 | self._extension = info['ext']
117 | self._notes = info.get('format_note') or ''
118 | self._url = info.get('url')
119 |
120 | self._info = info
121 |
122 | def get_filesize(self):
123 | """ Return filesize of the stream in bytes. Set member variable. """
124 |
125 | # Faster method
126 | if 'filesize' in self._info and self._info['filesize'] is not None:
127 | return self._info['filesize']
128 |
129 | # Fallback
130 | return super(YtdlStream, self).get_filesize()
131 |
132 | def download(self, filepath="", quiet=False, progress="Bytes",
133 | callback=None, meta=False, remux_audio=False):
134 |
135 | downloader = youtube_dl.downloader.http.HttpFD(ydl(),
136 | {'http_chunk_size': 10485760})
137 |
138 | progress_available = ["KB", "MB", "GB"]
139 | if progress not in progress_available:
140 | progress = "Bytes"
141 |
142 | status_string = get_status_string(progress)
143 |
144 | def progress_hook(s):
145 | if s['status'] == 'downloading':
146 | bytesdone = s['downloaded_bytes']
147 | total = s['total_bytes']
148 | if s.get('speed') is not None:
149 | rate = s['speed'] / 1024
150 | else:
151 | rate = 0
152 | if s.get('eta') is None:
153 | eta = 0
154 | else:
155 | eta = s['eta']
156 |
157 | progress_stats = (get_size_done(bytesdone, progress),
158 | bytesdone*1.0/total, rate, eta)
159 | if not quiet:
160 | status = status_string.format(*progress_stats)
161 | sys.stdout.write("\r" + status + ' ' * 4 + "\r")
162 | sys.stdout.flush()
163 |
164 | if callback:
165 | callback(total, *progress_stats)
166 |
167 | downloader._progress_hooks = [progress_hook]
168 |
169 | if filepath and os.path.isdir(filepath):
170 | filename = self.generate_filename(max_length=256 - len('.temp'))
171 | filepath = os.path.join(filepath, filename)
172 |
173 | elif filepath:
174 | pass
175 |
176 | else:
177 | filepath = self.generate_filename(meta=meta, max_length=256 - len('.temp'))
178 |
179 | infodict = {'url': self.url}
180 |
181 | downloader.download(filepath, infodict)
182 | print()
183 |
184 | if remux_audio and self.mediatype == "audio":
185 | subprocess.run(['mv', filepath, filepath + '.temp'])
186 | remux(filepath + '.temp', filepath, quiet=quiet, muxer=remux_audio)
187 |
188 |
189 | class ydl:
190 | def urlopen(self, url):
191 | return g.opener.open(url)
192 |
193 | def to_screen(self, *args, **kwargs):
194 | pass
195 |
196 | def to_console_title(self, *args, **kwargs):
197 | pass
198 |
199 | def trouble(self, *args, **kwargs):
200 | pass
201 |
202 | def report_warning(self, *args, **kwargs):
203 | pass
204 |
205 | def report_error(self, *args, **kwargs):
206 | pass
207 |
--------------------------------------------------------------------------------
/pafy/channel.py:
--------------------------------------------------------------------------------
1 | import re
2 | from .pafy import call_gdata
3 | from .playlist import Playlist
4 | from .backend_shared import pyver
5 |
6 |
7 | def get_channel(channel_url, basic=False, gdata=False,
8 | size=False, callback=None):
9 | """Return a Channel object
10 |
11 | The returned Pafy and Playlist objects are initialised using the arguments
12 | to get_channel() in the manner documented for pafy.new()
13 |
14 | """
15 |
16 | return Channel.from_url(channel_url, basic, gdata, size, callback)
17 |
18 |
19 | class Channel(object):
20 | def __init__(self, channel_url, basic, gdata, size, callback):
21 |
22 | self._channel_url = channel_url
23 | self._channel_id = None
24 | self._title = None
25 | self._description = None
26 | self._logo = None
27 | self._subscriberCount = None
28 | self._uploads = None
29 | self._basic = basic
30 | self._gdata = gdata
31 | self._size = size
32 | self._callback = callback
33 | self._playlists = None
34 | self._subscriptions = None
35 | self._have_basic = False
36 |
37 | @classmethod
38 | def from_dict(cls, ch, basic, gdata, size, callback):
39 | t = cls(ch['id'], basic, gdata, size, callback)
40 | t._channel_id = ch['id']
41 | t._title = ch['title']
42 | t._description = ch['description']
43 | t._logo = ch['logo']
44 | t._subscriberCount = ch['subscriberCount']
45 | t._uploads = ch['uploads']
46 | t._have_basic = True
47 |
48 | return t
49 |
50 | @classmethod
51 | def from_url(cls, url, basic, gdata, size, callback):
52 | t = cls(url, basic, gdata, size, callback)
53 | t._fetch_basic()
54 | return t
55 |
56 | @property
57 | def channel_id(self):
58 | if not self._have_basic:
59 | self._fetch_basic()
60 | return self._channel_id
61 |
62 | @property
63 | def title(self):
64 | if not self._have_basic:
65 | self._fetch_basic()
66 | return self._title
67 |
68 | @property
69 | def description(self):
70 | if not self._have_basic:
71 | self._fetch_basic()
72 | return self._description
73 |
74 | @property
75 | def logo(self):
76 | if not self._have_basic:
77 | self._fetch_basic()
78 | return self._logo
79 |
80 | @property
81 | def subscriberCount(self):
82 | if not self._have_basic:
83 | self._fetch_basic()
84 | return self._subscriberCount
85 |
86 | @property
87 | def uploads(self):
88 | if not self._uploads:
89 | self._fetch_basic()
90 | if type(self._uploads) != Playlist:
91 | self._uploads = Playlist.from_url(self._uploads, self._basic,
92 | self._gdata, self._size,
93 | self._callback)
94 |
95 | return self._uploads
96 |
97 | @property
98 | def playlists(self):
99 | if self._playlists is not None:
100 | for playlist in self._playlists:
101 | yield playlist
102 | return
103 |
104 | playlists = []
105 |
106 | query = {'part': 'snippet,contentDetails',
107 | 'maxResults': 50,
108 | 'channelId': self.channel_id}
109 |
110 | while True:
111 | playlistList = call_gdata('playlists', query)
112 |
113 | for pl in playlistList['items']:
114 | try:
115 | thumbnail = pl['snippet']['thumbnails']['standard']['url']
116 | except KeyError:
117 | thumbnail = None
118 | pl_data = dict(
119 | id=pl['id'],
120 | title=pl['snippet']['title'],
121 | author=pl['snippet']['channelTitle'],
122 | description=pl['snippet']['description'],
123 | thumbnail=thumbnail,
124 | len=pl['contentDetails']['itemCount']
125 | )
126 |
127 | pl_obj = Playlist.from_dict(pl_data, self._basic, self._gdata,
128 | self._size, self._callback)
129 | playlists.append(pl_obj)
130 | if self._callback:
131 | self._callback("Added playlist: %s" % pl_data['title'])
132 | yield pl_obj
133 |
134 | if not playlistList.get('nextPageToken'):
135 | break
136 | query['pageToken'] = playlistList['nextPageToken']
137 |
138 | self._playlists = playlists
139 |
140 | @property
141 | def subscriptions(self):
142 | if self._subscriptions is not None:
143 | for sub in self._subscriptions:
144 | yield sub
145 | return
146 |
147 | subscriptions = []
148 | query = {'part': 'snippet',
149 | 'maxResults': 50,
150 | 'channelId': self.channel_id}
151 |
152 | while True:
153 | subs_data = call_gdata('subscriptions', query)
154 | sub_ids = []
155 |
156 | for sub in subs_data['items']:
157 | sub_ids.append(sub['snippet']['resourceId']['channelId'])
158 |
159 | query2 = {'part': 'snippet, contentDetails, statistics',
160 | 'id': ','.join(sub_ids),
161 | 'maxResults': 50}
162 |
163 | data = call_gdata('channels', query2)
164 |
165 | for ch in data['items']:
166 | channel_data = dict(
167 | id=ch['id'],
168 | title=ch['snippet']['title'],
169 | description=ch['snippet']['description'],
170 | logo=ch['snippet']['thumbnails']['default']['url'],
171 | subscriberCount=ch['statistics']['subscriberCount'],
172 | uploads=ch['contentDetails']['relatedPlaylists']['uploads']
173 | )
174 | sub_obj = Channel.from_dict(channel_data, self._basic,
175 | self._gdata, self._size,
176 | self._callback)
177 | subscriptions.append(sub_obj)
178 | yield sub_obj
179 |
180 | if not subs_data.get('nextPageToken'):
181 | break
182 | query['pageToken'] = subs_data['nextPageToken']
183 |
184 | self._subscriptions = subscriptions
185 |
186 | def __repr__(self):
187 | if not self._have_basic:
188 | self._fetch_basic()
189 |
190 | info = [("Type", "Channel"),
191 | ("Title", self.title),
192 | ("Description", self.description),
193 | ("SubscriberCount", self.subscriberCount)]
194 |
195 | nfo = "\n".join(["%s: %s" % i for i in info])
196 |
197 | return nfo.encode("utf8", "replace") if pyver == 2 else nfo
198 |
199 | def _fetch_basic(self):
200 | query = None
201 | chanR = re.compile('.+channel\/([^\/]+)$')
202 | userR = re.compile('.+user\/([^\/]+)$')
203 | channel_id = None
204 | channel_url = self._channel_url
205 | if chanR.match(channel_url):
206 | channel_id = chanR.search(channel_url).group(1)
207 | elif userR.match(channel_url):
208 | username = userR.search(channel_url).group(1)
209 | query = {'part': 'snippet, contentDetails, statistics',
210 | 'forUsername': username}
211 | elif len(channel_url) == 24 and channel_url[:2] == 'UC':
212 | channel_id = channel_url
213 | else:
214 | username = channel_url
215 | query = {'part': 'snippet, contentDetails, statistics',
216 | 'forUsername': username}
217 |
218 | if query is None:
219 | query = {'part': 'snippet, contentDetails, statistics',
220 | 'id': channel_id}
221 | allinfo = call_gdata('channels', query)
222 |
223 | try:
224 | ch = allinfo['items'][0]
225 | except IndexError:
226 | err = "Unrecognized channel id, url or name : %s"
227 | raise ValueError(err % channel_url)
228 |
229 | self._channel_id = ch['id']
230 | self._title = ch['snippet']['title']
231 | self._description = ch['snippet']['description']
232 | self._logo = ch['snippet']['thumbnails']['default']['url']
233 | self._subscriberCount = ch['statistics']['subscriberCount']
234 | self._uploads = ch['contentDetails']['relatedPlaylists']['uploads']
235 | self._have_basic = True
236 |
--------------------------------------------------------------------------------
/pafy/g.py:
--------------------------------------------------------------------------------
1 | import sys
2 | if sys.version_info[:2] >= (3, 0):
3 | # pylint: disable=E0611,F0401,I0011
4 | from urllib.request import build_opener
5 | else:
6 | from urllib2 import build_opener
7 |
8 | from . import __version__
9 |
10 | urls = {
11 | 'gdata': "https://www.googleapis.com/youtube/v3/",
12 | 'watchv': "http://www.youtube.com/watch?v=%s",
13 | 'playlist': ('http://www.youtube.com/list_ajax?'
14 | 'style=json&action_get_list=1&list=%s'),
15 | 'thumb': "http://i.ytimg.com/vi/%s/default.jpg",
16 | 'bigthumb': "http://i.ytimg.com/vi/%s/mqdefault.jpg",
17 | 'bigthumbhd': "http://i.ytimg.com/vi/%s/hqdefault.jpg",
18 |
19 | # For internal backend
20 | 'vidinfo': ('https://www.youtube.com/get_video_info?video_id=%s&'
21 | 'eurl=https://youtube.googleapis.com/v/%s&sts=%s'),
22 | 'embed': "https://youtube.com/embed/%s"
23 | }
24 | api_key = "AIzaSyCIM4EzNqi1in22f4Z3Ru3iYvLaY8tc3bo"
25 | user_agent = "pafy " + __version__
26 | lifespan = 60 * 60 * 5 # 5 hours
27 | opener = build_opener()
28 | opener.addheaders = [('User-Agent', user_agent)]
29 | cache = {}
30 | def_ydl_opts = {'quiet': True, 'prefer_insecure': False, 'no_warnings': True}
31 |
32 | # The following are specific to the internal backend
33 | UEFSM = 'url_encoded_fmt_stream_map'
34 | AF = 'adaptive_fmts'
35 | jsplayer = r';ytplayer\.config\s*=\s*({.*?});'
36 | itags = {
37 | '5': ('320x240', 'flv', "normal", ''),
38 | '17': ('176x144', '3gp', "normal", ''),
39 | '18': ('640x360', 'mp4', "normal", ''),
40 | '22': ('1280x720', 'mp4', "normal", ''),
41 | '34': ('640x360', 'flv', "normal", ''),
42 | '35': ('854x480', 'flv', "normal", ''),
43 | '36': ('320x240', '3gp', "normal", ''),
44 | '37': ('1920x1080', 'mp4', "normal", ''),
45 | '38': ('4096x3072', 'mp4', "normal", '4:3 hi-res'),
46 | '43': ('640x360', 'webm', "normal", ''),
47 | '44': ('854x480', 'webm', "normal", ''),
48 | '45': ('1280x720', 'webm', "normal", ''),
49 | '46': ('1920x1080', 'webm', "normal", ''),
50 | '82': ('640x360-3D', 'mp4', "normal", ''),
51 | '83': ('640x480-3D', 'mp4', 'normal', ''),
52 | '84': ('1280x720-3D', 'mp4', "normal", ''),
53 | '100': ('640x360-3D', 'webm', "normal", ''),
54 | '102': ('1280x720-3D', 'webm', "normal", ''),
55 | '133': ('426x240', 'm4v', 'video', ''),
56 | '134': ('640x360', 'm4v', 'video', ''),
57 | '135': ('854x480', 'm4v', 'video', ''),
58 | '136': ('1280x720', 'm4v', 'video', ''),
59 | '137': ('1920x1080', 'm4v', 'video', ''),
60 | '138': ('4096x3072', 'm4v', 'video', ''),
61 | '139': ('48k', 'm4a', 'audio', ''),
62 | '140': ('128k', 'm4a', 'audio', ''),
63 | '141': ('256k', 'm4a', 'audio', ''),
64 | '160': ('256x144', 'm4v', 'video', ''),
65 | '167': ('640x480', 'webm', 'video', ''),
66 | '168': ('854x480', 'webm', 'video', ''),
67 | '169': ('1280x720', 'webm', 'video', ''),
68 | '170': ('1920x1080', 'webm', 'video', ''),
69 | '171': ('128k', 'ogg', 'audio', ''),
70 | '172': ('192k', 'ogg', 'audio', ''),
71 | '218': ('854x480', 'webm', 'video', 'VP8'),
72 | '219': ('854x480', 'webm', 'video', 'VP8'),
73 | '242': ('360x240', 'webm', 'video', 'VP9'),
74 | '243': ('480x360', 'webm', 'video', 'VP9'),
75 | '244': ('640x480', 'webm', 'video', 'VP9 low'),
76 | '245': ('640x480', 'webm', 'video', 'VP9 med'),
77 | '246': ('640x480', 'webm', 'video', 'VP9 high'),
78 | '247': ('720x480', 'webm', 'video', 'VP9'),
79 | '248': ('1920x1080', 'webm', 'video', 'VP9'),
80 | '249': ('48k', 'opus', 'audio', 'Opus'),
81 | '250': ('56k', 'opus', 'audio', 'Opus'),
82 | '251': ('128k', 'opus', 'audio', 'Opus'),
83 | '256': ('192k', 'm4a', 'audio', '6-channel'),
84 | '258': ('320k', 'm4a', 'audio', '6-channel'),
85 | '264': ('2560x1440', 'm4v', 'video', ''),
86 | '266': ('3840x2160', 'm4v', 'video', 'AVC'),
87 | '271': ('1920x1280', 'webm', 'video', 'VP9'),
88 | '272': ('3414x1080', 'webm', 'video', 'VP9'),
89 | '278': ('256x144', 'webm', 'video', 'VP9'),
90 | '298': ('1280x720', 'm4v', 'video', '60fps'),
91 | '299': ('1920x1080', 'm4v', 'video', '60fps'),
92 | '302': ('1280x720', 'webm', 'video', 'VP9'),
93 | '303': ('1920x1080', 'webm', 'video', 'VP9'),
94 | }
95 |
--------------------------------------------------------------------------------
/pafy/jsinterp.py:
--------------------------------------------------------------------------------
1 | # Copied from youtube_dl
2 |
3 | from __future__ import unicode_literals
4 |
5 | import json
6 | import operator
7 | import re
8 | import sys
9 | import traceback
10 |
11 |
12 | class ExtractorError(Exception):
13 | """Error during info extraction."""
14 |
15 | def __init__(self, msg, tb=None, expected=False, cause=None, video_id=None):
16 | """ tb, if given, is the original traceback (so that it can be printed out).
17 | If expected is set, this is a normal error message and most likely not a bug in youtube-dl.
18 | """
19 |
20 | if video_id is not None:
21 | msg = video_id + ': ' + msg
22 | if cause:
23 | msg += ' (caused by %r)' % cause
24 | super(ExtractorError, self).__init__(msg)
25 |
26 | self.traceback = tb
27 | self.exc_info = sys.exc_info() # preserve original exception
28 | self.cause = cause
29 | self.video_id = video_id
30 |
31 | def format_traceback(self):
32 | if self.traceback is None:
33 | return None
34 | return ''.join(traceback.format_tb(self.traceback))
35 |
36 |
37 | _OPERATORS = [
38 | ('|', operator.or_),
39 | ('^', operator.xor),
40 | ('&', operator.and_),
41 | ('>>', operator.rshift),
42 | ('<<', operator.lshift),
43 | ('-', operator.sub),
44 | ('+', operator.add),
45 | ('%', operator.mod),
46 | ('/', operator.truediv),
47 | ('*', operator.mul),
48 | ]
49 | _ASSIGN_OPERATORS = [(op + '=', opfunc) for op, opfunc in _OPERATORS]
50 | _ASSIGN_OPERATORS.append(('=', lambda cur, right: right))
51 |
52 | _NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*'
53 |
54 |
55 | class JSInterpreter(object):
56 | def __init__(self, code, objects=None):
57 | if objects is None:
58 | objects = {}
59 | self.code = code
60 | self._functions = {}
61 | self._objects = objects
62 |
63 | def interpret_statement(self, stmt, local_vars, allow_recursion=100):
64 | if allow_recursion < 0:
65 | raise ExtractorError('Recursion limit reached')
66 |
67 | should_abort = False
68 | stmt = stmt.lstrip()
69 | stmt_m = re.match(r'var\s', stmt)
70 | if stmt_m:
71 | expr = stmt[len(stmt_m.group(0)):]
72 | else:
73 | return_m = re.match(r'return(?:\s+|$)', stmt)
74 | if return_m:
75 | expr = stmt[len(return_m.group(0)):]
76 | should_abort = True
77 | else:
78 | # Try interpreting it as an expression
79 | expr = stmt
80 |
81 | v = self.interpret_expression(expr, local_vars, allow_recursion)
82 | return v, should_abort
83 |
84 | def interpret_expression(self, expr, local_vars, allow_recursion):
85 | expr = expr.strip()
86 |
87 | if expr == '': # Empty expression
88 | return None
89 |
90 | if expr.startswith('('):
91 | parens_count = 0
92 | for m in re.finditer(r'[()]', expr):
93 | if m.group(0) == '(':
94 | parens_count += 1
95 | else:
96 | parens_count -= 1
97 | if parens_count == 0:
98 | sub_expr = expr[1:m.start()]
99 | sub_result = self.interpret_expression(
100 | sub_expr, local_vars, allow_recursion)
101 | remaining_expr = expr[m.end():].strip()
102 | if not remaining_expr:
103 | return sub_result
104 | else:
105 | expr = json.dumps(sub_result) + remaining_expr
106 | break
107 | else:
108 | raise ExtractorError('Premature end of parens in %r' % expr)
109 |
110 | for op, opfunc in _ASSIGN_OPERATORS:
111 | m = re.match(r'''(?x)
112 | (?P%s)(?:\[(?P[^\]]+?)\])?
113 | \s*%s
114 | (?P.*)$''' % (_NAME_RE, re.escape(op)), expr)
115 | if not m:
116 | continue
117 | right_val = self.interpret_expression(
118 | m.group('expr'), local_vars, allow_recursion - 1)
119 |
120 | if m.groupdict().get('index'):
121 | lvar = local_vars[m.group('out')]
122 | idx = self.interpret_expression(
123 | m.group('index'), local_vars, allow_recursion)
124 | assert isinstance(idx, int)
125 | cur = lvar[idx]
126 | val = opfunc(cur, right_val)
127 | lvar[idx] = val
128 | return val
129 | else:
130 | cur = local_vars.get(m.group('out'))
131 | val = opfunc(cur, right_val)
132 | local_vars[m.group('out')] = val
133 | return val
134 |
135 | if expr.isdigit():
136 | return int(expr)
137 |
138 | var_m = re.match(
139 | r'(?!if|return|true|false)(?P%s)$' % _NAME_RE,
140 | expr)
141 | if var_m:
142 | return local_vars[var_m.group('name')]
143 |
144 | try:
145 | return json.loads(expr)
146 | except ValueError:
147 | pass
148 |
149 | m = re.match(
150 | r'(?P%s)\.(?P[^(]+)(?:\(+(?P[^()]*)\))?$' % _NAME_RE,
151 | expr)
152 | if m:
153 | variable = m.group('var')
154 | member = m.group('member')
155 | arg_str = m.group('args')
156 |
157 | if variable in local_vars:
158 | obj = local_vars[variable]
159 | else:
160 | if variable not in self._objects:
161 | self._objects[variable] = self.extract_object(variable)
162 | obj = self._objects[variable]
163 |
164 | if arg_str is None:
165 | # Member access
166 | if member == 'length':
167 | return len(obj)
168 | return obj[member]
169 |
170 | assert expr.endswith(')')
171 | # Function call
172 | if arg_str == '':
173 | argvals = tuple()
174 | else:
175 | argvals = tuple([
176 | self.interpret_expression(v, local_vars, allow_recursion)
177 | for v in arg_str.split(',')])
178 |
179 | if member == 'split':
180 | assert argvals == ('',)
181 | return list(obj)
182 | if member == 'join':
183 | assert len(argvals) == 1
184 | return argvals[0].join(obj)
185 | if member == 'reverse':
186 | assert len(argvals) == 0
187 | obj.reverse()
188 | return obj
189 | if member == 'slice':
190 | assert len(argvals) == 1
191 | return obj[argvals[0]:]
192 | if member == 'splice':
193 | assert isinstance(obj, list)
194 | index, howMany = argvals
195 | res = []
196 | for i in range(index, min(index + howMany, len(obj))):
197 | res.append(obj.pop(index))
198 | return res
199 |
200 | return obj[member](argvals)
201 |
202 | m = re.match(
203 | r'(?P%s)\[(?P.+)\]$' % _NAME_RE, expr)
204 | if m:
205 | val = local_vars[m.group('in')]
206 | idx = self.interpret_expression(
207 | m.group('idx'), local_vars, allow_recursion - 1)
208 | return val[idx]
209 |
210 | for op, opfunc in _OPERATORS:
211 | m = re.match(r'(?P.+?)%s(?P.+)' % re.escape(op), expr)
212 | if not m:
213 | continue
214 | x, abort = self.interpret_statement(
215 | m.group('x'), local_vars, allow_recursion - 1)
216 | if abort:
217 | raise ExtractorError(
218 | 'Premature left-side return of %s in %r' % (op, expr))
219 | y, abort = self.interpret_statement(
220 | m.group('y'), local_vars, allow_recursion - 1)
221 | if abort:
222 | raise ExtractorError(
223 | 'Premature right-side return of %s in %r' % (op, expr))
224 | return opfunc(x, y)
225 |
226 | m = re.match(
227 | r'^(?P%s)\((?P[a-zA-Z0-9_$,]+)\)$' % _NAME_RE, expr)
228 | if m:
229 | fname = m.group('func')
230 | argvals = tuple([
231 | int(v) if v.isdigit() else local_vars[v]
232 | for v in m.group('args').split(',')])
233 | if fname not in self._functions:
234 | self._functions[fname] = self.extract_function(fname)
235 | return self._functions[fname](argvals)
236 |
237 | raise ExtractorError('Unsupported JS expression %r' % expr)
238 |
239 | def extract_object(self, objname):
240 | obj = {}
241 | obj_m = re.search(
242 | (r'(?:var\s+)?%s\s*=\s*\{' % re.escape(objname)) +
243 | r'\s*(?P([a-zA-Z$0-9]+\s*:\s*function\(.*?\)\s*\{.*?\}(?:,\s*)?)*)' +
244 | r'\}\s*;',
245 | self.code)
246 | fields = obj_m.group('fields')
247 | # Currently, it only supports function definitions
248 | fields_m = re.finditer(
249 | r'(?P[a-zA-Z$0-9]+)\s*:\s*function'
250 | r'\((?P[a-z,]+)\){(?P[^}]+)}',
251 | fields)
252 | for f in fields_m:
253 | argnames = f.group('args').split(',')
254 | obj[f.group('key')] = self.build_function(argnames, f.group('code'))
255 |
256 | return obj
257 |
258 | def extract_function(self, funcname):
259 | func_m = re.search(
260 | r'''(?x)
261 | (?:function\s+%s|[{;,]\s*%s\s*=\s*function|var\s+%s\s*=\s*function)\s*
262 | \((?P[^)]*)\)\s*
263 | \{(?P[^}]+)\}''' % (
264 | re.escape(funcname), re.escape(funcname), re.escape(funcname)),
265 | self.code)
266 | if func_m is None:
267 | raise ExtractorError('Could not find JS function %r' % funcname)
268 | argnames = func_m.group('args').split(',')
269 |
270 | return self.build_function(argnames, func_m.group('code'))
271 |
272 | def call_function(self, funcname, *args):
273 | f = self.extract_function(funcname)
274 | return f(args)
275 |
276 | def build_function(self, argnames, code):
277 | def resf(args):
278 | local_vars = dict(zip(argnames, args))
279 | for stmt in code.split(';'):
280 | res, abort = self.interpret_statement(stmt, local_vars)
281 | if abort:
282 | break
283 | return res
284 | return resf
285 |
--------------------------------------------------------------------------------
/pafy/pafy.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | """
4 | pafy.py.
5 |
6 | Python library to download YouTube content and retrieve metadata
7 |
8 | https://github.com/np1/pafy
9 |
10 | Copyright (C) 2013-2014 np1
11 |
12 | This program is free software: you can redistribute it and/or modify it under
13 | the terms of the GNU Lesser General Public License as published by the Free
14 | Software Foundation, either version 3 of the License, or (at your option) any
15 | later version.
16 |
17 | This program is distributed in the hope that it will be useful, but WITHOUT ANY
18 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 | PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 |
21 | You should have received a copy of the GNU Lesser General Public License along
22 | with this program. If not, see .
23 |
24 | """
25 |
26 | import sys
27 | import os
28 | import logging
29 | import time
30 | import re
31 |
32 | if sys.version_info[:2] >= (3, 0):
33 | # pylint: disable=E0611,F0401,I0011
34 | from urllib.error import HTTPError
35 |
36 | else:
37 | from urllib2 import HTTPError
38 |
39 | from . import g
40 | from .util import call_gdata
41 |
42 | Pafy = None
43 |
44 | # Select which backend to use
45 | backend = "internal"
46 | if os.environ.get("PAFY_BACKEND") != "internal":
47 | try:
48 | import youtube_dl
49 | backend = "youtube-dl"
50 | except ImportError:
51 | raise ImportError(
52 | "pafy: youtube-dl not found; you can use the internal backend by "
53 | "setting the environmental variable PAFY_BACKEND to \"internal\". "
54 | "It is not enabled by default because it is not as well maintained "
55 | "as the youtube-dl backend.")
56 |
57 | if os.environ.get("pafydebug") == "1":
58 | logging.basicConfig(level=logging.DEBUG)
59 |
60 |
61 | dbg = logging.debug
62 |
63 |
64 | def fetch_decode(url, encoding=None):
65 | """ Fetch url and decode. """
66 | try:
67 | req = g.opener.open(url)
68 | except HTTPError as e:
69 | if e.getcode() == 503:
70 | time.sleep(.5)
71 | return fetch_decode(url, encoding)
72 | else:
73 | raise
74 |
75 | ct = req.headers['content-type']
76 |
77 | if encoding:
78 | return req.read().decode(encoding)
79 |
80 | elif "charset=" in ct:
81 | dbg("charset: %s", ct)
82 | encoding = re.search(r"charset=([\w-]+)\s*(:?;|$)", ct).group(1)
83 | return req.read().decode(encoding)
84 |
85 | else:
86 | dbg("encoding unknown")
87 | return req.read()
88 |
89 |
90 | def new(url, basic=True, gdata=False, size=False,
91 | callback=None, ydl_opts=None):
92 | """ Return a new pafy instance given a url or video id.
93 |
94 | NOTE: The signature argument has been deprecated and now has no effect,
95 | it will be removed in a future version.
96 |
97 | Optional arguments:
98 | basic - fetch basic metadata and streams
99 | gdata - fetch gdata info (upload date, description, category)
100 | size - fetch the size of each stream (slow)(decrypts urls if needed)
101 | callback - a callback function to receive status strings
102 |
103 | If any of the first three above arguments are False, those data items will
104 | be fetched only when first called for.
105 |
106 | The defaults are recommended for most cases. If you wish to create
107 | many video objects at once, you may want to set basic to False, eg:
108 |
109 | video = pafy.new(basic=False)
110 |
111 | This will be quick because no http requests will be made on initialisation.
112 |
113 | Setting size to True will override the basic argument and force basic data
114 | to be fetched too (basic data is required to obtain Stream objects).
115 |
116 | """
117 | global Pafy
118 | if Pafy is None:
119 | if backend == "internal":
120 | from .backend_internal import InternPafy as Pafy
121 | else:
122 | from .backend_youtube_dl import YtdlPafy as Pafy
123 |
124 | return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
125 |
126 |
127 | def cache(name):
128 | """ Returns a sub-cache dictionary under which global key, value pairs
129 | can be stored. Regardless of whether a dictionary already exists for
130 | the given name, the sub-cache is returned by reference.
131 | """
132 | if name not in g.cache:
133 | g.cache[name] = {}
134 | return g.cache[name]
135 |
136 |
137 | def get_categoryname(cat_id):
138 | """ Returns a list of video category names for one category ID. """
139 | timestamp = time.time()
140 | cat_cache = cache('categories')
141 | cached = cat_cache.get(cat_id, {})
142 | if cached.get('updated', 0) > timestamp - g.lifespan:
143 | return cached.get('title', 'unknown')
144 | # call videoCategories API endpoint to retrieve title
145 | query = {'id': cat_id,
146 | 'part': 'snippet'}
147 | catinfo = call_gdata('videoCategories', query)
148 | try:
149 | for item in catinfo.get('items', []):
150 | title = item.get('snippet', {}).get('title', 'unknown')
151 | cat_cache[cat_id] = {'title':title, 'updated':timestamp}
152 | return title
153 | cat_cache[cat_id] = {'updated':timestamp}
154 | return 'unknown'
155 | except Exception:
156 | raise IOError("Error fetching category name for ID %s" % cat_id)
157 |
158 |
159 | def set_categories(categories):
160 | """ Take a dictionary mapping video category IDs to name and retrieval
161 | time. All items are stored into cache node 'videoCategories', but
162 | for the ones with a retrieval time too long ago, the v3 API is queried
163 | before.
164 | """
165 | timestamp = time.time()
166 | idlist = [cid for cid, item in categories.items()
167 | if item.get('updated', 0) < timestamp - g.lifespan]
168 | if len(idlist) > 0:
169 | query = {'id': ','.join(idlist),
170 | 'part': 'snippet'}
171 | catinfo = call_gdata('videoCategories', query)
172 | try:
173 | for item in catinfo.get('items', []):
174 | cid = item['id']
175 | title = item.get('snippet', {}).get('title', 'unknown')
176 | categories[cid] = {'title':title, 'updated':timestamp}
177 | except Exception:
178 | raise IOError("Error fetching category name for IDs %s" % idlist)
179 | cache('categories').update(categories)
180 |
181 |
182 | def load_cache(newcache):
183 | """Loads a dict into pafy's internal cache."""
184 | set_categories(newcache.get('categories', {}))
185 |
186 |
187 | def dump_cache():
188 | """Returns pafy's cache for storing by program."""
189 | return g.cache
190 |
191 |
192 | def set_api_key(key):
193 | """Sets the api key to be used with youtube."""
194 | g.api_key = key
195 |
--------------------------------------------------------------------------------
/pafy/playlist.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import re
3 | import json
4 | import itertools
5 |
6 | from . import g
7 | from .pafy import new, get_categoryname, call_gdata, fetch_decode
8 |
9 |
10 | if sys.version_info[:2] >= (3, 0):
11 | # pylint: disable=E0611,F0401,I0011
12 | from urllib.parse import parse_qs, urlparse
13 | pyver = 3
14 | else:
15 | from urlparse import parse_qs, urlparse
16 | pyver = 2
17 |
18 |
19 | def extract_playlist_id(playlist_url):
20 | # Normal playlists start with PL, Mixes start with RD + first video ID,
21 | # Liked videos start with LL, Uploads start with UU,
22 | # Favorites lists start with FL
23 | # Album playlists start with OL
24 | idregx = re.compile(r'((?:RD|PL|LL|UU|FL|OL)[-_0-9a-zA-Z]+)$')
25 |
26 | playlist_id = None
27 | if idregx.match(playlist_url):
28 | playlist_id = playlist_url # ID of video
29 |
30 | if '://' not in playlist_url:
31 | playlist_url = '//' + playlist_url
32 | parsedurl = urlparse(playlist_url)
33 | if parsedurl.netloc in ('youtube.com', 'www.youtube.com'):
34 | query = parse_qs(parsedurl.query)
35 | if 'list' in query and idregx.match(query['list'][0]):
36 | playlist_id = query['list'][0]
37 |
38 | return playlist_id
39 |
40 |
41 | def get_playlist(playlist_url, basic=False, gdata=False,
42 | size=False, callback=None):
43 | """ Return a dict containing Pafy objects from a YouTube Playlist.
44 |
45 | The returned Pafy objects are initialised using the arguments to
46 | get_playlist() in the manner documented for pafy.new()
47 |
48 | """
49 |
50 | playlist_id = extract_playlist_id(playlist_url)
51 |
52 | if not playlist_id:
53 | err = "Unrecognized playlist url: %s"
54 | raise ValueError(err % playlist_url)
55 |
56 | url = g.urls["playlist"] % playlist_id
57 |
58 | allinfo = fetch_decode(url) # unicode
59 | allinfo = json.loads(allinfo)
60 |
61 | # playlist specific metadata
62 | playlist = dict(
63 | playlist_id=playlist_id,
64 | likes=allinfo.get('likes'),
65 | title=allinfo.get('title'),
66 | author=allinfo.get('author'),
67 | dislikes=allinfo.get('dislikes'),
68 | description=allinfo.get('description'),
69 | items=[]
70 | )
71 |
72 | # playlist items specific metadata
73 | for v in allinfo['video']:
74 |
75 | vid_data = dict(
76 | added=v.get('added'),
77 | is_cc=v.get('is_cc'),
78 | is_hd=v.get('is_hd'),
79 | likes=v.get('likes'),
80 | title=v.get('title'),
81 | views=v.get('views'),
82 | rating=v.get('rating'),
83 | author=v.get('author'),
84 | user_id=v.get('user_id'),
85 | privacy=v.get('privacy'),
86 | start=v.get('start', 0.0),
87 | dislikes=v.get('dislikes'),
88 | duration=v.get('duration'),
89 | comments=v.get('comments'),
90 | keywords=v.get('keywords'),
91 | thumbnail=v.get('thumbnail'),
92 | cc_license=v.get('cc_license'),
93 | category_id=v.get('category_id'),
94 | description=v.get('description'),
95 | encrypted_id=v.get('encrypted_id'),
96 | time_created=v.get('time_created'),
97 | time_updated=v.get('time_updated'),
98 | length_seconds=v.get('length_seconds'),
99 | end=v.get('end', v.get('length_seconds'))
100 | )
101 |
102 | try:
103 | pafy_obj = new(vid_data['encrypted_id'],
104 | basic=basic,
105 | gdata=gdata,
106 | size=size,
107 | callback=callback)
108 |
109 | except IOError as e:
110 | if callback:
111 | callback("%s: %s" % (v['title'], e.message))
112 | continue
113 |
114 | pafy_obj.populate_from_playlist(vid_data)
115 | playlist['items'].append(dict(pafy=pafy_obj,
116 | playlist_meta=vid_data))
117 | if callback:
118 | callback("Added video: %s" % v['title'])
119 |
120 | return playlist
121 |
122 |
123 | def parseISO8591(duration):
124 | """ Parse ISO 8591 formated duration """
125 | regex = re.compile(r'PT((\d{1,3})H)?((\d{1,3})M)?((\d{1,2})S)?')
126 | if duration:
127 | duration = regex.findall(duration)
128 | if len(duration) > 0:
129 | _, hours, _, minutes, _, seconds = duration[0]
130 | duration = [seconds, minutes, hours]
131 | duration = [int(v) if len(v) > 0 else 0 for v in duration]
132 | duration = sum([60**p*v for p, v in enumerate(duration)])
133 | else:
134 | duration = 30
135 | else:
136 | duration = 30
137 | return duration
138 |
139 |
140 | class Playlist(object):
141 | def __init__(self, playlist_url, basic, gdata, size, callback):
142 | playlist_id = extract_playlist_id(playlist_url)
143 |
144 | if not playlist_id:
145 | err = "Unrecognized playlist url: %s"
146 | raise ValueError(err % playlist_url)
147 |
148 | self.plid = playlist_id
149 | self._title = None
150 | self._author = None
151 | self._description = None
152 | self._len = None
153 | self._thumbnail = None
154 | self._basic = basic
155 | self._gdata = gdata
156 | self._size = size
157 | self._callback = callback
158 | self._pageToken = None
159 | self._have_basic = False
160 | self._items = []
161 |
162 | @classmethod
163 | def from_dict(cls, pl, basic, gdata, size, callback):
164 | t = cls(pl['id'], basic, gdata, size, callback)
165 | t._title = pl['title']
166 | t._author = pl['author']
167 | t._description = pl['description']
168 | t._len = pl['len']
169 | t._thumbnail = pl['thumbnail']
170 | t._have_basic = True
171 | return t
172 |
173 | @classmethod
174 | def from_url(cls, url, basic, gdata, size, callback):
175 | t = cls(url, basic, gdata, size, callback)
176 | t._fetch_basic()
177 | return t
178 |
179 | @property
180 | def title(self):
181 | if not self._have_basic:
182 | self._fetch_basic()
183 |
184 | return self._title
185 |
186 | @property
187 | def author(self):
188 | if not self._have_basic:
189 | self._fetch_basic()
190 |
191 | return self._author
192 |
193 | @property
194 | def description(self):
195 | if not self._have_basic:
196 | self._fetch_basic()
197 |
198 | return self._description
199 |
200 | @property
201 | def thumbnail(self):
202 | if not self._have_basic:
203 | self._fetch_basic()
204 |
205 | return self._thumbnail
206 |
207 | def __len__(self):
208 | if not self._have_basic:
209 | self._fetch_basic()
210 |
211 | return self._len
212 |
213 | def __iter__(self):
214 | for i in self._items:
215 | yield i
216 |
217 | # playlist items specific metadata
218 | query = {'part': 'snippet',
219 | 'maxResults': 50,
220 | 'playlistId': self.plid}
221 |
222 | # Use -1 to represent having reached the last page
223 | while self._pageToken != -1:
224 | if self._pageToken:
225 | query['pageToken'] = self._pageToken
226 | playlistitems = call_gdata('playlistItems', query)
227 |
228 | query2 = {'part': 'contentDetails,snippet,statistics',
229 | 'maxResults': 50,
230 | 'id': ','.join(i['snippet']['resourceId']['videoId']
231 | for i in playlistitems['items'])}
232 | wdata = call_gdata('videos', query2)
233 |
234 | index = len(self._items)
235 | for v in wdata['items']:
236 | vid_data = dict_for_playlist(v)
237 |
238 | try:
239 | pafy_obj = new(v['id'],
240 | basic=False, gdata=False,
241 | size=self._size, callback=self._callback)
242 |
243 | except IOError as e:
244 | if self._callback:
245 | self._callback("%s: %s" % (v['title'], e.message))
246 | continue
247 |
248 | pafy_obj.populate_from_playlist(vid_data)
249 | self._items.append(pafy_obj)
250 | if self._callback:
251 | self._callback("Added video: %s" % vid_data['title'])
252 |
253 | self._pageToken = playlistitems.get('nextPageToken', -1)
254 | if self._pageToken == -1:
255 | self._len = len(self._items)
256 |
257 | # Do not yield until self._items and self._pageToken are set
258 | for i in self._items[index:]:
259 | if self._basic:
260 | i._fetch_basic()
261 | if self._gdata:
262 | i._fetch_gdata()
263 |
264 | yield i
265 |
266 | def __getitem__(self, index):
267 | if index < len(self._items):
268 | return self._items[index]
269 |
270 | try:
271 | return next(itertools.islice(self, index, None))
272 | except StopIteration:
273 | raise IndexError('index out of range')
274 |
275 | def __repr__(self):
276 | if not self._have_basic:
277 | self._fetch_basic()
278 |
279 | info = [("Type", "Playlist"),
280 | ("Title", self._title),
281 | ("Author", self._author),
282 | ("Description", self._description),
283 | ("Length", self.__len__())]
284 |
285 | nfo = "\n".join(["%s: %s" % i for i in info])
286 |
287 | return nfo.encode("utf8", "replace") if pyver == 2 else nfo
288 |
289 | def _fetch_basic(self):
290 | query = {'part': 'snippet, contentDetails',
291 | 'id': self.plid}
292 | allinfo = call_gdata('playlists', query)
293 |
294 | pl = allinfo['items'][0]
295 |
296 | self._title = pl['snippet']['title']
297 | self._author = pl['snippet']['channelTitle']
298 | self._description = pl['snippet']['description']
299 | self._len = pl['contentDetails']['itemCount']
300 | try:
301 | self._thumbnail = pl['snippet']['thumbnails']['standard']['url']
302 | except KeyError:
303 | self._thumbnail = None
304 | self._have_basic = True
305 |
306 |
307 | def get_playlist2(playlist_url, basic=False, gdata=False,
308 | size=False, callback=None):
309 | """ Return a Playlist object from a YouTube Playlist.
310 |
311 | The returned Pafy objects are initialised using the arguments to
312 | get_playlist() in the manner documented for pafy.new()
313 |
314 | """
315 |
316 | return Playlist.from_url(playlist_url, basic, gdata, size, callback)
317 |
318 |
319 | def dict_for_playlist(v):
320 | """Returns a dict which can be used to initialise Pafy Object for playlist
321 |
322 | """
323 |
324 | stats = v.get('statistics', {})
325 | vid_data = dict(
326 | title=v['snippet']['title'],
327 | author=v['snippet']['channelTitle'],
328 | thumbnail=v['snippet'].get('thumbnails', {})
329 | .get('default', {}).get('url'),
330 | description=v['snippet']['description'],
331 | length_seconds=parseISO8591(
332 | v['contentDetails']['duration']),
333 | category=get_categoryname(v['snippet']['categoryId']),
334 | views=stats.get('viewCount', 0),
335 | likes=stats.get('likeCount', 0),
336 | dislikes=stats.get('dislikeCount', 0),
337 | comments=stats.get('commentCount', 0),
338 | )
339 |
340 | return vid_data
341 |
--------------------------------------------------------------------------------
/pafy/util.py:
--------------------------------------------------------------------------------
1 | import json
2 | import sys
3 | import os
4 |
5 | if sys.version_info[:2] >= (3, 0):
6 | # pylint: disable=E0611,F0401,I0011
7 | from urllib.error import HTTPError
8 | from urllib.parse import urlencode
9 |
10 | else:
11 | from urllib2 import HTTPError
12 | from urllib import urlencode
13 |
14 | from . import g
15 |
16 |
17 | mswin = os.name == "nt"
18 | not_utf8_environment = mswin or (sys.stdout.encoding and
19 | "UTF-8" not in sys.stdout.encoding)
20 |
21 |
22 | class GdataError(Exception):
23 | """Gdata query failed."""
24 | pass
25 |
26 |
27 | def call_gdata(api, qs):
28 | """Make a request to the youtube gdata api."""
29 | qs = dict(qs)
30 | qs['key'] = g.api_key
31 | url = g.urls['gdata'] + api + '?' + urlencode(qs)
32 |
33 | try:
34 | data = g.opener.open(url).read().decode('utf-8')
35 | except HTTPError as e:
36 | try:
37 | errdata = e.file.read().decode()
38 | error = json.loads(errdata)['error']['message']
39 | errmsg = 'Youtube Error %d: %s' % (e.getcode(), error)
40 | except:
41 | errmsg = str(e)
42 | raise GdataError(errmsg)
43 |
44 | return json.loads(data)
45 |
46 |
47 | def utf8_replace(txt):
48 | """
49 | Replace unsupported characters in unicode string.
50 |
51 | :param txt: text to filter
52 | :type txt: str
53 | :returns: Unicode text without any characters unsupported by locale
54 | :rtype: str
55 | """
56 | sse = sys.stdout.encoding
57 | txt = txt.encode(sse, "replace").decode(sse)
58 | return txt
59 |
60 |
61 | def xenc(stuff):
62 | """ Replace unsupported characters. """
63 | return utf8_replace(stuff) if not_utf8_environment else stuff
64 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | python-telegram-bot==13.10
2 | requests==2.27.1
3 | urlextract==1.5.0
4 | urllib3==1.26.8
5 | youtube-dl==2021.12.17
6 | psycopg2==2.9.3
7 |
--------------------------------------------------------------------------------
/ytadllib.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | import requests
4 |
5 | import pafy
6 | from helper import get_vid_id, is_yt_url
7 |
8 | FILE_SIZE_LIMIT = 50000000
9 |
10 |
11 | class FileSizeExceeded(Exception):
12 | """File size limit exceeds the max telegram bot upload limit"""
13 |
14 | def __str__(self) -> str:
15 | return "File size above 50 MB"
16 |
17 |
18 | class FileDownloadError(Exception):
19 | """File was not downloaded or not found in the disk"""
20 |
21 | def __str__(self) -> str:
22 | return "Unable to download file"
23 |
24 |
25 | class UnableToDownload(Exception):
26 | def __str__(self) -> str:
27 | return "Unable to download file"
28 |
29 |
30 | class YTADL:
31 | """YoutubeAudioDl object to carry out download operation"""
32 |
33 | def __init__(self, url: str, url_only=False):
34 | self.vid_id = get_vid_id(url)
35 | if self.vid_id == "":
36 | raise ValueError("Invalid YouTube URL")
37 | self.url = "https://www.youtube.com/watch?v=" + self.vid_id
38 | if not is_yt_url(self.url):
39 | raise ValueError("Invalid YouTube URL")
40 | self.pafy_obj = None
41 | self.audio_stream = None
42 | self.size = None
43 | self.downloadable = None
44 | self.file_title = None
45 | self.file_ext = None
46 | self.filename = None
47 | self.audio_file = None
48 | self.thumbnail = None
49 | if not url_only:
50 | self.processor_url()
51 |
52 | def processor_url(self):
53 | """Starts processing link and gather meta infos"""
54 | try:
55 | self.pafy_obj = pafy.new(self.vid_id, size=True)
56 | self.audio_stream = self.pafy_obj.getbestaudio(preftype="m4a")
57 | self.size = self.audio_stream.get_filesize()
58 | self.downloadable = False
59 | if self.size > FILE_SIZE_LIMIT:
60 | print("Bot will not be able to send file above 50MB!")
61 | raise FileSizeExceeded
62 | self.downloadable = True
63 | self.file_title = (
64 | self.pafy_obj.title.replace("/", "_")
65 | .replace("<", "_")
66 | .replace(">", "_")
67 | )
68 | self.file_ext = self.audio_stream.extension
69 | self.filename = ""
70 | self.audio_file = None
71 | self.thumbnail = requests.get(self.pafy_obj.getbestthumb()).content
72 | except:
73 | raise UnableToDownload
74 |
75 | def download(self):
76 | """Downloads the audio file"""
77 | self.filename = self.audio_stream.download(quiet=True)
78 | if self.filename is None:
79 | self.filename = self.file_title + "." + self.file_ext
80 | try:
81 | self.audio_file = open(self.filename, "rb")
82 | except FileNotFoundError:
83 | print("File download failed!")
84 | raise FileDownloadError
85 |
86 | def __del__(self):
87 | try:
88 | if self.filename:
89 | os.remove(self.filename)
90 | except FileNotFoundError:
91 | pass
92 |
--------------------------------------------------------------------------------