pax_global_header00006660000000000000000000000064127372652020014520gustar00rootroot0000000000000052 comment=a9541cbff9b949bd273d3e8e54a6e53715589819 mps-youtube-0.2.7.1/000077500000000000000000000000001273726520200141565ustar00rootroot00000000000000mps-youtube-0.2.7.1/.gitignore000066400000000000000000000001661273726520200161510ustar00rootroot00000000000000*.py[cod] otherstuff/ pafy.py pafy vi.py MANIFEST .dev # Packages *.egg *.egg-info dist build eggs parts bin var tags mps-youtube-0.2.7.1/CHANGELOG000066400000000000000000000322501273726520200153720ustar00rootroot00000000000000Version 0.2.7.1 6 July 2016 Bug fixes: ---------- - Fix pickle error (@ids1024) (#503) - Install LICENSE, README.md, and CHANGELOG as package_data (@ids1024) - Update youtube-dl in py2exe build (@ids1024) ------------------------------------------------------------------------------- Version 0.2.7 27 June 2016 Features: --------- - Setting for default audio format (@nishanthkarthik) (#71 #466 #467) - Search history with `history` command (@kraetzin) (#344 #472) - Add syntax for repeating a track several times (@ghallak) (#437 #490) - Reverse command (@kraetzin) (#422 #473) - New `daurl ` command (@maricn) (#402) Enhancements: ------------- - Pass `--no-ytdl` to mpv (@ids1024) (#492) - Do not suppress mpv output in debug mode (@ids1024) - Print traceback in debug mode (@ids1024) - Speed up `checkupdate` by avoiding redirects (@ids1024) (#454) - Pass `--play-and-exit` if vlc is used as a player (@ids1024)` Bug fixes: ---------- - Fix error with python 3.3 (@ids1024) (#414) - Fix maxres bug (@ids1024) (#430) - Fix playlist cache bug in info command (@ids1024) (#468) - Fix usersearch argument error (@ids1024) (#469 #482) - Fix progress bar with new mpv versions (@ids1024) (#470 #485) - Fix syntax error with set command (@ids1024) (#419) - Fix syntax error with -w, -f, -a (@ids1024) (#420) - Fix clipboard support (@ids1024) (#461) - Fix mpv version checking on windows (@ids1024) (#397) - Add youtube_dl as dependency to Dockerfile (@PI-Victor) (#481) Internal api changes: --------------------- - The code is now split between several files (@ids1024) (#208) - Add initial content api, for now only used for comments (@ids1024) - Begin documenting internal apis using sphinx (@ids1024) ------------------------------------------------------------------------------- Version 0.2.6 1 January 2016 Features: - Add browserplay command to play video in browser (rjvani) (#355 #390) - Make url command support multiple url, and allow loading urls from file (paulnicolet) (#90 #391) - Make youtube id available to download command (ids1024) (#375) - Make pagination work properly for all lists of songs (local playlists, etc.) and make them all support the dump command (ids1024) (#201) - Show warning when adding duplicate track to playlist (BensonQiu) (#374 #383) Bug fixes: - Support playlists longer than 200 items (ids1024) (#192) - Make dapl download complete playlist (JKatzwinkel) (#294 #325) - Use pyperclip instead of xerox, fixing clipboard on linux (ids1024) (#311) - Fix error message on windows when video title has & (ids1024) (#321) - Run notifier command in background (hrnr) (#339) - Correctly handle -v option of play (TimoDritschler) (#352) - Make it possible to use a pager with --help (livingBEEF) (#349) - Allow mplayer for https streams if version is new enough (ids1024 Evidlo paddatrapper) (#315 #336 #317) - Use mpv.com instead of mpv.exe on windows (ids1024) Internal API changes: - Split code into several files (still a work in progress) (ids1024) (#208) - Use a python decorator to declare commands (ids1024) - Create a paginatesongs() function for handling the pagination and dump command uniformly as mentioned in features (#399) (ids1024) - Various others There have been various other small bug fixes and internal API changes. Use `git log v0.2.5..0.2.6` to see all of them. ------------------------------------------------------------------------------- Version 0.2.5 1 June 2015 - Support python 3 only (will not run with python 2) - Fix progress bar bug on Windows (#215, #223) - Fix bug in playlist search (#282) - Add user_order setting for separate order of user searches (lol768) (#277) - Add mix command (lol768) (#241, #283) - Add page count with total number of pages and current page (JKatzwinkel) - Fix UnicodeEncodeError on non-unicode terminals - Install .desktop in setup.py so it does not have to be installed manually required for Ubuntu sound menu support ------------------------------------------------------------------------------- Version 0.2.4 13 May 2015 - Fix issue with rate-limiting in "album" command (#217) - Implement support for MPRIS (hrnr and ids1024) (#191, #210) - Fix display issue with small terminal width (#215) - Use version 3 of the youtube api (JKatzwinkel, ids1024, and lol768) (#256) version 2 was disabled by youtube and no longer works ------------------------------------------------------------------------------- Version 0.2.3 17 February 2015 - Fix issue with opening old playlist file (#214) - Fix issue with "set" command - Metadata tagging of downloaded audio files requires ffmpeg or avconv early implementation, uses YouTube video title, needs more work ------------------------------------------------------------------------------- Version 0.2.2 14 February 2015 - Implement skip to previous track (ids1024) #175 #18 #14 Use > and < keys for next and prev track, q for quit (was ctrl-c) mpv / mplayer input.conf can be imported to use custom keymappings - Implement optional external download application (ids1024 and np1) #198 #132 enter "help dl-command" for more info - Show stream quality on status line (Brebiche38) #134 #163 - Implement support for creating Windows builds with py2exe (ids1024) #190 Use setup.py in py2exe branch - Fix progress bar for mpv 0.7 (ids1024) #155 #161 Resolves issues with playback looping - Fix mps-youtube does not run on FreeBSD (nivit) - Fix YouTube comments overshoot window size - Fix issue with audio remux when using `d` download command ------------------------------------------------------------------------------- Version 0.2.1 27 November 2014 [Bugfix] - Detect installed player on installation (#149) ------------------------------------------------------------------------------- Version 0.2.0 25 November 2014 - Auto detect terminal window size - Transcode audio to MP3 and other formats (requires ffmpeg or avconv) - Enabled da and dv commands for playlist search results - Added dapl and dvpl to download YouTube playlist audio/video by url/id to a separate subdirectory (mtahmed) #110 - Progress indicator now works with mpv (previously only mplayer) - Added option to show system notifications (Alex Nisnevich) #95 (can be used with libnotify - notify-send on linux) - Added overwrite true/false option for downloads (mtahmed) #93 (skips download if downloaded file already exists) - Added copy to clipboard feature #97 (requires python xerox module and xclip on linux or pywin32 on windows) - Remux audio downloads for better file compatibility (#135) [Bugfix] - Accept two-character length usernames (#99) [Bugfix] - Expand ~ character in download directory setting (punchagan) [Bugfix] - Don't truncate filename (YoussF) #122 [Bugfix] - Exit with correct status code ------------------------------------------------------------------------------- Version 0.01.46 May 18 2014 - Added c to view comments for a video (first 50 comments, no reply-comments) - Added feature to match album tracks using MusicBrainz (vixus0) To search albums, enter "album" optionally followed by album title - Custom formatted search result list using "set columns" command Optionally shows: rating, likes, dislikes, views, user, date, category and comments (number of) in search results - Added "set order " command for specifying search result ordering - Added "set console-width" for setting output width (default 80) - Added uploaded date in video info display (request #64) - Added likes / dislikes in video info display ------------------------------------------------------------------------------- Version 0.01.44 Mar 27 2014 - Added max_res config item for selecting video resolution (request 56) - Added window_size and window_pos for selecting size / position of player - Improved handling of playlist file read/write (issue 66) - Revert to lower quality on unavailable/corrupt streams (issue 65) - Enabled multiple invocation commands (comma separated) (request 61) Eg: mpsyt //the doors, 1, shuffle, 1-3-a, quit - Improved url caching - Added shuffle command (no arguments, shuffles displayed items) - Handle 'video not available in your country' during multiple downloads - Fix UnicodeDecodeError in mplayer progress parsing (issue 58) - Fix UnicodeEncodeError under Windows with Python 2.7 (issue 59) ------------------------------------------------------------------------------- Version 0.01.41 Mar 14 2014 - new configuration option, max_results - sets number of returned results (1 - 50) - pl now accepts YouTube playlist id's as well as url's - Fix error in download of .m4a files when mplayer is set as player. - Added option for downloading multiple items eg. da 1-4 or dv 3,5,6 ------------------------------------------------------------------------------- Version 0.01.40 Mar 11 2014 - Bugfix release, corrects UnicodeDecode error ------------------------------------------------------------------------------- Version 0.01.39 Mar 10 2014 Features: - Added time / progress bar indicator. Big thanks to Thomas LÉVEIL! (mplayer only) - Added command to retrieve YouTube user playlists; userpl {username} or upl {username} - Added command to retrieve related videos; r{number} - playurl now respects -f, -w, and -a flags (fullscreen, window or audio) - Implemented input history persistence (uses readline, linux only) - Implemented non-interactive mode for command line invocation with playurl - Implemented less interactive mode for command line invocation with dlurl Changes: - // or .. prefix to search for YouTube playlists (was plsearch / pls) Help / Documentation: - help will show relevant help topic page e.g., help dlurl - Documented configuration options ("set" commands) - Re-authored some help categories - Changed help to help , removed menu from help pages Bugfixes - mplayer / mpv window now displays content title instead of url - Fixed issue with Python3 terminal output on playback ctrl-c interrupt - Fixed issue with debug mode not logging to file - Changed year format in YT playlist result screen to last two digits. (Under some environments it was showing 4-digit years) ------------------------------------------------------------------------------- Version 0.01.38 Mar 5 2014 - Added i for YT playlist information display - Added stream url preloading for first item of opened YT playlists - Added last-updated date to YT playlist search result list - Fixed UnicodeError when run in non-UTF8 environments - Added more verbosity to --version command line option - Added --debug command line option (logs to /mpsyt.log) ------------------------------------------------------------------------------- Version 0.01.37 Mar 2 2014 - Excluded YouTube paid videos from search results - Added "pl " command to open YouTube playlist url - Added "plsearch " command to search for YouTube playlists - Added "dump" command to show entire YT playlist (useful for saving locally) - Added "undump" command to undo dump (re-paginates YT playlist items) - Added "u " command to list uploads by uploader of - Added "url " command for retrieving a specific YT video - Added "playurl " command for directly playing a YT video - Added "dlurl " command for directly downloading a YT video - Changed command to list user uploads, new syntax is "user " - Added mpsyt --version flag for printing version info - Added mpsyt --help flag for printing help text - Fixed indentation bug when scrolling through readline history ------------------------------------------------------------------------------- Version 0.01.36 Feb 27 2014 - Wait for preloading item if it is selected while preloading - Fix bug where -a audio override is sometimes ignored ------------------------------------------------------------------------------- Version 0.01.35 Feb 25 2014 - Faster opening of audio streams with mpv (--demuxer-lavf-o=fflags=+ignid argument added for m4a streams) - Faster opening of audio streams with mplayer (reject m4a streams, prefer ogg streams) (fallback to video stream and suppress video when no ogg available) ------------------------------------------------------------------------------- Version 0.01.34 Feb 22 2014 - Fix bug: fails when no playlist file exists ------------------------------------------------------------------------------- Version 0.01.33 Feb 22 2014 - Changed structure of playlist file. Now consumes KB's rather than MB's - Same playlist file shared between Python2.7+ and Python3+ (was separate) - Improved display alignment for Russian text - Preload of url fails silently on unobtainable items (previously crashed) - Added prompt to mux audio on m4v download (experimental) ------------------------------------------------------------------------------- Version 0.01.32 Feb 17 2014 - Improve display alignment with certain unicode characters - Fix crash when using 'set' command on Windows (thomasleveil) ------------------------------------------------------------------------------- Version 0.01.31 Feb 13 2014 - Migrate location of config directory to reflect new name (~/.config/mps-youtube) mps-youtube-0.2.7.1/CONTRIBUTING000066400000000000000000000013161273726520200160110ustar00rootroot00000000000000Contributions are very much appreciated! Pull requests should be based on and submitted to the "develop" branch. Please raise an issue to discuss what you plan to implement or change before you start if it is going to involve a lot of work on your part. Please keep pull requests specific, do not make many disparate changes or new features in one request. A separate pull request for each feature change is preferred. Please ensure your changes work in Python 3.3+ and Windows. Code conventions: ----------------- Maximum line length is 80 characters Follow the line-spacing style that is already in place. Ensure all functions and classes have a PEP257 compliant docstring and the code is PEP8 compliant. mps-youtube-0.2.7.1/Dockerfile000066400000000000000000000003421273726520200161470ustar00rootroot00000000000000FROM python:latest MAINTAINER Justin Garrison RUN apt-get update && apt-get install -y mplayer mpv RUN pip install mps-youtube youtube-dl RUN apt-get clean && apt-get purge ENTRYPOINT ["mpsyt"] mps-youtube-0.2.7.1/LICENSE000066400000000000000000001044671273726520200151770ustar00rootroot00000000000000GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. {http://fsf.org/} Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. {one line to give the program's name and a brief idea of what it does.} Copyright (C) {year} {name of author} This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see {http://www.gnu.org/licenses/}. The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read {http://www.gnu.org/philosophy/why-not-lgpl.html}. mps-youtube-0.2.7.1/MANIFEST.in000066400000000000000000000001211273726520200157060ustar00rootroot00000000000000include mps-youtube.desktop include LICENSE include README.rst include CHANGELOG mps-youtube-0.2.7.1/README.rst000066400000000000000000000116341273726520200156520ustar00rootroot00000000000000mps-youtube =========== .. image:: https://img.shields.io/pypi/v/mps-youtube.svg :target: https://pypi.python.org/pypi/mps-youtube .. image:: https://img.shields.io/pypi/dm/mps-youtube.svg :target: https://pypi.python.org/pypi/mps-youtube .. image:: https://img.shields.io/pypi/wheel/mps-youtube.svg :target: http://pythonwheels.com/ :alt: Wheel Status Features -------- - Search and play audio/video from YouTube - Search tracks of albums by album title - Search and import YouTube playlists - Create and save local playlists - Download audio/video - Convert to mp3 & other formats (requires ffmpeg or avconv) - View video comments - Works with Python 3.x - Works with Windows, Linux and Mac OS X - Requires mplayer or mpv This project is based on `mps `_, a terminal based program to search, stream and download music. This implementation uses YouTube as a source of content and can play and download video as well as audio. The `pafy `_ library handles interfacing with YouTube. `FAQ / Troubleshooting common issues `_ Screenshots ----------- Search ~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/std-search.png A standard search is performed by entering ``/`` followed by search terms. Local Playlists ~~~~~~~~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/local-playlists.png Search result items can easily be stored in local playlists. YouTube Playlists ~~~~~~~~~~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/playlist-search.png YouTube playlists can be searched and played or saved as local playlists. Download ~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/download.png Content can be downloaded in various formats and resolutions. Comments ~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/comments.png A basic comments browser is available to view YouTube user comments. Music Album Matching ~~~~~~~~~~~~~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/album-1.png .. image:: http://np1.github.io/mpsyt-images2/album-2.png An album title can be specified and mps-youtube will attempt to find matches for each track of the album, based on title and duration. Type ``help search`` for more info. Customisation ~~~~~~~~~~~~~ .. image:: http://np1.github.io/mpsyt-images2/customisation2.png Search results can be customised to display additional fields and ordered by various criteria. This configuration was set up using the following commands:: set order views set columns user:14 date comments rating likes dislikes category:9 views Type ``help config`` for help on configuration options Installation ------------ Using `pip `_:: [sudo] pip3 install mps-youtube Installing youtube_dl is highly recommended:: [sudo] pip3 install youtube_dl Additional Mac OS X installation notes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Install mplayer with `MacPorts `_:: sudo port install MPlayer Or with `Homebrew `_:: brew install mplayer Additional Windows installation notes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ As an alternative to installing with pip, there is a standalone binary available. Go to `Releases `_ and download mpsyt-VERSION.exe under downloads for the latest release. Install the python `colorama `_ module to get colors (optional):: pip3 install colorama Download mplayer for your CPU type from the "Build Selection table" `here `_. Extract the ``mplayer.exe`` file, saving it to the folder that ``mpsyt.exe`` resides in (usually ``C:\PythonXX\Scripts\``) or to a folder in the system path. Alternatively to mplayer, use ``mpv.exe`` which can be downloaded from: http://mpv.io/installation/ Run via Docker container ~~~~~~~~~~~~~~~~~~~~~~~~ Using `Docker `_, run with:: sudo docker run -v /dev/snd:/dev/snd -it --rm --privileged --name mpsyt mpsyt Additional Docker notes ~~~~~~~~~~~~~~~~~~~~~~~ If you would like to locally build the container you can run the following steps Check out this repo:: git clone https://github.com/np1/mps-youtube.git Enter the directory and run docker build:: cd mps-youtube sudo docker build -t mpsyt . Now run the container interactively with:: sudo docker run -v /dev/snd:/dev/snd -it --rm --privileged --name mpsyt mpsyt In order to have access to the local sound device (/dev/snd) the container needs to be privileged. Upgrading --------- Upgrade pip installation:: [sudo] pip3 install mps-youtube --upgrade Usage ----- mps-youtube is run on the command line using the command:: mpsyt Enter ``h`` from within the program for help. IRC --- An IRC channel `#mps-youtube` for the project is available on Freenode (chat.freenode.net:6697). mps-youtube-0.2.7.1/VERSION000066400000000000000000000001061273726520200152230ustar00rootroot00000000000000# This file is used by clients to check for updates version 0.2.7.1 mps-youtube-0.2.7.1/doc/000077500000000000000000000000001273726520200147235ustar00rootroot00000000000000mps-youtube-0.2.7.1/doc/conf.py000066400000000000000000000265221273726520200162310ustar00rootroot00000000000000#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # mps_youtube documentation build configuration file, created by # sphinx-quickstart on Mon Apr 18 17:35:31 2016. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys import os import sphinx_rtd_theme # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.todo', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = 'mps_youtube' copyright = '2016, mps-youtube developers' author = 'mps-youtube developers' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = '' # The full version, including alpha/beta/rc tags. release = '' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = 'en' # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. #html_theme = 'alabaster' html_theme = "sphinx_rtd_theme" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # The name for this set of Sphinx documents. # " v documentation" by default. #html_title = 'mps_youtube v' # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (relative to this directory) to use as a favicon of # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not None, a 'Last updated on:' timestamp is inserted at every page # bottom, using the given strftime format. # The empty string is equivalent to '%b %d, %Y'. #html_last_updated_fmt = None # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Language to be used for generating the HTML full-text search index. # Sphinx supports the following languages: # 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' # 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr', 'zh' #html_search_language = 'en' # A dictionary with options for the search language support, empty by default. # 'ja' uses this config value. # 'zh' user can custom change `jieba` dictionary path. #html_search_options = {'type': 'default'} # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. #html_search_scorer = 'scorer.js' # Output file base name for HTML help builder. htmlhelp_basename = 'mps_youtubedoc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', # Latex figure (float) alignment #'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'mps_youtube.tex', 'mps\\_youtube Documentation', 'Author', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'mps_youtube', 'mps_youtube Documentation', [author], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'mps_youtube', 'mps_youtube Documentation', author, 'mps_youtube', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False # -- Options for Epub output ---------------------------------------------- # Bibliographic Dublin Core info. epub_title = project epub_author = author epub_publisher = author epub_copyright = copyright # The basename for the epub file. It defaults to the project name. #epub_basename = project # The HTML theme for the epub output. Since the default themes are not # optimized for small screen space, using the same theme for HTML and epub # output is usually not wise. This defaults to 'epub', a theme designed to save # visual space. #epub_theme = 'epub' # The language of the text. It defaults to the language option # or 'en' if the language is not set. #epub_language = '' # The scheme of the identifier. Typical schemes are ISBN or URL. #epub_scheme = '' # The unique identifier of the text. This can be a ISBN number # or the project homepage. #epub_identifier = '' # A unique identification for the text. #epub_uid = '' # A tuple containing the cover image and cover page html template filenames. #epub_cover = () # A sequence of (type, uri, title) tuples for the guide element of content.opf. #epub_guide = () # HTML files that should be inserted before the pages created by sphinx. # The format is a list of tuples containing the path and title. #epub_pre_files = [] # HTML files that should be inserted after the pages created by sphinx. # The format is a list of tuples containing the path and title. #epub_post_files = [] # A list of files that should not be packed into the epub file. epub_exclude_files = ['search.html'] # The depth of the table of contents in toc.ncx. #epub_tocdepth = 3 # Allow duplicate toc entries. #epub_tocdup = True # Choose between 'default' and 'includehidden'. #epub_tocscope = 'default' # Fix unsupported image types using the Pillow. #epub_fix_images = False # Scale large images. #epub_max_image_width = 0 # How to display URL addresses: 'footnote', 'no', or 'inline'. #epub_show_urls = 'inline' # If false, no index is generated. #epub_use_index = True mps-youtube-0.2.7.1/doc/index.rst000066400000000000000000000006501273726520200165650ustar00rootroot00000000000000.. mps_youtube documentation master file, created by sphinx-quickstart on Mon Apr 18 17:35:31 2016. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. API Documentation for mps_youtube ================================= Contents: .. toctree:: mps_youtube Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` mps-youtube-0.2.7.1/doc/modules.rst000066400000000000000000000001061273726520200171220ustar00rootroot00000000000000mps_youtube =========== .. toctree:: :maxdepth: 4 mps_youtube mps-youtube-0.2.7.1/doc/mps_youtube.c.rst000066400000000000000000000002021273726520200202430ustar00rootroot00000000000000mps_youtube.c module ==================== .. automodule:: mps_youtube.c :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.cache.rst000066400000000000000000000002161273726520200210710ustar00rootroot00000000000000mps_youtube.cache module ======================== .. automodule:: mps_youtube.cache :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.album_search.rst000066400000000000000000000002761273726520200242610ustar00rootroot00000000000000mps_youtube.commands.album_search module ======================================== .. automodule:: mps_youtube.commands.album_search :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.config.rst000066400000000000000000000002541273726520200230750ustar00rootroot00000000000000mps_youtube.commands.config module ================================== .. automodule:: mps_youtube.commands.config :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.download.rst000066400000000000000000000002621273726520200234360ustar00rootroot00000000000000mps_youtube.commands.download module ==================================== .. automodule:: mps_youtube.commands.download :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.local_playlist.rst000066400000000000000000000003041273726520200246370ustar00rootroot00000000000000mps_youtube.commands.local_playlist module ========================================== .. automodule:: mps_youtube.commands.local_playlist :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.misc.rst000066400000000000000000000002461273726520200225640ustar00rootroot00000000000000mps_youtube.commands.misc module ================================ .. automodule:: mps_youtube.commands.misc :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.play.rst000066400000000000000000000002461273726520200225760ustar00rootroot00000000000000mps_youtube.commands.play module ================================ .. automodule:: mps_youtube.commands.play :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.rst000066400000000000000000000007461273726520200216370ustar00rootroot00000000000000mps_youtube.commands package ============================ Submodules ---------- .. toctree:: mps_youtube.commands.album_search mps_youtube.commands.config mps_youtube.commands.download mps_youtube.commands.local_playlist mps_youtube.commands.misc mps_youtube.commands.play mps_youtube.commands.search mps_youtube.commands.songlist Module contents --------------- .. automodule:: mps_youtube.commands :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.search.rst000066400000000000000000000002541273726520200230750ustar00rootroot00000000000000mps_youtube.commands.search module ================================== .. automodule:: mps_youtube.commands.search :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.commands.songlist.rst000066400000000000000000000002621273726520200234710ustar00rootroot00000000000000mps_youtube.commands.songlist module ==================================== .. automodule:: mps_youtube.commands.songlist :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.config.rst000066400000000000000000000002211273726520200212670ustar00rootroot00000000000000mps_youtube.config module ========================= .. automodule:: mps_youtube.config :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.content.rst000066400000000000000000000002241273726520200214770ustar00rootroot00000000000000mps_youtube.content module ========================== .. automodule:: mps_youtube.content :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.g.rst000066400000000000000000000002021273726520200202470ustar00rootroot00000000000000mps_youtube.g module ==================== .. automodule:: mps_youtube.g :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.helptext.rst000066400000000000000000000002271273726520200216650ustar00rootroot00000000000000mps_youtube.helptext module =========================== .. automodule:: mps_youtube.helptext :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.history.rst000066400000000000000000000002241273726520200215260ustar00rootroot00000000000000mps_youtube.history module ========================== .. automodule:: mps_youtube.history :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.init.rst000066400000000000000000000002131273726520200207660ustar00rootroot00000000000000mps_youtube.init module ======================= .. automodule:: mps_youtube.init :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.main.rst000066400000000000000000000002131273726520200207470ustar00rootroot00000000000000mps_youtube.main module ======================= .. automodule:: mps_youtube.main :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.mpris.rst000066400000000000000000000002161273726520200211600ustar00rootroot00000000000000mps_youtube.mpris module ======================== .. automodule:: mps_youtube.mpris :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.paths.rst000066400000000000000000000002161273726520200211450ustar00rootroot00000000000000mps_youtube.paths module ======================== .. automodule:: mps_youtube.paths :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.player.rst000066400000000000000000000002211273726520200213160ustar00rootroot00000000000000mps_youtube.player module ========================= .. automodule:: mps_youtube.player :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.playlist.rst000066400000000000000000000002271273726520200216710ustar00rootroot00000000000000mps_youtube.playlist module =========================== .. automodule:: mps_youtube.playlist :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.playlists.rst000066400000000000000000000002321273726520200220500ustar00rootroot00000000000000mps_youtube.playlists module ============================ .. automodule:: mps_youtube.playlists :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.rst000066400000000000000000000012171273726520200200310ustar00rootroot00000000000000mps_youtube package =================== Subpackages ----------- .. toctree:: mps_youtube.commands Submodules ---------- .. toctree:: mps_youtube.c mps_youtube.cache mps_youtube.config mps_youtube.content mps_youtube.g mps_youtube.helptext mps_youtube.history mps_youtube.init mps_youtube.main mps_youtube.mpris mps_youtube.paths mps_youtube.player mps_youtube.playlist mps_youtube.playlists mps_youtube.screen mps_youtube.streams mps_youtube.terminalsize mps_youtube.util Module contents --------------- .. automodule:: mps_youtube :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.screen.rst000066400000000000000000000002211273726520200213010ustar00rootroot00000000000000mps_youtube.screen module ========================= .. automodule:: mps_youtube.screen :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.streams.rst000066400000000000000000000002241273726520200215030ustar00rootroot00000000000000mps_youtube.streams module ========================== .. automodule:: mps_youtube.streams :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.terminalsize.rst000066400000000000000000000002431273726520200225340ustar00rootroot00000000000000mps_youtube.terminalsize module =============================== .. automodule:: mps_youtube.terminalsize :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/doc/mps_youtube.util.rst000066400000000000000000000002131273726520200210000ustar00rootroot00000000000000mps_youtube.util module ======================= .. automodule:: mps_youtube.util :members: :undoc-members: :show-inheritance: mps-youtube-0.2.7.1/mps-youtube.desktop000066400000000000000000000003551273726520200200450ustar00rootroot00000000000000[Desktop Entry] Name=mps-youtube GenericName=Music Player Keywords=Audio;Song;Podcast;Playlist;youtube.com; Exec=mpsyt %U Terminal=true Icon=terminal Type=Application Categories=AudioVideo;Audio;Player; StartupNotify=true NoDisplay=true mps-youtube-0.2.7.1/mps_youtube/000077500000000000000000000000001273726520200165315ustar00rootroot00000000000000mps-youtube-0.2.7.1/mps_youtube/__init__.py000066400000000000000000000003051273726520200206400ustar00rootroot00000000000000__version__ = "0.2.7.1" __notes__ = "released 6 July 2016" __author__ = "np1" __license__ = "GPLv3" __url__ = "http://github.com/np1/mps-youtube" from . import init init.init() from . import main mps-youtube-0.2.7.1/mps_youtube/c.py000066400000000000000000000013121273726520200173220ustar00rootroot00000000000000""" Module for holding colour code values. """ import os import re import sys if sys.stdout.isatty(): white = "\x1b[%sm" % 0 ul = "\x1b[%sm" * 3 % (2, 4, 33) cols = ["\x1b[%sm" % n for n in range(91, 96)] red, green, yellow, blue, pink = cols else: ul = red = green = yellow = blue = pink = white = "" r, g, y, b, p, w = red, green, yellow, blue, pink, white ansirx = re.compile(r'\x1b\[\d*m', re.UNICODE) def c(colour, text): """ Return coloured text. """ colours = {'r': r, 'g': g, 'y': y, 'b':b, 'p':p} return colours[colour] + text + w def charcount(s): """ Return number of characters in string, with ANSI color codes excluded. """ return len(ansirx.sub('', s)) mps-youtube-0.2.7.1/mps_youtube/cache.py000066400000000000000000000025231273726520200201500ustar00rootroot00000000000000import os import pickle import pafy from . import g, c, streams from .util import dbg # Updated every time incompatible changes are made to cache format, # So old cache can be dropped CACHE_VERSION = 1 def load(): """ Import cache file. """ if os.path.isfile(g.CACHEFILE): try: with open(g.CACHEFILE, "rb") as cf: cached = pickle.load(cf) # Note: will be none for mpsyt 0.2.5 or earlier version = cached.get('version') if 'streams' in cached: if version and version >= 1: g.streams = cached['streams'] g.username_query_cache = cached['userdata'] else: g.streams = cached if 'pafy' in cached: pafy.load_cache(cached['pafy']) dbg(c.g + "%s cached streams imported%s", str(len(g.streams)), c.w) except (EOFError, IOError): dbg(c.r + "Cache file failed to open" + c.w) streams.prune() def save(): """ Save stream cache. """ caches = dict( version=CACHE_VERSION, streams=g.streams, userdata=g.username_query_cache, pafy=pafy.dump_cache()) with open(g.CACHEFILE, "wb") as cf: pickle.dump(caches, cf, protocol=2) dbg(c.p + "saved cache file: " + g.CACHEFILE + c.w) mps-youtube-0.2.7.1/mps_youtube/commands/000077500000000000000000000000001273726520200203325ustar00rootroot00000000000000mps-youtube-0.2.7.1/mps_youtube/commands/__init__.py000066400000000000000000000012211273726520200224370ustar00rootroot00000000000000import collections import re from .. import g Command = collections.namedtuple('Command', 'regex category usage function') # input types WORD = r'[^\W\d][-\w\s]{,100}' RS = r'(?:(?:repeat|shuffle|-[avfw])\s*)' PL = r'\S*?((?:RD|PL)[-_0-9a-zA-Z]+)\s*' def command(regex): """ Decorator to register an mps-youtube command. """ def decorator(function): cmd = Command(re.compile(regex), None, None, function) g.commands.append(cmd) return function return decorator # Placed at bottom to deal with cyclic imports from . import download, search, album_search, misc, config, local_playlist from . import play, songlist mps-youtube-0.2.7.1/mps_youtube/commands/album_search.py000066400000000000000000000223631273726520200233370ustar00rootroot00000000000000import re import time import difflib from urllib.request import build_opener from urllib.error import HTTPError, URLError from urllib.parse import urlencode from xml.etree import ElementTree as ET import pafy from .. import c, g, screen, __version__, __url__, content, config, util from . import command from .songlist import paginatesongs from .search import generate_search_qs, get_tracks_from_json def show_message(message, col=c.r, update=False): """ Show message using col, update screen if required. """ g.content = content.generate_songlist_display() g.message = col + message + c.w if update: screen.update() def _do_query(url, query, err='query failed', report=False): """ Perform http request using mpsyt user agent header. if report is True, return whether response is from memo """ # create url opener ua = "mps-youtube/%s ( %s )" % (__version__, __url__) mpsyt_opener = build_opener() mpsyt_opener.addheaders = [('User-agent', ua)] # convert query to sorted list of tuples (needed for consistent url_memo) query = [(k, query[k]) for k in sorted(query.keys())] url = "%s?%s" % (url, urlencode(query)) try: wdata = mpsyt_opener.open(url).read().decode() except (URLError, HTTPError) as e: g.message = "%s: %s (%s)" % (err, e, url) g.content = content.logo(c.r) return None if not report else (None, False) return wdata if not report else (wdata, False) def _best_song_match(songs, title, duration): """ Select best matching song based on title, length. Score from 0 to 1 where 1 is best. """ # pylint: disable=R0914 seqmatch = difflib.SequenceMatcher def variance(a, b): """ Return difference ratio. """ return float(abs(a - b)) / max(a, b) candidates = [] ignore = "music video lyrics new lyrics video audio".split() extra = "official original vevo".split() for song in songs: dur, tit = int(song.length), song.title util.dbg("Title: %s, Duration: %s", tit, dur) for word in extra: if word in tit.lower() and word not in title.lower(): pattern = re.compile(word, re.I) tit = pattern.sub("", tit) for word in ignore: if word in tit.lower() and word not in title.lower(): pattern = re.compile(word, re.I) tit = pattern.sub("", tit) replacechars = re.compile(r"[\]\[\)\(\-]") tit = replacechars.sub(" ", tit) multiple_spaces = re.compile(r"(\s)(\s*)") tit = multiple_spaces.sub(r"\1", tit) title_score = seqmatch(None, title.lower(), tit.lower()).ratio() duration_score = 1 - variance(duration, dur) util.dbg("Title score: %s, Duration score: %s", title_score, duration_score) # apply weightings score = duration_score * .5 + title_score * .5 candidates.append((score, song)) best_score, best_song = max(candidates, key=lambda x: x[0]) percent_score = int(100 * best_score) return best_song, percent_score def _match_tracks(artist, title, mb_tracks): """ Match list of tracks in mb_tracks by performing multiple searches. """ # pylint: disable=R0914 util.dbg("artists is %s", artist) util.dbg("title is %s", title) title_artist_str = c.g + title + c.w, c.g + artist + c.w util.xprint("\nSearching for %s by %s\n\n" % title_artist_str) def dtime(x): """ Format time to M:S. """ return time.strftime('%M:%S', time.gmtime(int(x))) # do matching for track in mb_tracks: ttitle = track['title'] length = track['length'] util.xprint("Search : %s%s - %s%s - %s" % (c.y, artist, ttitle, c.w, dtime(length))) q = "%s %s" % (artist, ttitle) w = q = ttitle if artist == "Various Artists" else q query = generate_search_qs(w, 0) util.dbg(query) # perform fetch wdata = pafy.call_gdata('search', query) results = get_tracks_from_json(wdata) if not results: util.xprint(c.r + "Nothing matched :(\n" + c.w) continue s, score = _best_song_match(results, artist + " " + ttitle, length) cc = c.g if score > 85 else c.y cc = c.r if score < 75 else cc util.xprint("Matched: %s%s%s - %s \n[%sMatch confidence: " "%s%s]\n" % (c.y, s.title, c.w, util.fmt_time(s.length), cc, score, c.w)) yield s def _get_mb_tracks(albumid): """ Get track listing from MusicBraiz by album id. """ ns = {'mb': 'http://musicbrainz.org/ns/mmd-2.0#'} url = "http://musicbrainz.org/ws/2/release/" + albumid query = {"inc": "recordings"} wdata = _do_query(url, query, err='album search error') if not wdata: return None root = ET.fromstring(wdata) tlist = root.find("./mb:release/mb:medium-list/mb:medium/mb:track-list", namespaces=ns) mb_songs = tlist.findall("mb:track", namespaces=ns) tracks = [] path = "./mb:recording/mb:" for track in mb_songs: try: title, length, rawlength = "unknown", 0, 0 title = track.find(path + "title", namespaces=ns).text rawlength = track.find(path + "length", namespaces=ns).text length = int(round(float(rawlength) / 1000)) except (ValueError, AttributeError): util.xprint("not found") tracks.append(dict(title=title, length=length, rawlength=rawlength)) return tracks def _get_mb_album(albumname, **kwa): """ Return artist, album title and track count from MusicBrainz. """ url = "http://musicbrainz.org/ws/2/release/" qargs = dict( release='"%s"' % albumname, primarytype=kwa.get("primarytype", "album"), status=kwa.get("status", "official")) qargs.update({k: '"%s"' % v for k, v in kwa.items()}) qargs = ["%s:%s" % item for item in qargs.items()] qargs = {"query": " AND ".join(qargs)} g.message = "Album search for '%s%s%s'" % (c.y, albumname, c.w) wdata = _do_query(url, qargs) if not wdata: return None ns = {'mb': 'http://musicbrainz.org/ns/mmd-2.0#'} root = ET.fromstring(wdata) rlist = root.find("mb:release-list", namespaces=ns) if int(rlist.get('count')) == 0: return None album = rlist.find("mb:release", namespaces=ns) artist = album.find("./mb:artist-credit/mb:name-credit/mb:artist", namespaces=ns).find("mb:name", namespaces=ns).text title = album.find("mb:title", namespaces=ns).text aid = album.get('id') return dict(artist=artist, title=title, aid=aid) @command(r'album\s*(.{0,500})') def search_album(term): """Search for albums. """ # pylint: disable=R0914,R0912 if not term: show_message("Enter album name:", c.g, update=True) term = input("> ") if not term or len(term) < 2: g.message = c.r + "Not enough input!" + c.w g.content = content.generate_songlist_display() return album = _get_mb_album(term) if not album: show_message("Album '%s' not found!" % term) return out = "'%s' by %s%s%s\n\n" % (album['title'], c.g, album['artist'], c.w) out += ("[Enter] to continue, [q] to abort, or enter artist name for:\n" " %s" % (c.y + term + c.w + "\n")) prompt = "Artist? [%s] > " % album['artist'] util.xprint(prompt, end="") artistentry = input().strip() if artistentry: if artistentry == "q": show_message("Album search abandoned!") return album = _get_mb_album(term, artist=artistentry) if not album: show_message("Album '%s' by '%s' not found!" % (term, artistentry)) return title, artist = album['title'], album['artist'] mb_tracks = _get_mb_tracks(album['aid']) if not mb_tracks: show_message("Album '%s' by '%s' has 0 tracks!" % (title, artist)) return msg = "%s%s%s by %s%s%s\n\n" % (c.g, title, c.w, c.g, artist, c.w) msg += "Enter to begin matching or [q] to abort" g.message = msg g.content = "Tracks:\n" for n, track in enumerate(mb_tracks, 1): g.content += "%02s %s" % (n, track['title']) g.content += "\n" screen.update() entry = input("Continue? [Enter] > ") if entry == "": pass else: show_message("Album search abandoned!") return songs = [] screen.clear() itt = _match_tracks(artist, title, mb_tracks) stash = config.SEARCH_MUSIC.get, config.ORDER.get config.SEARCH_MUSIC.value = True config.ORDER.value = "relevance" try: songs.extend(itt) except KeyboardInterrupt: util.xprint("%sHalted!%s" % (c.r, c.w)) finally: config.SEARCH_MUSIC.value, config.ORDER.value = stash if songs: util.xprint("\n%s / %s songs matched" % (len(songs), len(mb_tracks))) input("Press Enter to continue") msg = "Contents of album %s%s - %s%s %s(%d/%d)%s:" % ( c.y, artist, title, c.w, c.b, len(songs), len(mb_tracks), c.w) failmsg = "Found no album tracks for %s%s%s" % (c.y, title, c.w) paginatesongs(songs, msg=msg, failmsg=failmsg) mps-youtube-0.2.7.1/mps_youtube/commands/config.py000066400000000000000000000043721273726520200221570ustar00rootroot00000000000000from .. import g, c, config, util from . import command @command(r'set|showconfig') def showconfig(): """ Dump config data. """ width = util.getxy().width width -= 30 s = " %s%-17s%s : %s\n" out = " %s%-17s %s%s%s\n" % (c.ul, "Key", "Value", " " * width, c.w) for setting in config: val = config[setting] # don't show player specific settings if unknown player if not util.is_known_player(config.PLAYER.get) and \ val.require_known_player: continue # don't show max_results if auto determined if g.detectable_size and setting == "MAX_RESULTS": continue if g.detectable_size and setting == "CONSOLE_WIDTH": continue out += s % (c.g, setting.lower(), c.w, val.display) g.content = out g.message = "Enter %sset %s to change\n" % (c.g, c.w) g.message += "Enter %sset all default%s to reset all" % (c.g, c.w) @command(r'set\s+([-\w]+)\s*(.*)') def setconfig(key, val): """ Set configuration variable. """ key = key.replace("-", "_") if key.upper() == "ALL" and val.upper() == "DEFAULT": for ci in config: config[ci].value = config[ci].default config.save() message = "Default configuration reinstated" elif not key.upper() in config: message = "Unknown config item: %s%s%s" % (c.r, key, c.w) elif val.upper() == "DEFAULT": att = config[key.upper()] att.value = att.default message = "%s%s%s set to %s%s%s (default)" dispval = att.display or "None" message = message % (c.y, key, c.w, c.y, dispval, c.w) config.save() else: # config.save() will be called by config.set() method message = config[key.upper()].set(val) showconfig() g.message = message @command(r'encoders?') def show_encs(): """ Display available encoding presets. """ out = "%sEncoding profiles:%s\n\n" % (c.ul, c.w) for x, e in enumerate(g.encoders): sel = " (%sselected%s)" % (c.y, c.w) if config.ENCODER.get == x else "" out += "%2d. %s%s\n" % (x, e['name'], sel) g.content = out message = "Enter %sset encoder %s to select an encoder" g.message = message % (c.g, c.w) mps-youtube-0.2.7.1/mps_youtube/commands/download.py000066400000000000000000000407651273726520200225270ustar00rootroot00000000000000import os import re import sys import time import shlex import random import subprocess from urllib.request import urlopen from urllib.error import HTTPError from .. import g, c, screen, streams, content, config, util from . import command, PL from .search import yt_url, user_pls from .songlist import dump, plist @command(r'(dv|da|d|dl|download)\s*(\d{1,4})') def download(dltype, num): """ Download a track or playlist by menu item number. """ # This function needs refactoring! # pylint: disable=R0912 # pylint: disable=R0914 if g.browse_mode == "ytpl" and dltype in ("da", "dv"): plid = g.ytpls[int(num) - 1]["link"] down_plist(dltype, plid) return elif g.browse_mode == "ytpl": g.message = "Use da or dv to specify audio / video playlist download" g.message = c.y + g.message + c.w g.content = content.generate_songlist_display() return elif g.browse_mode != "normal": g.message = "Download must refer to a specific video item" g.message = c.y + g.message + c.w g.content = content.generate_songlist_display() return screen.writestatus("Fetching video info...") song = (g.model[int(num) - 1]) best = dltype.startswith("dv") or dltype.startswith("da") if not best: try: # user prompt for download stream url, ext, url_au, ext_au = prompt_dl(song) except KeyboardInterrupt: g.message = c.r + "Download aborted!" + c.w g.content = content.generate_songlist_display() return if not url or ext_au == "abort": # abort on invalid stream selection g.content = content.generate_songlist_display() g.message = "%sNo download selected / invalid input%s" % (c.y, c.w) return else: # download user selected stream(s) filename = _make_fname(song, ext) args = (song, filename, url) if url_au and ext_au: # downloading video and audio stream for muxing audio = False filename_au = _make_fname(song, ext_au) args_au = (song, filename_au, url_au) else: audio = ext in ("m4a", "ogg") kwargs = dict(audio=audio) elif best: # set updownload without prompt url_au = None av = "audio" if dltype.startswith("da") else "video" audio = av == "audio" filename = _make_fname(song, None, av=av) args = (song, filename) kwargs = dict(url=None, audio=audio) try: # perform download(s) dl_filenames = [args[1]] f = _download(*args, **kwargs) if f: g.message = "Saved to " + c.g + f + c.w if url_au: dl_filenames += [args_au[1]] _download(*args_au, allow_transcode=False, **kwargs) except KeyboardInterrupt: g.message = c.r + "Download halted!" + c.w try: for downloaded in dl_filenames: os.remove(downloaded) except IOError: pass if url_au: # multiplex name, ext = os.path.splitext(args[1]) tmpvideoname = name + '.' +str(random.randint(10000, 99999)) + ext os.rename(args[1], tmpvideoname) mux_cmd = [g.muxapp, "-i", tmpvideoname, "-i", args_au[1], "-c", "copy", name + ".mp4"] try: subprocess.call(mux_cmd) g.message = "Saved to :" + c.g + mux_cmd[7] + c.w os.remove(tmpvideoname) os.remove(args_au[1]) except KeyboardInterrupt: g.message = "Audio/Video multiplex aborted!" g.content = content.generate_songlist_display() @command(r'(da|dv)\s+((?:\d+\s\d+|-\d|\d+-|\d,)(?:[\d\s,-]*))') def down_many(dltype, choice, subdir=None): """ Download multiple items. """ choice = util.parse_multi(choice) choice = list(set(choice)) downsongs = [g.model[int(x) - 1] for x in choice] temp = g.model[::] g.model.songs = downsongs[::] count = len(downsongs) av = "audio" if dltype.startswith("da") else "video" msg = "" def handle_error(message): """ Handle error in download. """ g.message = message g.content = disp screen.update() time.sleep(2) g.model.songs.pop(0) try: for song in downsongs: g.result_count = len(g.model) disp = content.generate_songlist_display() title = "Download Queue (%s):%s\n\n" % (av, c.w) disp = re.sub(r"(Num\s*?Title.*?\n)", title, disp) g.content = disp screen.update() try: filename = _make_fname(song, None, av=av, subdir=subdir) except IOError as e: handle_error("Error for %s: %s" % (song.title, str(e))) count -= 1 continue except KeyError: handle_error("No audio track for %s" % song.title) count -= 1 continue try: _download(song, filename, url=None, audio=av == "audio") except HTTPError: handle_error("HTTP Error for %s" % song.title) count -= 1 continue g.model.songs.pop(0) msg = "Downloaded %s items" % count g.message = "Saved to " + c.g + song.title + c.w except KeyboardInterrupt: msg = "Downloads interrupted!" finally: g.model.songs = temp[::] g.message = msg g.result_count = len(g.model) g.content = content.generate_songlist_display() @command(r'(da|dv)pl\s+%s' % PL) def down_plist(dltype, parturl): """ Download YouTube playlist. """ plist(parturl) dump(False) title = g.pafy_pls[parturl][0].title subdir = util.mswinfn(title.replace("/", "-")) down_many(dltype, "1-", subdir=subdir) msg = g.message plist(parturl) g.message = msg @command(r'(da|dv)upl\s+(.*)') def down_user_pls(dltype, user): """ Download all user playlists. """ user_pls(user) for i in g.ytpls: down_plist(dltype, i.get('link')) return def _make_fname(song, ext=None, av=None, subdir=None): """" Create download directory, generate filename. """ # pylint: disable=E1103 # Instance of 'bool' has no 'extension' member (some types not inferable) ddir = os.path.join(config.DDIR.get, subdir) if subdir else config.DDIR.get if not os.path.exists(ddir): os.makedirs(ddir) if not ext: stream = streams.select(streams.get(song), audio=av == "audio", m4a_ok=True) ext = stream['ext'] # filename = song.title[:59] + "." + ext filename = song.title + "." + ext filename = os.path.join(ddir, util.mswinfn(filename.replace("/", "-"))) filename = filename.replace('"', '') return filename def extract_metadata(name): """ Try to determine metadata from video title. """ seps = name.count(" - ") artist = title = None if seps == 1: pos = name.find(" - ") artist = name[:pos].strip() title = name[pos + 3:].strip() else: title = name.strip() return dict(artist=artist, title=title) def remux_audio(filename, title): """ Remux audio file. Insert limited metadata tags. """ util.dbg("starting remux") temp_file = filename + "." + str(random.randint(10000, 99999)) os.rename(filename, temp_file) meta = extract_metadata(title) metadata = ["title=%s" % meta["title"]] if meta["artist"]: metadata = ["title=%s" % meta["title"], "-metadata", "artist=%s" % meta["artist"]] cmd = [g.muxapp, "-y", "-i", temp_file, "-acodec", "copy", "-metadata"] cmd += metadata + ["-vn", filename] util.dbg(cmd) try: with open(os.devnull, "w") as devnull: subprocess.call(cmd, stdout=devnull, stderr=subprocess.STDOUT) except OSError: util.dbg("Failed to remux audio using %s", g.muxapp) os.rename(temp_file, filename) else: os.unlink(temp_file) util.dbg("remuxed audio file using %s" % g.muxapp) def transcode(filename, enc_data): """ Re encode a download. """ base = os.path.splitext(filename)[0] exe = g.muxapp if g.transcoder_path == "auto" else g.transcoder_path # ensure valid executable if not exe or not os.path.exists(exe) or not os.access(exe, os.X_OK): util.xprint("Encoding failed. Couldn't find a valid encoder :(\n") time.sleep(2) return filename command = shlex.split(enc_data['command']) newcom, outfn = command[::], "" for n, d in enumerate(command): if d == "ENCODER_PATH": newcom[n] = exe elif d == "IN": newcom[n] = filename elif d == "OUT": newcom[n] = outfn = base elif d == "OUT.EXT": newcom[n] = outfn = base + "." + enc_data['ext'] returncode = subprocess.call(newcom) if returncode == 0 and g.delete_orig: os.unlink(filename) return outfn def external_download(song, filename, url): """ Perform download using external application. """ cmd = config.DOWNLOAD_COMMAND.get ddir, basename = config.DDIR.get, os.path.basename(filename) cmd_list = shlex.split(cmd) def list_string_sub(orig, repl, lst): """ Replace substrings for items in a list. """ return [x if orig not in x else x.replace(orig, repl) for x in lst] cmd_list = list_string_sub("%F", filename, cmd_list) cmd_list = list_string_sub("%d", ddir, cmd_list) cmd_list = list_string_sub("%f", basename, cmd_list) cmd_list = list_string_sub("%u", url, cmd_list) cmd_list = list_string_sub("%i", song.ytid, cmd_list) util.dbg("Downloading using: %s", " ".join(cmd_list)) subprocess.call(cmd_list) def _download(song, filename, url=None, audio=False, allow_transcode=True): """ Download file, show status. Return filename or None in case of user specified download command. """ # pylint: disable=R0914 # too many local variables # Instance of 'bool' has no 'url' member (some types not inferable) if not url: stream = streams.select(streams.get(song), audio=audio, m4a_ok=True) url = stream['url'] # if an external download command is set, use it if config.DOWNLOAD_COMMAND.get: title = c.y + os.path.splitext(os.path.basename(filename))[0] + c.w util.xprint("Downloading %s using custom command" % title) external_download(song, filename, url) return None if not config.OVERWRITE.get: if os.path.exists(filename): util.xprint("File exists. Skipping %s%s%s ..\n" % (c.r, filename, c.w)) time.sleep(0.2) return filename util.xprint("Downloading to %s%s%s .." % (c.r, filename, c.w)) status_string = (' {0}{1:,}{2} Bytes [{0}{3:.2%}{2}] received. Rate: ' '[{0}{4:4.0f} kbps{2}]. ETA: [{0}{5:.0f} secs{2}]') resp = urlopen(url) total = int(resp.info()['Content-Length'].strip()) chunksize, bytesdone, t0 = 16384, 0, time.time() outfh = open(filename, 'wb') while True: chunk = resp.read(chunksize) outfh.write(chunk) elapsed = time.time() - t0 bytesdone += len(chunk) rate = (bytesdone / 1024) / elapsed eta = (total - bytesdone) / (rate * 1024) stats = (c.y, bytesdone, c.w, bytesdone * 1.0 / total, rate, eta) if not chunk: outfh.close() break status = status_string.format(*stats) sys.stdout.write("\r" + status + ' ' * 4 + "\r") sys.stdout.flush() active_encoder = g.encoders[config.ENCODER.get] ext = filename.split(".")[-1] valid_ext = ext in active_encoder['valid'].split(",") if audio and g.muxapp: remux_audio(filename, song.title) if config.ENCODER.get != 0 and valid_ext and allow_transcode: filename = transcode(filename, active_encoder) return filename def menu_prompt(model, prompt=" > ", rows=None, header=None, theading=None, footer=None, force=0): """ Generate a list of choice, returns item from model. """ content = "" for x in header, theading, rows, footer: if isinstance(x, list): for line in x: content += line + "\n" elif isinstance(x, str): content += x + "\n" g.content = content screen.update() choice = input(prompt) if choice in model: return model[choice] elif force: return menu_prompt(model, prompt, rows, header, theading, footer, force) elif not choice.strip(): return False, False else: # unrecognised input return False, "abort" def prompt_dl(song): """ Prompt user do choose a stream to dl. Return (url, extension). """ # pylint: disable=R0914 dl_data, p = get_dl_data(song) dl_text = gen_dl_text(dl_data, song, p) model = [x['url'] for x in dl_data] ed = enumerate(dl_data) model = {str(n + 1): (x['url'], x['ext']) for n, x in ed} url, ext = menu_prompt(model, "Download number: ", *dl_text) url2 = ext2 = None mediatype = [i for i in dl_data if i['url'] == url][0]['mediatype'] if mediatype == "video" and g.muxapp and not config.DOWNLOAD_COMMAND.get: # offer mux if not using external downloader dl_data, p = get_dl_data(song, mediatype="audio") dl_text = gen_dl_text(dl_data, song, p) au_choices = "1" if len(dl_data) == 1 else "1-%s" % len(dl_data) footer = [util.F('-audio') % ext, util.F('select mux') % au_choices] dl_text = tuple(dl_text[0:3]) + (footer,) aext = ("ogg", "m4a") model = [x['url'] for x in dl_data if x['ext'] in aext] ed = enumerate(dl_data) model = {str(n + 1): (x['url'], x['ext']) for n, x in ed} prompt = "Audio stream: " url2, ext2 = menu_prompt(model, prompt, *dl_text) return url, ext, url2, ext2 def gen_dl_text(ddata, song, p): """ Generate text for dl screen. """ hdr = [] hdr.append(" %s%s%s" % (c.r, song.title, c.w)) author = p.author hdr.append(c.r + " Uploaded by " + author + c.w) hdr.append(" [" + util.fmt_time(song.length) + "]") hdr.append("") heading = tuple("Item Format Quality Media Size Notes".split()) fmt = " {0}%-6s %-8s %-13s %-7s %-5s %-16s{1}" heading = [fmt.format(c.w, c.w) % heading] heading.append("") content = [] for n, d in enumerate(ddata): row = (n + 1, d['ext'], d['quality'], d['mediatype'], d['size'], d['notes']) fmt = " {0}%-6s %-8s %-13s %-7s %5s Mb %-16s{1}" row = fmt.format(c.g, c.w) % row content.append(row) content.append("") footer = "Select [%s1-%s%s] to download or [%sEnter%s] to return" footer = [footer % (c.y, len(content) - 1, c.w, c.y, c.w)] return(content, hdr, heading, footer) def get_dl_data(song, mediatype="any"): """ Get filesize and metadata for all streams, return dict. """ def mbsize(x): """ Return size in MB. """ return str(int(x / (1024 ** 2))) p = util.get_pafy(song) dldata = [] text = " [Fetching stream info] >" streamlist = [x for x in p.allstreams] if mediatype == "audio": streamlist = [x for x in p.audiostreams] l = len(streamlist) for n, stream in enumerate(streamlist): sys.stdout.write(text + "-" * n + ">" + " " * (l - n - 1) + "<\r") sys.stdout.flush() try: size = mbsize(stream.get_filesize()) except TypeError: util.dbg(c.r + "---Error getting stream size" + c.w) size = 0 item = {'mediatype': stream.mediatype, 'size': size, 'ext': stream.extension, 'quality': stream.quality, 'notes': stream.notes, 'url': stream.url} dldata.append(item) screen.writestatus("") return dldata, p @command(r'dlurl\s(.*[-_a-zA-Z0-9]{11}.*)') def dl_url(url): """ Open and prompt for download of youtube video url. """ g.browse_mode = "normal" yt_url(url) if len(g.model) == 1: download("download", "1") if g.command_line: sys.exit() @command(r'daurl\s(.*[-_a-zA-Z0-9]{11}.*)') def da_url(url): """ Open and prompt for download of youtube best audio from url. """ g.browse_mode = "normal" yt_url(url) if len(g.model) == 1: download("da", "1") if g.command_line: sys.exit() mps-youtube-0.2.7.1/mps_youtube/commands/local_playlist.py000066400000000000000000000127241273726520200237250ustar00rootroot00000000000000import re from .. import g, c, playlists, content, util from ..playlist import Playlist from . import command, WORD from .songlist import paginatesongs, songlist_rm_add @command(r'rmp\s*(\d+|%s)' % WORD) def playlist_remove(name): """ Delete a saved playlist by name - or purge working playlist if *all.""" if name.isdigit() or g.userpl.get(name): if name.isdigit(): name = int(name) - 1 name = sorted(g.userpl)[name] del g.userpl[name] g.message = "Deleted playlist %s%s%s" % (c.y, name, c.w) g.content = content.playlists_display() playlists.save() else: g.message = util.F('pl not found advise ls') % name g.content = content.playlists_display() @command(r'add\s*(-?\d[-,\d\s]{1,250})(%s)' % WORD) def playlist_add(nums, playlist): """ Add selected song nums to saved playlist. """ nums = util.parse_multi(nums) if not g.userpl.get(playlist): playlist = playlist.replace(" ", "-") g.userpl[playlist] = Playlist(playlist) for songnum in nums: g.userpl[playlist].songs.append(g.model[songnum - 1]) dur = g.userpl[playlist].duration f = (len(nums), playlist, len(g.userpl[playlist]), dur) g.message = util.F('added to saved pl') % f if nums: playlists.save() g.content = content.generate_songlist_display() @command(r'mv\s*(\d{1,3})\s*(%s)' % WORD) def playlist_rename_idx(_id, name): """ Rename a playlist by ID. """ _id = int(_id) - 1 playlist_rename(sorted(g.userpl)[_id] + " " + name) @command(r'mv\s*(%s\s+%s)' % (WORD, WORD)) def playlist_rename(playlists_): """ Rename a playlist using mv command. """ # Deal with old playlist names that permitted spaces a, b = "", playlists_.split(" ") while a not in g.userpl: a = (a + " " + (b.pop(0))).strip() if not b and a not in g.userpl: g.message = util.F('no pl match for rename') g.content = g.content or content.playlists_display() return b = "-".join(b) g.userpl[b] = Playlist(b) g.userpl[b].songs = list(g.userpl[a].songs) playlist_remove(a) g.message = util.F('pl renamed') % (a, b) playlists.save() @command(r'(rm|add)\s(?:\*|all)') def add_rm_all(action): """ Add all displayed songs to current playlist. remove all displayed songs from view. """ if action == "rm": g.model.songs.clear() msg = c.b + "Cleared all songs" + c.w g.content = content.generate_songlist_display(zeromsg=msg) elif action == "add": size = len(g.model) songlist_rm_add("add", "-" + str(size)) @command(r'save') def save_last(): """ Save command with no playlist name. """ if g.last_opened: open_save_view("save", g.last_opened) else: saveas = "" # save using artist name in postion 1 if g.model: saveas = g.model[0].title[:18].strip() saveas = re.sub(r"[^-\w]", "-", saveas, re.UNICODE) # loop to find next available name post = 0 while g.userpl.get(saveas): post += 1 saveas = g.model[0].title[:18].strip() + "-" + str(post) # Playlists are not allowed to start with a digit # TODO: Possibly change this, but ban purely numerical names saveas = saveas.lstrip("0123456789") open_save_view("save", saveas) @command(r'(open|save|view)\s*(%s)' % WORD) def open_save_view(action, name): """ Open, save or view a playlist by name. Get closest name match. """ name = name.replace(" ", "-") if action == "open" or action == "view": saved = g.userpl.get(name) if not saved: name = util.get_near_name(name, g.userpl) saved = g.userpl.get(name) elif action == "open": g.active.songs = list(saved.songs) g.last_opened = name msg = util.F("pl loaded") % name paginatesongs(g.active, msg=msg) elif action == "view": g.last_opened = "" msg = util.F("pl viewed") % name paginatesongs(list(saved.songs), msg=msg) elif not saved and action in "view open".split(): g.message = util.F("pl not found") % name g.content = content.playlists_display() elif action == "save": if not g.model: g.message = "Nothing to save. " + util.F('advise search') g.content = content.generate_songlist_display() else: g.userpl[name] = Playlist(name, list(g.model.songs)) g.message = util.F('pl saved') % name playlists.save() g.content = content.generate_songlist_display() @command(r'(open|view)\s*(\d{1,4})') def open_view_bynum(action, num): """ Open or view a saved playlist by number. """ srt = sorted(g.userpl) name = srt[int(num) - 1] open_save_view(action, name) @command(r'ls') def ls(): """ List user saved playlists. """ if not g.userpl: g.message = util.F('no playlists') g.content = g.content or \ content.generate_songlist_display(zeromsg=g.message) else: g.content = content.playlists_display() g.message = util.F('pl help') @command(r'vp') def vp(): """ View current working playlist. """ msg = util.F('current pl') txt = util.F('advise add') if g.model else util.F('advise search') failmsg = util.F('pl empty') + " " + txt paginatesongs(g.active, msg=msg, failmsg=failmsg) mps-youtube-0.2.7.1/mps_youtube/commands/misc.py000066400000000000000000000167541273726520200216540ustar00rootroot00000000000000import re import time import socket import traceback from urllib.request import urlopen from urllib.error import HTTPError, URLError try: # pylint: disable=F0401 import pyperclip has_pyperclip = True except ImportError: has_pyperclip = False try: import readline has_readline = True except ImportError: has_readline = False import pafy from .. import g, c, __version__, content, screen, cache from .. import streams, history, config, util from ..helptext import get_help from ..content import generate_songlist_display, logo from . import command from .songlist import paginatesongs @command(r'clearcache') def clearcache(): """ Clear cached items - for debugging use. """ g.pafs = {} g.streams = {} util.dbg("%scache cleared%s", c.p, c.w) g.message = "cache cleared" @command(r'(?:help|h)(?:\s+([-_a-zA-Z]+))?') def show_help(choice): """ Print help message. """ g.content = get_help(choice) @command(r'(?:q|quit|exit)') def quits(showlogo=True): """ Exit the program. """ if has_readline: readline.write_history_file(g.READLINE_FILE) util.dbg("Saved history file") cache.save() screen.clear() msg = logo(c.r, version=__version__) if showlogo else "" msg += util.F("exitmsg", 2) if config.CHECKUPDATE.get and showlogo: try: url = "https://raw.githubusercontent.com/mps-youtube/mps-youtube/master/VERSION" v = urlopen(url, timeout=1).read().decode() v = re.search(r"^version\s*([\d\.]+)\s*$", v, re.MULTILINE) if v: v = v.group(1) if v > __version__: msg += "\n\nA newer version is available (%s)\n" % v except (URLError, HTTPError, socket.timeout): util.dbg("check update timed out") screen.msgexit(msg) def fetch_comments(item): """ Fetch comments for item using gdata. """ # pylint: disable=R0912 # pylint: disable=R0914 ytid, title = item.ytid, item.title util.dbg("Fetching comments for %s", c.c("y", ytid)) screen.writestatus("Fetching comments for %s" % c.c("y", title[:55])) qs = {'textFormat': 'plainText', 'videoId': ytid, 'maxResults': 50, 'part': 'snippet'} # XXX should comment threads be expanded? this would require # additional requests for comments responding on top level comments jsdata = pafy.call_gdata('commentThreads', qs) coms = jsdata.get('items', []) coms = [x.get('snippet', {}) for x in coms] coms = [x.get('topLevelComment', {}) for x in coms] # skip blanks coms = [x for x in coms if len(x.get('snippet', {}).get('textDisplay', '').strip())] if not len(coms): g.message = "No comments for %s" % item.title[:50] g.content = generate_songlist_display() return commentstext = '' for n, com in enumerate(coms, 1): snippet = com.get('snippet', {}) poster = snippet.get('authorDisplayName') _, shortdate = util.yt_datetime(snippet.get('publishedAt', '')) text = snippet.get('textDisplay', '') cid = ("%s/%s" % (n, len(coms))) commentstext += ("%s %-35s %s\n" % (cid, c.c("g", poster), shortdate)) commentstext += c.c("y", text.strip()) + '\n\n' g.content = content.StringContent(commentstext) @command(r'c\s?(\d{1,4})') def comments(number): """ Receive use request to view comments. """ if g.browse_mode == "normal": item = g.model[int(number) - 1] fetch_comments(item) else: g.content = generate_songlist_display() g.message = "Comments only available for video items" @command(r'x\s*(\d+)') def clip_copy(num): """ Copy item to clipboard. """ if g.browse_mode == "ytpl": p = g.ytpls[int(num) - 1] link = "https://youtube.com/playlist?list=%s" % p['link'] elif g.browse_mode == "normal": item = (g.model[int(num) - 1]) link = "https://youtube.com/watch?v=%s" % item.ytid else: g.message = "clipboard copy not valid in this mode" g.content = generate_songlist_display() return if has_pyperclip: try: pyperclip.copy(link) g.message = c.y + link + c.w + " copied" g.content = generate_songlist_display() except Exception as e: g.content = generate_songlist_display() g.message = link + "\nError - couldn't copy to clipboard.\n" + \ ''.join(traceback.format_exception_only(type(e), e)) else: g.message = "pyperclip module must be installed for clipboard support\n" g.message += "see https://pypi.python.org/pypi/pyperclip/" g.content = generate_songlist_display() @command(r'i\s*(\d{1,4})') def info(num): """ Get video description. """ if g.browse_mode == "ytpl": p = g.ytpls[int(num) - 1] # fetch the playlist item as it has more metadata if p['link'] in g.pafy_pls: ytpl = g.pafy_pls[p['link']][0] else: g.content = logo(col=c.g) g.message = "Fetching playlist info.." screen.update() util.dbg("%sFetching playlist using pafy%s", c.y, c.w) ytpl = pafy.get_playlist2(p['link']) g.pafy_pls[p['link']] = (ytpl, util.IterSlicer(ytpl)) ytpl_desc = ytpl.description g.content = generate_songlist_display() created = util.yt_datetime(p['created'])[0] updated = util.yt_datetime(p['updated'])[0] out = c.ul + "Playlist Info" + c.w + "\n\n" out += p['title'] out += "\n" + ytpl_desc out += ("\n\nAuthor : " + p['author']) out += "\nSize : " + str(p['size']) + " videos" out += "\nCreated : " + time.strftime("%x %X", created) out += "\nUpdated : " + time.strftime("%x %X", updated) out += "\nID : " + str(p['link']) out += ("\n\n%s[%sPress enter to go back%s]%s" % (c.y, c.w, c.y, c.w)) g.content = out elif g.browse_mode == "normal": g.content = logo(c.b) screen.update() screen.writestatus("Fetching video metadata..") item = (g.model[int(num) - 1]) streams.get(item) p = util.get_pafy(item) pub = time.strptime(str(p.published), "%Y-%m-%d %H:%M:%S") screen.writestatus("Fetched") out = c.ul + "Video Info" + c.w + "\n\n" out += p.title or "" out += "\n" + (p.description or "") out += "\n\nAuthor : " + str(p.author) out += "\nPublished : " + time.strftime("%c", pub) out += "\nView count : " + str(p.viewcount) out += "\nRating : " + str(p.rating)[:4] out += "\nLikes : " + str(p.likes) out += "\nDislikes : " + str(p.dislikes) out += "\nCategory : " + str(p.category) out += "\nLink : " + "https://youtube.com/watch?v=%s" % p.videoid out += "\n\n%s[%sPress enter to go back%s]%s" % (c.y, c.w, c.y, c.w) g.content = out @command(r'history') def view_history(): """ Display the user's play history """ history = g.userhist.get('history') #g.last_opened = "" try: paginatesongs(list(reversed(history.songs))) g.message = "Viewing play history" except AttributeError: g.content = logo(c.r) g.message = "History empty" @command(r'history clear') def clear_history(): """ Clears the user's play history """ g.userhist['history'].songs = [] history.save() g.message = "History cleared" g.content = logo() mps-youtube-0.2.7.1/mps_youtube/commands/play.py000066400000000000000000000100341273726520200216470ustar00rootroot00000000000000import sys import webbrowser from urllib.error import HTTPError, URLError from .. import g, c, streams, util, content from . import command, WORD, RS from .songlist import plist from .search import yt_url from ..player import play_range @command(r'play\s+(%s|\d+)' % WORD) def play_pl(name): """ Play a playlist by name. """ if name.isdigit(): name = int(name) name = sorted(g.userpl)[name - 1] saved = g.userpl.get(name) if not saved: name = util.get_near_name(name, g.userpl) saved = g.userpl.get(name) if saved: g.model.songs = list(saved.songs) play_all("", "", "") else: g.message = util.F("pl not found") % name g.content = content.playlists_display() @command(r'(%s{0,3})([-,\d\s\[\]]{1,250})\s*(%s{0,3})$' % (RS, RS)) def play(pre, choice, post=""): """ Play choice. Use repeat/random if appears in pre/post. """ # pylint: disable=R0914 # too many local variables if g.browse_mode == "ytpl": if choice.isdigit(): return plist(g.ytpls[int(choice) - 1]['link']) else: g.message = "Invalid playlist selection: %s" % c.y + choice + c.w g.content = content.generate_songlist_display() return if not g.model: g.message = c.r + "There are no tracks to select" + c.w g.content = g.content or content.generate_songlist_display() else: shuffle = "shuffle" in pre + post repeat = "repeat" in pre + post novid = "-a" in pre + post fs = "-f" in pre + post nofs = "-w" in pre + post forcevid = "-v" in pre + post if ((novid and fs) or (novid and nofs) or (nofs and fs) or (novid and forcevid)): raise IOError("Conflicting override options specified") override = False override = "audio" if novid else override override = "fullscreen" if fs else override override = "window" if nofs else override if (not fs) and (not nofs): override = "forcevid" if forcevid else override selection = util.parse_multi(choice) songlist = [g.model[x - 1] for x in selection] # cache next result of displayed items # when selecting a single item if len(songlist) == 1: chosen = selection[0] - 1 if len(g.model) > chosen + 1: streams.preload(g.model[chosen + 1], override=override) play_range(songlist, shuffle, repeat, override) g.content = content.generate_songlist_display() @command(r'(%s{0,3})(?:\*|all)\s*(%s{0,3})' % (RS, RS)) def play_all(pre, choice, post=""): """ Play all tracks in model (last displayed). shuffle/repeat if req'd.""" options = pre + choice + post play(options, "1-" + str(len(g.model))) @command(r'playurl\s(.*[-_a-zA-Z0-9]{11}[^\s]*)(\s-(?:f|a|w))?') def play_url(url, override): """ Open and play a youtube video url. """ override = override if override else "_" g.browse_mode = "normal" yt_url(url, print_title=1) if len(g.model) == 1: play(override, "1", "_") if g.command_line: sys.exit() @command(r'browserplay\s(\d{1,50})') def browser_play(number): """Open a previously searched result in the browser.""" if (len(g.model) == 0): g.message = c.r + "No previous search." + c.w g.content = content.logo(c.r) return try: index = int(number) - 1 if (0 <= index < len(g.model)): base_url = "https://www.youtube.com/watch?v=" video = g.model[index] url = base_url + video.ytid webbrowser.open(url) g.content = g.content or content.generate_songlist_display() else: g.message = c.r + "Out of range." + c.w g.content = g.content or content.generate_songlist_display() return except (HTTPError, URLError, Exception) as e: g.message = c.r + str(e) + c.w g.content = g.content or content.generate_songlist_display() return mps-youtube-0.2.7.1/mps_youtube/commands/search.py000066400000000000000000000412131273726520200221520ustar00rootroot00000000000000import re import json import math import base64 import logging import pafy from .. import g, c, screen, config, util, content from ..playlist import Video from . import command from .songlist import plist, paginatesongs ISO8601_TIMEDUR_EX = re.compile(r'PT((\d{1,3})H)?((\d{1,3})M)?((\d{1,2})S)?') def _search(progtext, qs=None, msg=None, failmsg=None): """ Perform memoized url fetch, display progtext. """ loadmsg = "Searching for '%s%s%s'" % (c.y, progtext, c.w) wdata = pafy.call_gdata('search', qs) def iter_songs(): wdata2 = wdata while True: for song in get_tracks_from_json(wdata2): yield song if not wdata2.get('nextPageToken'): break qs['pageToken'] = wdata2['nextPageToken'] wdata2 = pafy.call_gdata('search', qs) # The youtube search api returns a maximum of 500 results length = min(wdata['pageInfo']['totalResults'], 500) slicer = util.IterSlicer(iter_songs(), length) paginatesongs(slicer, length=length, msg=msg, failmsg=failmsg, loadmsg=loadmsg) def token(page): """ Returns a page token for a given start index. """ index = (page or 0) * util.getxy().max_results k = index//128 - 1 index -= 128 * k f = [8, index] if k > 0 or index > 127: f.append(k+1) f += [16, 0] b64 = base64.b64encode(bytes(f)).decode('utf8') return b64.strip('=') def generate_search_qs(term, match='term'): """ Return query string. """ aliases = dict(views='viewCount') qs = { 'q': term, 'maxResults': 50, 'safeSearch': "none", 'order': aliases.get(config.ORDER.get, config.ORDER.get), 'part': 'id,snippet', 'type': 'video', 'key': config.API_KEY.get } if match == 'related': qs['relatedToVideoId'] = term del qs['q'] if config.SEARCH_MUSIC.get: qs['videoCategoryId'] = 10 return qs def userdata_cached(userterm): """ Check if user name search term found in cache """ userterm = ''.join([t.strip().lower() for t in userterm.split(' ')]) return g.username_query_cache.get(userterm) def cache_userdata(userterm, username, channel_id): """ Cache user name and channel id tuple """ userterm = ''.join([t.strip().lower() for t in userterm.split(' ')]) g.username_query_cache[userterm] = (username, channel_id) util.dbg('Cache data for username search query "{}": {} ({})'.format( userterm, username, channel_id)) while len(g.username_query_cache) > 300: g.username_query_cache.popitem(last=False) return (username, channel_id) def channelfromname(user): """ Query channel id from username. """ cached = userdata_cached(user) if cached: user, channel_id = cached else: # if the user is looked for by their display name, # we have to sent an additional request to find their # channel id qs = {'part': 'id,snippet', 'maxResults': 1, 'q': user, 'type': 'channel'} try: userinfo = pafy.call_gdata('search', qs)['items'] if len(userinfo) > 0: snippet = userinfo[0].get('snippet', {}) channel_id = snippet.get('channelId', user) username = snippet.get('title', user) user = cache_userdata(user, username, channel_id)[0] else: g.message = "User {} not found.".format(c.y + user + c.w) return except pafy.GdataError as e: g.message = "Could not retrieve information for user {}\n{}".format( c.y + user + c.w, e) util.dbg('Error during channel request for user {}:\n{}'.format( user, e)) return # at this point, we know the channel id associated to a user name return (user, channel_id) @command(r'user\s+(.+)') def usersearch(q_user, identify='forUsername'): """ Fetch uploads by a YouTube user. """ user, _, term = (x.strip() for x in q_user.partition("/")) if identify == 'forUsername': ret = channelfromname(user) if not ret: # Error return user, channel_id = ret else: channel_id = user # at this point, we know the channel id associated to a user name usersearch_id(user, channel_id, term) def usersearch_id(user, channel_id, term): """ Performs a search within a user's (i.e. a channel's) uploads for an optional search term with the user (i.e. the channel) identified by its ID """ query = generate_search_qs(term) aliases = dict(views='viewCount') # The value of the config item is 'views' not 'viewCount' if config.USER_ORDER.get: query['order'] = aliases.get(config.USER_ORDER.get, config.USER_ORDER.get) query['channelId'] = channel_id termuser = tuple([c.y + x + c.w for x in (term, user)]) if term: msg = "Results for {1}{3}{0} (by {2}{4}{0})" progtext = "%s by %s" % termuser failmsg = "No matching results for %s (by %s)" % termuser else: msg = "Video uploads by {2}{4}{0}" progtext = termuser[1] if config.SEARCH_MUSIC: failmsg = """User %s not found or has no videos in the Music category. Use 'set search_music False' to show results not in the Music category.""" % termuser[1] else: failmsg = "User %s not found or has no videos." % termuser[1] msg = str(msg).format(c.w, c.y, c.y, term, user) _search(progtext, query, msg, failmsg) def related_search(vitem): """ Fetch uploads by a YouTube user. """ query = generate_search_qs(vitem.ytid, match='related') if query.get('videoCategoryId'): del query['videoCategoryId'] t = vitem.title ttitle = t[:48].strip() + ".." if len(t) > 49 else t msg = "Videos related to %s%s%s" % (c.y, ttitle, c.w) failmsg = "Related to %s%s%s not found" % (c.y, vitem.ytid, c.w) _search(ttitle, query, msg, failmsg) # Note: [^./] is to prevent overlap with playlist search command @command(r'(?:search|\.|/)\s*([^./].{1,500})') def search(term): """ Perform search. """ if not term or len(term) < 2: g.message = c.r + "Not enough input" + c.w g.content = content.generate_songlist_display() return logging.info("search for %s", term) query = generate_search_qs(term) msg = "Search results for %s%s%s" % (c.y, term, c.w) failmsg = "Found nothing for %s%s%s" % (c.y, term, c.w) _search(term, query, msg, failmsg) @command(r'u(?:ser)?pl\s(.*)') def user_pls(user): """ Retrieve user playlists. """ return pl_search(user, is_user=True) @command(r'(?:\.\.|\/\/|pls(?:earch)?\s)\s*(.*)') def pl_search(term, page=0, splash=True, is_user=False): """ Search for YouTube playlists. term can be query str or dict indicating user playlist search. """ if not term or len(term) < 2: g.message = c.r + "Not enough input" + c.w g.content = content.generate_songlist_display() return if splash: g.content = content.logo(c.g) prog = "user: " + term if is_user else term g.message = "Searching playlists for %s" % c.y + prog + c.w screen.update() if is_user: ret = channelfromname(term) if not ret: # Error return user, channel_id = ret else: # playlist search is done with the above url and param type=playlist logging.info("playlist search for %s", prog) qs = generate_search_qs(term) qs['pageToken'] = token(page) qs['type'] = 'playlist' if 'videoCategoryId' in qs: del qs['videoCategoryId'] # Incompatable with type=playlist pldata = pafy.call_gdata('search', qs) id_list = [i.get('id', {}).get('playlistId') for i in pldata.get('items', ())] result_count = min(pldata['pageInfo']['totalResults'], 500) qs = {'part': 'contentDetails,snippet', 'maxResults': 50} if is_user: if page: qs['pageToken'] = token(page) qs['channelId'] = channel_id else: qs['id'] = ','.join(id_list) pldata = pafy.call_gdata('playlists', qs) playlists = get_pl_from_json(pldata)[:util.getxy().max_results] if is_user: result_count = pldata['pageInfo']['totalResults'] if playlists: g.last_search_query = (pl_search, {"term": term, "is_user": is_user}) g.browse_mode = "ytpl" g.current_page = page g.result_count = result_count g.ytpls = playlists g.message = "Playlist results for %s" % c.y + prog + c.w g.content = content.generate_playlist_display() else: g.message = "No playlists found for: %s" % c.y + prog + c.w g.current_page = 0 g.content = content.generate_songlist_display(zeromsg=g.message) def get_pl_from_json(pldata): """ Process json playlist data. """ try: items = pldata['items'] except KeyError: items = [] results = [] for item in items: snippet = item['snippet'] results.append(dict( link=item["id"], size=item["contentDetails"]["itemCount"], title=snippet["title"], author=snippet["channelTitle"], created=snippet["publishedAt"], updated=snippet['publishedAt'], #XXX Not available in API? description=snippet["description"])) return results def get_track_id_from_json(item): """ Try to extract video Id from various response types """ fields = ['contentDetails/videoId', 'snippet/resourceId/videoId', 'id/videoId', 'id'] for field in fields: node = item for p in field.split('/'): if node and isinstance(node, dict): node = node.get(p) if node: return node return '' def get_tracks_from_json(jsons): """ Get search results from API response """ items = jsons.get("items") if not items: util.dbg("got unexpected data or no search results") return () # fetch detailed information about items from videos API qs = {'part':'contentDetails,statistics,snippet', 'id': ','.join([get_track_id_from_json(i) for i in items])} wdata = pafy.call_gdata('videos', qs) items_vidinfo = wdata.get('items', []) # enhance search results by adding information from videos API response for searchresult, vidinfoitem in zip(items, items_vidinfo): searchresult.update(vidinfoitem) # populate list of video objects songs = [] for item in items: try: ytid = get_track_id_from_json(item) duration = item.get('contentDetails', {}).get('duration') if duration: duration = ISO8601_TIMEDUR_EX.findall(duration) if len(duration) > 0: _, hours, _, minutes, _, seconds = duration[0] duration = [seconds, minutes, hours] duration = [int(v) if len(v) > 0 else 0 for v in duration] duration = sum([60**p*v for p, v in enumerate(duration)]) else: duration = 30 else: duration = 30 stats = item.get('statistics', {}) snippet = item.get('snippet', {}) title = snippet.get('title', '').strip() # instantiate video representation in local model cursong = Video(ytid=ytid, title=title, length=duration) likes = int(stats.get('likeCount', 0)) dislikes = int(stats.get('dislikeCount', 0)) #XXX this is a very poor attempt to calculate a rating value rating = 5.*likes/(likes+dislikes) if (likes+dislikes) > 0 else 0 category = snippet.get('categoryId') # cache video information in custom global variable store g.meta[ytid] = dict( # tries to get localized title first, fallback to normal title title=snippet.get('localized', {'title':snippet.get('title', '[!!!]')}).get('title', '[!]'), length=str(util.fmt_time(cursong.length)), rating=str('{}'.format(rating))[:4].ljust(4, "0"), uploader=snippet.get('channelId'), uploaderName=snippet.get('channelTitle'), category=category, aspect="custom", #XXX uploaded=util.yt_datetime(snippet.get('publishedAt', ''))[1], likes=str(num_repr(likes)), dislikes=str(num_repr(dislikes)), commentCount=str(num_repr(int(stats.get('commentCount', 0)))), viewCount=str(num_repr(int(stats.get('viewCount', 0))))) except Exception as e: util.dbg(json.dumps(item, indent=2)) util.dbg('Error during metadata extraction/instantiation of ' + 'search result {}\n{}'.format(ytid, e)) songs.append(cursong) # return video objects return songs def num_repr(num): """ Return up to four digit string representation of a number, eg 2.6m. """ if num <= 9999: return str(num) def digit_count(x): """ Return number of digits. """ return int(math.floor(math.log10(x)) + 1) digits = digit_count(num) sig = 3 if digits % 3 == 0 else 2 rounded = int(round(num, int(sig - digits))) digits = digit_count(rounded) suffix = "_kmBTqXYX"[(digits - 1) // 3] front = 3 if digits % 3 == 0 else digits % 3 if not front == 1: return str(rounded)[0:front] + suffix return str(rounded)[0] + "." + str(rounded)[1] + suffix @command(r'u\s?([\d]{1,4})') def user_more(num): """ Show more videos from user of vid num. """ if g.browse_mode != "normal": g.message = "User uploads must refer to a specific video item" g.message = c.y + g.message + c.w g.content = content.generate_songlist_display() return g.current_page = 0 item = g.model[int(num) - 1] #TODO: Cleaner way of doing this? if item.ytid in g.meta: channel_id = g.meta.get(item.ytid, {}).get('uploader') user = g.meta.get(item.ytid, {}).get('uploaderName') else: paf = util.get_pafy(item) user, channel_id = channelfromname(paf.author) usersearch_id(user, channel_id, '') @command(r'r\s?(\d{1,4})') def related(num): """ Show videos related to to vid num. """ if g.browse_mode != "normal": g.message = "Related items must refer to a specific video item" g.message = c.y + g.message + c.w g.content = content.generate_songlist_display() return g.current_page = 0 item = g.model[int(num) - 1] related_search(item) @command(r'mix\s*(\d{1,4})') def mix(num): """ Retrieves the YouTube mix for the selected video. """ g.content = g.content or content.generate_songlist_display() if g.browse_mode != "normal": g.message = util.F('mix only videos') else: item = (g.model[int(num) - 1]) if item is None: g.message = util.F('invalid item') return item = util.get_pafy(item) # Mix playlists are made up of 'RD' + video_id try: plist("RD" + item.videoid) except OSError: g.message = util.F('no mix') @command(r'url\s(.*[-_a-zA-Z0-9]{11}.*)') def yt_url(url, print_title=0): """ Acess videos by urls. """ url_list = url.split() g.model.songs = [] for u in url_list: try: p = util.get_pafy(u) except (IOError, ValueError) as e: g.message = c.r + str(e) + c.w g.content = g.content or content.generate_songlist_display( zeromsg=g.message) return g.browse_mode = "normal" v = Video(p.videoid, p.title, p.length) g.model.songs.append(v) if not g.command_line: g.content = content.generate_songlist_display() if print_title: util.xprint(v.title) @command(r'url_file\s(\S+)') def yt_url_file(file_name): """ Access a list of urls in a text file """ #Open and read the file try: with open(file_name, "r") as fo: output = ' '.join([line.strip() for line in fo if line.strip()]) except (IOError): g.message = c.r + 'Error while opening the file, check the validity of the path' + c.w g.content = g.content or content.generate_songlist_display( zeromsg=g.message) return #Finally pass the input to yt_url yt_url(output) mps-youtube-0.2.7.1/mps_youtube/commands/songlist.py000066400000000000000000000166361273726520200225620ustar00rootroot00000000000000import math import random import pafy from .. import g, c, screen, streams, content, util from ..playlist import Video from . import command, PL def paginatesongs(func, page=0, splash=True, dumps=False, length=None, msg=None, failmsg=None, loadmsg=None): """ A utility function for handling lists of songs, so that the pagination and the dump command will work properly. :param func: Either a function taking a start and end index, or a slicable object. Either way, it should produce an iterable of :class:`mps_youtube.playlist.Video` objects. :param page: The page number to display :param splash: Whether or not to display a splash screen while loading. :param dumps: Used by :func:`dump` command to load all songs, instead of only those that fit on a page :param length: The total number of songs. It it is not provided, ``len(func)`` will be used instead. :param msg: Message to display after loading successfully :param failmsg: Message to display on failure (if no songs are returned by func :param loadmsg: Message to display while loading :type page: int :type splash: bool :type dumps: bool :type length: int :type msg: str :type failmsg: str :type loadmsg: str """ if splash: g.message = loadmsg or '' g.content = content.logo(col=c.b) screen.update() max_results = util.getxy().max_results if dumps: s = 0 e = None else: s = page * max_results e = (page + 1) * max_results if callable(func): songs = func(s, e) else: songs = func[s:e] if length is None: length = len(func) args = {'func':func, 'length':length, 'msg':msg, 'failmsg':failmsg, 'loadmsg': loadmsg} g.last_search_query = (paginatesongs, args) g.browse_mode = "normal" g.current_page = page g.result_count = length g.model.songs = songs g.content = content.generate_songlist_display() g.last_opened = "" g.message = msg or '' if not songs: g.message = failmsg or g.message if songs: # preload first result url streams.preload(songs[0], delay=0) @command(r'pl\s+%s' % PL) def plist(parturl): """ Retrieve YouTube playlist. """ if parturl in g.pafy_pls: ytpl, plitems = g.pafy_pls[parturl] else: util.dbg("%sFetching playlist using pafy%s", c.y, c.w) ytpl = pafy.get_playlist2(parturl) plitems = util.IterSlicer(ytpl) g.pafy_pls[parturl] = (ytpl, plitems) def pl_seg(s, e): return [Video(i.videoid, i.title, i.length) for i in plitems[s:e]] msg = "Showing YouTube playlist %s" % (c.y + ytpl.title + c.w) loadmsg = "Retrieving YouTube playlist" paginatesongs(pl_seg, length=len(ytpl), msg=msg, loadmsg=loadmsg) @command(r'(rm|add)\s*(-?\d[-,\d\s]{,250})') def songlist_rm_add(action, songrange): """ Remove or add tracks. works directly on user input. """ selection = util.parse_multi(songrange) if action == "add": duplicate_songs = [] for songnum in selection: if g.model[songnum - 1] in g.active: duplicate_songs.append(str(songnum)) g.active.songs.append(g.model[songnum - 1]) d = g.active.duration g.message = util.F('added to pl') % (len(selection), len(g.active), d) if duplicate_songs: duplicate_songs = ', '.join(sorted(duplicate_songs)) g.message += '\n' g.message += util.F('duplicate tracks') % duplicate_songs elif action == "rm": selection = sorted(set(selection), reverse=True) removed = str(tuple(reversed(selection))).replace(",", "") for x in selection: g.model.songs.pop(x - 1) g.message = util.F('songs rm') % (len(selection), removed) g.content = content.generate_songlist_display() @command(r'(mv|sw)\s*(\d{1,4})\s*[\s,]\s*(\d{1,4})') def songlist_mv_sw(action, a, b): """ Move a song or swap two songs. """ i, j = int(a) - 1, int(b) - 1 if action == "mv": g.model.songs.insert(j, g.model.songs.pop(i)) g.message = util.F('song move') % (g.model[j].title, b) elif action == "sw": g.model[i], g.model[j] = g.model[j], g.model[i] g.message = util.F('song sw') % (min(a, b), max(a, b)) g.content = content.generate_songlist_display() @command(r'(n|p)\s*(\d{1,2})?') def nextprev(np, page=None): """ Get next / previous search results. """ if isinstance(g.content, content.PaginatedContent): page_count = g.content.numPages() function = g.content.getPage args = {} else: page_count = math.ceil(g.result_count/util.getxy().max_results) function, args = g.last_search_query good = False if function: if np == "n": if g.current_page + 1 < page_count: g.current_page += 1 good = True elif np == "p": if page and int(page) in range(1,20): g.current_page = int(page)-1 good = True elif g.current_page > 0: g.current_page -= 1 good = True if good: function(page=g.current_page, **args) else: norp = "next" if np == "n" else "previous" g.message = "No %s items to display" % norp if not isinstance(g.content, content.PaginatedContent): g.content = content.generate_songlist_display() return good @command(r'(un)?dump') def dump(un): """ Show entire playlist. """ func, args = g.last_search_query if func is paginatesongs: paginatesongs(dumps=(not un), **args) else: un = "" if not un else un g.message = "%s%sdump%s may only be used on an open YouTube playlist" g.message = g.message % (c.y, un, c.w) g.content = content.generate_songlist_display() @command(r'shuffle') def shuffle_fn(): """ Shuffle displayed items. """ random.shuffle(g.model.songs) g.message = c.y + "Items shuffled" + c.w g.content = content.generate_songlist_display() @command(r'reverse') def reverse_songs(): """ Reverse order of displayed items. """ g.model.songs = g.model.songs[::-1] g.message = c.y + "Reversed displayed songs" + c.w g.content = content.generate_songlist_display() @command(r'reverse\s*(\d{1,4})\s*-\s*(\d{1,4})\s*') def reverse_songs_range(lower, upper): """ Reverse the songs within a specified range. """ lower, upper = int(lower), int(upper) if lower > upper: lower, upper = upper, lower g.model.songs[lower-1:upper] = reversed(g.model.songs[lower-1:upper]) g.message = c.y + "Reversed range: " + str(lower) + "-" + str(upper) + c.w g.content = content.generate_songlist_display() @command(r'reverse all') def reverse_playlist(): """ Reverse order of entire loaded playlist. """ # Prevent crash if no last query if g.last_search_query == (None, None) or \ 'func' not in g.last_search_query[1]: g.content = content.logo() g.message = "No playlist loaded" return songs_list_or_func = g.last_search_query[1]['func'] if callable(songs_list_or_func): songs = reversed(songs_list_or_func(0,None)) else: songs = reversed(songs_list_or_func) paginatesongs(list(songs)) g.message = c.y + "Reversed entire playlist" + c.w g.content = content.generate_songlist_display() mps-youtube-0.2.7.1/mps_youtube/config.py000066400000000000000000000301161273726520200203510ustar00rootroot00000000000000import os import re import sys import copy import pickle from urllib.request import urlopen from urllib.error import HTTPError from urllib.parse import urlencode import pafy from . import g, c, paths, util mswin = os.name == "nt" class ConfigItem(object): """ A configuration item. """ def __init__(self, name, value, minval=None, maxval=None, check_fn=None, require_known_player=False, allowed_values=None): """ If specified, the check_fn should return a dict. {valid: bool, message: success/fail mesage, value: value to set} """ self.default = self.value = value self.name = name self.type = type(value) self.maxval, self.minval = maxval, minval self.check_fn = check_fn self.require_known_player = require_known_player self.allowed_values = [] if allowed_values: self.allowed_values = allowed_values @property def get(self): """ Return value. """ return self.value @property def display(self): """ Return value in a format suitable for display. """ retval = self.value if self.name == "max_res": retval = str(retval) + "p" if self.name == "encoder": retval = str(retval) + " [%s]" % (str(g.encoders[retval]['name'])) return retval def set(self, value): """ Set value with checks. """ # note: fail_msg should contain %s %s for self.name, value # success_msg should not # pylint: disable=R0912 # too many branches success_msg = fail_msg = "" value = value.strip() value_orig = value # handle known player not set if self.allowed_values and value not in self.allowed_values: fail_msg = "%s must be one of * - not %s" allowed_values = copy.copy(self.allowed_values) if '' in allowed_values: allowed_values[allowed_values.index('')] = "" fail_msg = fail_msg.replace("*", ", ".join(allowed_values)) if self.require_known_player and \ not util.is_known_player(Config.PLAYER.get): fail_msg = "%s requires mpv or mplayer, can't set to %s" # handle true / false values elif self.type == bool: if value.upper() in "0 OFF NO DISABLED FALSE".split(): value = False success_msg = "%s set to False" % c.c("g", self.name) elif value.upper() in "1 ON YES ENABLED TRUE".split(): value = True success_msg = "%s set to True" % c.c("g", self.name) else: fail_msg = "%s requires True/False, got %s" # handle int values elif self.type == int: if not value.isdigit(): fail_msg = "%s requires a number, got %s" else: value = int(value) if self.maxval and self.minval: if not self.minval <= value <= self.maxval: m = " must be between %s and %s, got " m = m % (self.minval, self.maxval) fail_msg = "%s" + m + "%s" if not fail_msg: dispval = value or "None" success_msg = "%s set to %s" % (c.c("g", self.name), dispval) # handle space separated list elif self.type == list: success_msg = "%s set to %s" % (c.c("g", self.name), value) value = value.split() # handle string values elif self.type == str: dispval = value or "None" success_msg = "%s set to %s" % (c.c("g", self.name), c.c("g", dispval)) # handle failure if fail_msg: failed_val = value_orig.strip() or "" colvals = c.y + self.name + c.w, c.y + failed_val + c.w return fail_msg % colvals elif self.check_fn: checked = self.check_fn(value) value = checked.get("value") or value if checked['valid']: value = checked.get("value", value) self.value = value Config.save() return checked.get("message", success_msg) else: return checked.get('message', fail_msg) elif success_msg: self.value = value Config.save() return success_msg def check_console_width(val): """ Show ruler to check console width. """ valid = True message = "-" * val + "\n" message += "console_width set to %s, try a lower value if above line ove"\ "rlaps" % val return dict(valid=valid, message=message) def check_api_key(key): """ Validate an API key by calling an API endpoint with no quota cost """ url = "https://www.googleapis.com/youtube/v3/i18nLanguages" query = {"part": "snippet", "fields": "items/id", "key": key} try: urlopen(url + "?" + urlencode(query)).read() message = "The key, '" + key + "' will now be used for API requests." # Make pafy use the same api key pafy.set_api_key(Config.API_KEY.get) return dict(valid=True, message=message) except HTTPError: message = "Invalid key or quota exceeded, '" + key + "'" return dict(valid=False, message=message) def check_ddir(d): """ Check whether dir is a valid directory. """ expanded = os.path.expanduser(d) if os.path.isdir(expanded): message = "Downloads will be saved to " + c.y + d + c.w return dict(valid=True, message=message, value=expanded) else: message = "Not a valid directory: " + c.r + d + c.w return dict(valid=False, message=message) def check_win_pos(pos): """ Check window position input. """ if not pos.strip(): return dict(valid=True, message="Window position not set (default)") pos = pos.lower() reg = r"(TOP|BOTTOM).?(LEFT|RIGHT)" if not re.match(reg, pos, re.I): msg = "Try something like top-left or bottom-right (or default)" return dict(valid=False, message=msg) else: p = re.match(reg, pos, re.I).groups() p = "%s-%s" % p msg = "Window position set to %s" % p return dict(valid=True, message=msg, value=p) def check_win_size(size): """ Check window size input. """ if not size.strip(): return dict(valid=True, message="Window size not set (default)") size = size.lower() reg = r"\d{1,4}x\d{1,4}" if not re.match(reg, size, re.I): msg = "Try something like 720x480" return dict(valid=False, message=msg) else: return dict(valid=True, value=size) def check_encoder(option): """ Check encoder value is acceptable. """ encs = g.encoders if option >= len(encs): message = "%s%s%s is too high, type %sencoders%s to see valid values" message = message % (c.y, option, c.w, c.g, c.w) return dict(valid=False, message=message) else: message = "Encoder set to %s%s%s" message = message % (c.y, encs[option]['name'], c.w) return dict(valid=True, message=message) def check_player(player): """ Check player exefile exists and get mpv version. """ if util.has_exefile(player): util.load_player_info(player) if "mpv" in player: version = "%s.%s.%s" % g.mpv_version fmt = c.g, c.w, c.g, c.w, version msg = "%splayer%s set to %smpv%s (version %s)" % fmt return dict(valid=True, message=msg, value=player) else: msg = "%splayer%s set to %s%s%s" % (c.g, c.w, c.g, player, c.w) return dict(valid=True, message=msg, value=player) else: if mswin and not (player.endswith(".exe") or player.endswith(".com")): # Using mpv.exe has issues; use mpv.com if "mpv" in player: retval = check_player(player + ".com") if retval["valid"]: return retval return check_player(player + ".exe") else: msg = "Player application %s%s%s not found" % (c.r, player, c.w) return dict(valid=False, message=msg) class _Config(object): """ Holds various configuration values. """ _configitems = [ ConfigItem("order", "relevance", allowed_values="relevance date views rating title".split()), ConfigItem("user_order", "", allowed_values = [""] + "relevance date views rating".split()), ConfigItem("max_results", 19, maxval=50, minval=1), ConfigItem("console_width", 80, minval=70, maxval=880, check_fn=check_console_width), ConfigItem("max_res", 2160, minval=192, maxval=2160), ConfigItem("player", "mplayer" + ".exe" * mswin, check_fn=check_player), ConfigItem("playerargs", ""), ConfigItem("encoder", 0, minval=0, check_fn=check_encoder), ConfigItem("notifier", ""), ConfigItem("checkupdate", True), ConfigItem("show_mplayer_keys", True, require_known_player=True), ConfigItem("fullscreen", False, require_known_player=True), ConfigItem("show_status", True), ConfigItem("columns", ""), ConfigItem("ddir", paths.get_default_ddir(), check_fn=check_ddir), ConfigItem("overwrite", True), ConfigItem("show_video", False), ConfigItem("search_music", True), ConfigItem("window_pos", "", check_fn=check_win_pos, require_known_player=True), ConfigItem("window_size", "", check_fn=check_win_size, require_known_player=True), ConfigItem("download_command", ''), ConfigItem("audio_format", "auto", allowed_values="auto webm m4a".split()), ConfigItem("api_key", "AIzaSyCIM4EzNqi1in22f4Z3Ru3iYvLaY8tc3bo", check_fn=check_api_key) ] def __getitem__(self, key): # TODO: Possibly more efficient algorithm, w/ caching for i in self._configitems: if i.name.upper() == key: return i raise KeyError def __getattr__(self, name): try: return self[name] except KeyError: raise AttributeError def __iter__(self): return (i.name.upper() for i in self._configitems) def save(self): """ Save current config to file. """ config = {setting: self[setting].value for setting in self} with open(g.CFFILE, "wb") as cf: pickle.dump(config, cf, protocol=2) util.dbg(c.p + "Saved config: " + g.CFFILE + c.w) def load(self): """ Override config if config file exists. """ if os.path.exists(g.CFFILE): with open(g.CFFILE, "rb") as cf: saved_config = pickle.load(cf) for k, v in saved_config.items(): try: self[k].value = v except KeyError: # Ignore unrecognised data in config util.dbg("Unrecognised config item: %s", k) # Update config files from versions <= 0.01.41 if isinstance(self.PLAYERARGS.get, list): self.WINDOW_POS.value = "top-right" redundant = ("-really-quiet --really-quiet -prefer-ipv4 -nolirc " "-fs --fs".split()) for r in redundant: util.dbg("removing redundant arg %s", r) util.list_update(r, self.PLAYERARGS.value, remove=True) self.PLAYERARGS.value = " ".join(self.PLAYERARGS.get) self.save() Config = _Config() del _Config # _Config is a singleton and should not have more instances # Prevent module from being deleted # http://stackoverflow.com/questions/5365562/why-is-the-value-of-name-changing-after-assignment-to-sys-modules-name ref = sys.modules[__name__] # Any module trying to import config will get the Config object instead sys.modules[__name__] = Config mps-youtube-0.2.7.1/mps_youtube/content.py000066400000000000000000000177721273726520200205730ustar00rootroot00000000000000import math import copy import random import pafy from . import g, c, config from .util import getxy, fmt_time, uea_pad, yt_datetime, F # In the future, this could support more advanced features class Content: pass class PaginatedContent(Content): def getPage(self, page): raise NotImplementedError def numPages(self): raise NotImplementedError class LineContent(PaginatedContent): def getPage(self, page): max_results = getxy().max_results s = page * max_results e = (page + 1) * max_results return self.get_text(s, e) def numPages(self): return math.ceil(self.get_count()/getxy().max_results) def get_text(self, s, e): raise NotImplementedError def get_count(self): raise NotImplementedError class StringContent(LineContent): def __init__(self, string): self._lines = string.splitlines() def get_text(self, s, e): return '\n'.join(self._lines[s:e]) def get_count(self): width = getxy().width count = sum(len(i) // width + 1 for i in self._lines) return count def page_msg(page=0): """ Format information about currently displayed page to a string. """ if isinstance(g.content, PaginatedContent): page_count = g.content.numPages() else: page_count = math.ceil(g.result_count/getxy().max_results) if page_count > 1: pagemsg = "{}{}/{}{}" #start_index = max_results * g.current_page return pagemsg.format('<' if page > 0 else '[', "%s%s%s" % (c.y, page+1, c.w), page_count, '>' if page + 1 < page_count else ']') return None def generate_songlist_display(song=False, zeromsg=None): """ Generate list of choices from a song list.""" # pylint: disable=R0914 if g.browse_mode == "ytpl": return generate_playlist_display() max_results = getxy().max_results if not g.model: g.message = zeromsg or "Enter /search-term to search or [h]elp" return logo(c.g) + "\n\n" g.rprompt = page_msg(g.current_page) have_meta = all(x.ytid in g.meta for x in g.model) user_columns = _get_user_columns() if have_meta else [] maxlength = max(x.length for x in g.model) lengthsize = 8 if maxlength > 35999 else 7 lengthsize = 5 if maxlength < 6000 else lengthsize reserved = 9 + lengthsize + len(user_columns) cw = getxy().width cw -= 1 title_size = cw - sum(1 + x['size'] for x in user_columns) - reserved before = [{"name": "idx", "size": 3, "heading": "Num"}, {"name": "title", "size": title_size, "heading": "Title"}] after = [{"name": "length", "size": lengthsize, "heading": "Time"}] columns = before + user_columns + after for n, column in enumerate(columns): column['idx'] = n column['sign'] = "-" if not column['name'] == "length" else "" fmt = ["%{}{}s ".format(x['sign'], x['size']) for x in columns] fmtrow = fmt[0:1] + ["%s "] + fmt[2:] fmt, fmtrow = "".join(fmt).strip(), "".join(fmtrow).strip() titles = tuple([x['heading'][:x['size']] for x in columns]) hrow = c.ul + fmt % titles + c.w out = "\n" + hrow + "\n" for n, x in enumerate(g.model[:max_results]): col = (c.r if n % 2 == 0 else c.p) if not song else c.b details = {'title': x.title, "length": fmt_time(x.length)} details = copy.copy(g.meta[x.ytid]) if have_meta else details otitle = details['title'] details['idx'] = "%2d" % (n + 1) details['title'] = uea_pad(columns[1]['size'], otitle) cat = details.get('category') or '-' details['category'] = pafy.get_categoryname(cat) data = [] for z in columns: fieldsize, field = z['size'], z['name'] if len(details[field]) > fieldsize: details[field] = details[field][:fieldsize] data.append(details[field]) line = fmtrow % tuple(data) col = col if not song or song != g.model[n] else c.p line = col + line + c.w out += line + "\n" return out + "\n" * (5 - len(g.model)) if not song else out def generate_playlist_display(): """ Generate list of playlists. """ if not g.ytpls: g.message = c.r + "No playlists found!" return logo(c.g) + "\n\n" g.rprompt = page_msg(g.current_page) cw = getxy().width fmtrow = "%s%-5s %s %-12s %-8s %-2s%s\n" fmthd = "%s%-5s %-{}s %-12s %-9s %-5s%s\n".format(cw - 36) head = (c.ul, "Item", "Playlist", "Author", "Updated", "Count", c.w) out = "\n" + fmthd % head for n, x in enumerate(g.ytpls): col = (c.g if n % 2 == 0 else c.w) length = x.get('size') or "?" length = "%4s" % length title = x.get('title') or "unknown" author = x.get('author') or "unknown" updated = yt_datetime(x.get('updated'))[1] title = uea_pad(cw - 36, title) out += (fmtrow % (col, str(n + 1), title, author[:12], updated, str(length), c.w)) return out + "\n" * (5 - len(g.ytpls)) def _get_user_columns(): """ Get columns from user config, return dict. """ total_size = 0 user_columns = config.COLUMNS.get user_columns = user_columns.replace(",", " ").split() defaults = {"views": dict(name="viewCount", size=4, heading="View"), "rating": dict(name="rating", size=4, heading="Rtng"), "comments": dict(name="commentCount", size=4, heading="Comm"), "date": dict(name="uploaded", size=8, heading="Date"), "user": dict(name="uploaderName", size=10, heading="User"), "likes": dict(name="likes", size=4, heading="Like"), "dislikes": dict(name="dislikes", size=4, heading="Dslk"), "category": dict(name="category", size=8, heading="Category")} ret = [] for column in user_columns: namesize = column.split(":") name = namesize[0] if name in defaults: z = defaults[name] nm, sz, hd = z['name'], z['size'], z['heading'] if len(namesize) == 2 and namesize[1].isdigit(): sz = int(namesize[1]) total_size += sz cw = getxy().width if total_size < cw - 18: ret.append(dict(name=nm, size=sz, heading=hd)) return ret def logo(col=None, version=""): """ Return text logo. """ col = col if col else random.choice((c.g, c.r, c.y, c.b, c.p, c.w)) logo_txt = r""" _ _ _ __ ___ _ __ ___ _ _ ___ _ _| |_ _ _| |__ ___ | '_ ` _ \| '_ \/ __|_____| | | |/ _ \| | | | __| | | | '_ \ / _ \ | | | | | | |_) \__ \_____| |_| | (_) | |_| | |_| |_| | |_) | __/ |_| |_| |_| .__/|___/ \__, |\___/ \__,_|\__|\__,_|_.__/ \___| |_| |___/""" version = " v" + version if version else "" logo_txt = col + logo_txt + c.w + version lines = logo_txt.split("\n") length = max(len(x) for x in lines) x, y, _ = getxy() indent = (x - length - 1) // 2 newlines = (y - 12) // 2 indent, newlines = (0 if x < 0 else x for x in (indent, newlines)) lines = [" " * indent + l for l in lines] logo_txt = "\n".join(lines) + "\n" * newlines return "" if g.debug_mode else logo_txt def playlists_display(): """ Produce a list of all playlists. """ if not g.userpl: g.message = F("no playlists") return generate_songlist_display() if g.model else (logo(c.y) + "\n\n") maxname = max(len(a) for a in g.userpl) out = " {0}Local Playlists{1}\n".format(c.ul, c.w) start = " " fmt = "%s%s%-3s %-" + str(maxname + 3) + "s%s %s%-7s%s %-5s%s" head = (start, c.b, "ID", "Name", c.b, c.b, "Count", c.b, "Duration", c.w) out += "\n" + fmt % head + "\n\n" for v, z in enumerate(sorted(g.userpl)): n, p = z, g.userpl[z] l = fmt % (start, c.g, v + 1, n, c.w, c.y, str(len(p)), c.y, p.duration, c.w) + "\n" out += l return out mps-youtube-0.2.7.1/mps_youtube/g.py000066400000000000000000000126131273726520200173340ustar00rootroot00000000000000""" Module for holding globals that are needed throught mps-youtube. """ import os import sys import collections from . import c, paths from .playlist import Playlist transcoder_path = "auto" delete_orig = True encoders = [] muxapp = False meta = {} detectable_size = True command_line = False debug_mode = False preload_disabled = False ytpls = [] mpv_version = 0, 0, 0 mpv_options = None mpv_usesock = False mplayer_version = 0 mprisctl = None browse_mode = "normal" preloading = [] # expiry = 5 * 60 * 60 # 5 hours no_clear_screen = False max_retries = 3 max_cached_streams = 1500 username_query_cache = collections.OrderedDict() model = Playlist(name="model") last_search_query = (None, None) current_page = 0 result_count = 0 rprompt = None active = Playlist(name="active") userpl = {} userhist = {} pafs = collections.OrderedDict() streams = collections.OrderedDict() pafy_pls = {} # last_opened = message = content = "" suffix = "3" # Python 3 CFFILE = os.path.join(paths.get_config_dir(), "config") TCFILE = os.path.join(paths.get_config_dir(), "transcode") OLD_PLFILE = os.path.join(paths.get_config_dir(), "playlist" + suffix) PLFILE = os.path.join(paths.get_config_dir(), "playlist_v2") HISTFILE = os.path.join(paths.get_config_dir(), "play_history") CACHEFILE = os.path.join(paths.get_config_dir(), "cache_py_" + sys.version[0:5]) READLINE_FILE = None playerargs_defaults = { "mpv": { "msglevel": {"<0.4": "--msglevel=all=no:statusline=status", ">=0.4": "--msg-level=all=no:statusline=status"}, "title": "--title", "fs": "--fs", "novid": "--no-video", "ignidx": "--demuxer-lavf-o=fflags=+ignidx", "geo": "--geometry"}, "mplayer": { "title": "-title", "fs": "-fs", "novid": "-novideo", # "ignidx": "-lavfdopts o=fflags=+ignidx".split() "ignidx": "", "geo": "-geometry"} } argument_commands = [] commands = [] text = { "exitmsg": ("*mps-youtube - *http://github.com/np1/mps-youtube*" "\nReleased under the GPLv3 license\n" "(c) 2014, 2015 np1 and contributors*\n"""), "exitmsg_": (c.r, c.b, c.r, c.w), # Error / Warning messages 'no playlists': "*No saved playlists found!*", 'no playlists_': (c.r, c.w), 'pl bad name': '*&&* is not valid a valid name. Ensure it starts with' ' a letter or _', 'pl bad name_': (c.r, c.w), 'pl not found': 'Playlist *&&* unknown. Saved playlists are shown ' 'above', 'pl not found_': (c.r, c.w), 'pl not found advise ls': 'Playlist "*&&*" not found. Use *ls* to ' 'list', 'pl not found advise ls_': (c.y, c.w, c.g, c.w), 'pl empty': 'Playlist is empty!', 'advise add': 'Use *add N* to add a track', 'advise add_': (c.g, c.w), 'advise search': 'Search for items and then use *add* to add them', 'advise search_': (c.g, c.w), 'no data': 'Error fetching data. Possible network issue.' '\n*&&*', 'no data_': (c.r, c.w), 'use dot': 'Start your query with a *.* to perform a search', 'use dot_': (c.g, c.w), 'cant get track': 'Problem playing last item: *&&*', 'cant get track_': (c.r, c.w), 'track unresolved': 'Sorry, this track is not available', 'no player': '*&&* was not found on this system', 'no player_': (c.y, c.w), 'no pl match for rename': '*Couldn\'t find matching playlist to ' 'rename*', 'no pl match for rename_': (c.r, c.w), 'invalid range': "*Invalid item / range entered!*", 'invalid range_': (c.r, c.w), '-audio': "*Warning* - the filetype you selected (&&) has no audio!", '-audio_': (c.y, c.w), 'no mix': 'No mix is available for the selected video', 'mix only videos': 'Mixes are only available for videos', 'invalid item': '*Invalid item entered!*', 'duplicate tracks': '*Warning* - duplicate track(s) && added to ' 'playlist!', 'duplicate tracks_': (c.y, c.w), # Info messages.. 'select mux': ("Select [*&&*] to mux audio or [*Enter*] to download " "without audio\nThis feature is experimental!"), 'select mux_': (c.y, c.w, c.y, c.w), 'pl renamed': 'Playlist *&&* renamed to *&&*', 'pl renamed_': (c.y, c.w, c.y, c.w), 'pl saved': 'Playlist saved as *&&*. Use *ls* to list playlists', 'pl saved_': (c.y, c.w, c.g, c.w), 'pl loaded': 'Loaded playlist *&&* as current playlist', 'pl loaded_': (c.y, c.w), 'pl viewed': 'Showing playlist *&&*', 'pl viewed_': (c.y, c.w), 'pl help': 'Enter *open * to load a playlist', 'pl help_': (c.g, c.w), 'added to pl': '*&&* tracks added (*&&* total [*&&*]). Use *vp* to ' 'view', 'added to pl_': (c.y, c.w, c.y, c.w, c.y, c.w, c.g, c.w), 'added to saved pl': '*&&* tracks added to *&&* (*&&* total [*&&*])', 'added to saved pl_': (c.y, c.w, c.y, c.w, c.y, c.w, c.y, c.w), 'song move': 'Moved *&&* to position *&&*', 'song move_': (c.y, c.w, c.y, c.w), 'song sw': ("Switched item *&&* with *&&*"), 'song sw_': (c.y, c.w, c.y, c.w), 'current pl': "This is the current playlist. Use *save * to save" " it", 'current pl_': (c.g, c.w), 'help topic': (" Enter *help * for specific help:"), 'help topic_': (c.y, c.w), 'songs rm': '*&&* tracks removed &&', 'songs rm_': (c.y, c.w)} mps-youtube-0.2.7.1/mps_youtube/helptext.py000066400000000000000000000334411273726520200207450ustar00rootroot00000000000000from . import c, g from .util import get_near_name, F def helptext(): """ Return a list of help categories, with their contents. """ return [ ("basic", "Basics", """ {0}Basic Usage{1} Use {2}/{1} or {2}.{1} to prefix your search query. e.g., {2}/pink floyd{1} Then, when results are shown: {2}{1} - play specified items, separated by commas. e.g., {2}1-3,5{1} plays items 1, 2, 3 and 5. {2}i {1} - view information on video {2}c {1} - view comments for video {2}d {1} - download video {2}r {1} - show videos related to video {2}u {1} - show videos uploaded by uploader of video {2}x {1} - copy item url to clipboard (requires pyperclip) {2}q{1}, {2}quit{1} - exit mpsyt """.format(c.ul, c.w, c.y)), ("search", "Searching and Retrieving", """ {0}Searching and Retrieving{1} {2}set search_music false{1} - search all YouTube categories. {2}set search_music true{1} - search only YouTube music category. {2}/{1} or {2}.{1} to search for videos. e.g., {2}/daft punk{1} {2}//{1} or {2}..{1} - search for YouTube playlists. e.g., \ {2}//80's music{1} {2}n{1} and {2}p{1} - continue search to next/previous pages. {2}p {1} - switch to page . {2}album {1} - Search for matching tracks using album title {2}user {1} - list YouTube uploads by . {2}user /{1} - as above, but matches . {2}userpl {1} - list YouTube playlists created by . {2}pl {1} - Open YouTube playlist by url or id. {2}url {1} - Retrieve specific YouTube video by url or id. {2}url ... {1} - Retrieve specific YouTube videos by url or id. {2}url_file {1} - Retrieve YouTube videos by url or id from a .txt file. File format : .txt, with one url or id by line. {2}r {1} - show videos related to video . {2}u {1} - show videos uploaded by uploader of video . {2}c {1} - view comments for video """.format(c.ul, c.w, c.y)), ("edit", "Editing / Manipulating Results", """ {0}Editing and Manipulating Results{1} {2}rm {1} - remove items from displayed results. {2}sw ,{1} - swap two items. {2}mv ,{1} - move item to position . {2}save {1} - save displayed items as a local playlist. {2}mix {1} - show YouTube mix playlist from item in results. {2}shuffle{1} - Shuffle the displayed results. {2}reverse{1} or {2}reverse -{1} - Reverse the displayed items or item range. {2}reverse all{1} - Reverse order of entire loaded playlist """.format(c.ul, c.w, c.y)), ("download", "Downloading and Playback", """ {0}Downloading and Playback{1} {2}set show_video true{1} - play video instead of audio. {2}{1} - play specified items, separated by commas. e.g., {2}1-3,5{1} plays items 1, 2, 3 and 5 {2}d {1} - view downloads available for an item. {2}da {1} - download best available audio file(s). {2}dv {1} - download best available video file(s). {2}dapl {1} - download YouTube playlist (audio) by url or id. {2}dvpl {1} - download YouTube playlist (video) by url or id. {2}daupl {1} - download user's YouTube playlists (audio). {2}dvupl {1} - download user's YouTube playlists (video). {2}dlurl {1} - download a YouTube video by url or video id. {2}daurl {1} - download best available audio of YouTube video by url or video id. {2}playurl {1} - play a YouTube video by url or id. {2}browserplay {1} - open a specified previous search in browser. {2}all{1} or {2}*{1} - play all displayed items. {2}repeat {1} - play and repeat the specified items. {2}shuffle {1} - play specified items in random order. """.format(c.ul, c.w, c.y)), ("dl-command", "Downloading Using External Application", """ {0}Download Using A Custom Application{1} Use {2}set download_command {1} to specify a custom command to use for downloading. mps-youtube will make the following substitutions: %u - url of the remote file to download %d - download directory as set in DDIR in mps-youtube config %f - filename (determined by title and filetype) %F - full file path (%d/%f) %i - youtube video id for example, to download using aria2c (http://aria2.sourceforge.net), enter: {2}set download_command aria2c --dir=%d --out=%f %u{1} Note that using a custom download command does not support transcoding the downloaded file to another format using mps-youtube. """.format(c.ul, c.w, c.y)), ("encode", "Encoding to MP3 and other formats", """ {0}Encoding to MP3 and other formats{1} Enter {2}encoders{1} to view available encoding presets Enter {2}set encoder {1} to apply an encoding preset for downloads This feature requires that ffmpeg or avconv is installed on your system and is available in the system path. The encoding presets can be modified by editing the text config file which resides at: {3} """.format(c.ul, c.w, c.y, g.TCFILE)), ("playlists", "Using Local Playlists", """ {0}Using Local Playlists{1} {2}add {1} - add items to the current playlist. {2}add {1} - add items to the specified playlist. ( will be created if it doesn't already exist) {2}vp{1} - view current playlist. {2}ls{1} - list saved playlists. {2}mv {1} - rename a playlist. {2}rmp {1} - delete a playlist from disk. {2}open {1} - open a saved playlist as the current playlist. {2}play {1} - play a saved playlist directly. {2}view {1} - view a playlist (current playlist left intact). {2}save{1} or {2}save {1} - save the displayed items as a playlist. {2}rm {1} - remove items from displayed results. {2}sw ,{1} - swap two items. {2}mv ,{1} - move item to position . """.format(c.ul, c.w, c.y)), ("history", "Accessing Local History", """ {0}Accessing Local History{1} Access songs that have been played within mpsyt {2}history{1} - displays a list of songs contained in history {2}history clear{1} - clears the song history """.format(c.ul, c.w, c.y)), ("invoke", "Invocation Parameters", """ {0}Invocation{1} All mpsyt commands can be entered from the command line. For example; {2}mpsyt dlurl {1} to download a YouTube video by url or id {2}mpsyt playurl {1} to play a YouTube video by url or id {2}mpsyt /mozart{1} to search {2}mpsyt //best songs of 2010{1} for a playlist search {2}mpsyt play {1} to play a saved playlist {2}mpsyt ls{1} to list saved playlists For further automation, a series of commands can be entered separated by commas (,). E.g., {2}mpsyt open 1, 2-4{1} - play items 2-4 of first saved playlist {2}mpsyt //the doors, 1, all -a{1} - open YouTube playlist and play audio If you need to enter an actual comma on the command line, use {2},,{1} instead. """.format(c.ul, c.w, c.y)), ("config", "Configuration Options", """ {0}Configuration{1} {2}set{1} - view current configuration {2}set default{1} - set an item to its default value {2}set all default{1} - restore default settings {2}set checkupdate true|false{1} - check for updates on exit {2}set columns {1} - select extra displayed fields in search results: (valid: views comments rating date user likes dislikes category) {2}set ddir {1} - set where downloads are saved {2}set download_command {1} - type {2}help dl-command{1} for info {2}set encoder {1} - set encoding preset for downloaded files {2}set fullscreen true|false{1} - output video content in full-screen mode {2}set max_res {1} - play / download maximum video resolution height{3} {2}set notifier {1} - call with each new song title {2}set order {1} search result ordering {2}set user_order <|relevance|date|views|rating>{1} user upload list result ordering, leave blank for the same as order setting {2}set overwrite true|false{1} - overwrite existing files (skip if false) {2}set player {1} - use for playback {2}set playerargs {1} - use specified arguments with player {2}set search_music true|false{1} - search only music (all categories if false) {2}set show_mplayer_keys true|false{1} - show keyboard help for mplayer and mpv {2}set show_status true|false{1} - show status messages and progress {2}set show_video true|false{1} - show video output (audio only if false) {2}set window_pos -{1} - set player window position {2}set window_size x{1} - set player window width & height {2}set audio_format {1} - set default music audio format {2}set api_key {1} - use a different API key for accessing the YouTube Data API """.format(c.ul, c.w, c.y, '\n{0}set max_results {1} - show re' 'sults when searching (max 50)'.format(c.y, c.w) if not g.detectable_size else '')), ("tips", "Advanced Tips", """ {0}Advanced Tips{1} Use {2}-w{1}, {2}-f{1} or {2}-a{1} with your choice to override the configured\ setting and play items in windowed, fullscreen or audio modes. E.g., 1-4 -a When specifying columns with {2}set columns{1} command, append :N to set\ width. E.g.: {2}set columns date views user:17 likes{1} When using {2}open{1}, {2}view{1} or {2}play{1} to access a local playlist, \ you can enter the first few characters instead of the whole name. Use {2}5-{1} to select items 5 upward and {2}-5{1} to select up to item 5. \ This can be included with other choices. e.g., 5,3,7-,-2 You can use spaces instead of commas: 5 3 7- -2 Reversed ranges also work. eg., 5-2 {2}dump{1} - to show entire contents of an opened YouTube playlist. (useful for playing or saving entire playlists, use {2}undump{1} to \ undo) {2}set player mpv{1} or {2}set player mplayer{1} - change player application Use {2}1{1} and {2}0{1} in place of true and false when using the {2}set{1} \ command """.format(c.ul, c.w, c.y)), ("new", "New Features", """ {0}New Features in v0.2.7{1} - Setting for default audio format (nishanthkarthik) - Search history with "history" command (kraetzin) - Add syntax for repeating a track several times (ghallak) - New "reverse" command (kraetzin) - New "daurl " command (maricn){2} """.format(c.ul, c.w, c.y))] def get_help(choice): """ Return help message. """ helps = {"download": ("playback dl listen watch show repeat playing" "show_video playurl browserplay dlurl d da dv all *" " play browsersearch".split()), "dl-command": ("dlcmd dl-cmd download-cmd dl_cmd download_cmd " "download-command download_command".split()), "encode": ("encoding transcoding transcode wma mp3 format " "encode encoder".split()), "invoke": "command commands mpsyt invocation".split(), "search": ("user userpl pl pls r n p url album " "editing result results related remove swop".split()), "edit": ("editing manupulate manipulating rm mv sw edit move " "swap shuffle".split()), "tips": ("undump dump -f -w -a adv advanced".split(" ")), "basic": ("basic comment basics c copy clipboard comments u " "i".split()), "config": ("set checkupdate colours colors ddir directory player " "arguments args playerargs music search_music keys " "status show_status show_video video configuration " "fullscreen full screen folder player mpv mplayer" " settings default reset configure audio results " "max_results size lines rows height window " "position window_pos quality resolution max_res " "columns width console overwrite".split()), "playlists": ("save rename delete move rm ls mv sw add vp open" " view".split())} for topic, aliases in helps.items(): if choice in aliases: choice = topic break choice = "menu" if not choice else choice out, all_help = "", helptext() help_names = [x[0] for x in all_help] choice = get_near_name(choice, help_names) def indent(x): """ Indent. """ return "\n ".join(x.split("\n")) if choice == "menu" or choice not in help_names: out += " %sHelp Topics%s" % (c.ul, c.w) out += F('help topic', 2, 1) for x in all_help: out += ("\n%s %-10s%s : %s" % (c.y, x[0], c.w, x[1])) out += "\n" return out else: choice = help_names.index(choice) return indent(all_help[choice][2]) mps-youtube-0.2.7.1/mps_youtube/history.py000066400000000000000000000015021273726520200206020ustar00rootroot00000000000000import os import pickle from . import g, c from .util import dbg from .playlist import Playlist def add(song): """ Add song to history. """ if not g.userhist.get('history'): g.userhist['history'] = Playlist('history') g.userhist['history'].songs.append(song) save() def load(): """ Open history. Called once on script invocation. """ try: with open(g.HISTFILE, "rb") as hlf: g.userhist = pickle.load(hlf) except IOError: # no playlist found, create a blank one if not os.path.isfile(g.HISTFILE): g.userhist = {} save() def save(): """ Save history. Called each time history is updated. """ with open(g.HISTFILE, "wb") as hlf: pickle.dump(g.userhist, hlf, protocol=2) dbg(c.r + "History saved\n---" + c.w) mps-youtube-0.2.7.1/mps_youtube/init.py000066400000000000000000000176131273726520200200560ustar00rootroot00000000000000import os import re import sys import logging import tempfile import argparse import platform import multiprocessing import pafy try: # pylint: disable=F0401 import colorama has_colorama = True except ImportError: has_colorama = False try: import readline readline.set_history_length(2000) has_readline = True except ImportError: has_readline = False from . import cache, g, __version__, __notes__, screen, c, paths, config from .util import has_exefile, dbg, xprint, load_player_info from .helptext import helptext mswin = os.name == "nt" def init(): """ Initial setup. """ _process_cl_args() # set player to mpv or mplayer if found, otherwise unset suffix = ".exe" if mswin else "" mplayer, mpv = "mplayer" + suffix, "mpv" + suffix if not os.path.exists(g.CFFILE): if has_exefile(mpv): config.PLAYER.set(mpv) elif has_exefile(mplayer): config.PLAYER.set(mplayer) config.save() else: config.load() _init_readline() cache.load() _init_transcode() # ensure encoder is not set beyond range of available presets if config.ENCODER.get >= len(g.encoders): config.ENCODER.set("0") # check mpv/mplayer version if has_exefile(config.PLAYER.get): load_player_info(config.PLAYER.get) # setup colorama if has_colorama and mswin: # Colorama converts ansi escape codes to Windows system calls colorama.init() # find muxer app if mswin: g.muxapp = has_exefile("ffmpeg.exe") or has_exefile("avconv.exe") else: g.muxapp = has_exefile("ffmpeg") or has_exefile("avconv") # initialize remote interface try: from . import mpris g.mprisctl, conn = multiprocessing.Pipe() t = multiprocessing.Process(target=mpris.main, args=(conn,)) t.daemon = True t.start() except ImportError: pass # Make pafy use the same api key pafy.set_api_key(config.API_KEY.get) def _init_transcode(): """ Create transcoding presets if not present. Read transcoding presets. """ if not os.path.exists(g.TCFILE): config_file_contents = """\ # transcoding presets for mps-youtube # VERSION 0 # change ENCODER_PATH to the path of ffmpeg / avconv or leave it as auto # to let mps-youtube attempt to find ffmpeg or avconv ENCODER_PATH: auto # Delete original file after encoding it # Set to False to keep the original downloaded file DELETE_ORIGINAL: True # ENCODING PRESETS # Encode ogg or m4a to mp3 256k name: MP3 256k extension: mp3 valid for: ogg,m4a command: ENCODER_PATH -i IN -codec:a libmp3lame -b:a 256k OUT.EXT # Encode ogg or m4a to mp3 192k name: MP3 192k extension: mp3 valid for: ogg,m4a command: ENCODER_PATH -i IN -codec:a libmp3lame -b:a 192k OUT.EXT # Encode ogg or m4a to mp3 highest quality vbr name: MP3 VBR best extension: mp3 valid for: ogg,m4a command: ENCODER_PATH -i IN -codec:a libmp3lame -q:a 0 OUT.EXT # Encode ogg or m4a to mp3 high quality vbr name: MP3 VBR good extension: mp3 valid for: ogg,m4a command: ENCODER_PATH -i IN -codec:a libmp3lame -q:a 2 OUT.EXT # Encode m4a to ogg name: OGG 256k extension: ogg valid for: m4a command: ENCODER_PATH -i IN -codec:a libvorbis -b:a 256k OUT.EXT # Encode ogg to m4a name: M4A 256k extension: m4a valid for: ogg command: ENCODER_PATH -i IN -strict experimental -codec:a aac -b:a 256k OUT.EXT # Encode ogg or m4a to wma v2 name: Windows Media Audio v2 extension: wma valid for: ogg,m4a command: ENCODER_PATH -i IN -codec:a wmav2 -q:a 0 OUT.EXT""" with open(g.TCFILE, "w") as tcf: tcf.write(config_file_contents) dbg("generated transcoding config file") else: dbg("transcoding config file exists") with open(g.TCFILE, "r") as tcf: g.encoders = [dict(name="None", ext="COPY", valid="*")] e = {} for line in tcf.readlines(): if line.startswith("TRANSCODER_PATH:"): m = re.match("TRANSCODER_PATH:(.*)", line).group(1) g.transcoder_path = m.strip() elif line.startswith("DELETE_ORIGINAL:"): m = re.match("DELETE_ORIGINAL:(.*)", line).group(1) do = m.strip().lower() in ("true", "yes", "enabled", "on") g.delete_orig = do elif line.startswith("name:"): e['name'] = re.match("name:(.*)", line).group(1).strip() elif line.startswith("extension:"): e['ext'] = re.match("extension:(.*)", line).group(1).strip() elif line.startswith("valid for:"): e['valid'] = re.match("valid for:(.*)", line).group(1).strip() elif line.startswith("command:"): e['command'] = re.match("command:(.*)", line).group(1).strip() if "name" in e and "ext" in e and "valid" in e: g.encoders.append(e) e = {} def _init_readline(): """ Enable readline for input history. """ if g.command_line: return if has_readline: g.READLINE_FILE = os.path.join(paths.get_config_dir(), "input_history") if os.path.exists(g.READLINE_FILE): readline.read_history_file(g.READLINE_FILE) dbg(c.g + "Read history file" + c.w) def _process_cl_args(): """ Process command line arguments. """ parser = argparse.ArgumentParser(add_help=False) parser.add_argument('commands', nargs='*') parser.add_argument('--help', '-h', action='store_true') parser.add_argument('--version', '-v', action='store_true') parser.add_argument('--debug', '-d', action='store_true') parser.add_argument('--logging', '-l', action='store_true') parser.add_argument('--no-autosize', action='store_true') parser.add_argument('--no-preload', action='store_true') args = parser.parse_args() if args.version: screen.msgexit(_get_version_info()) elif args.help: screen.msgexit('\n'.join(i[2] for i in helptext())) if args.debug or os.environ.get("mpsytdebug") == "1": xprint(_get_version_info()) g.debug_mode = True g.no_clear_screen = True if args.logging or os.environ.get("mpsytlog") == "1" or g.debug_mode: logfile = os.path.join(tempfile.gettempdir(), "mpsyt.log") logging.basicConfig(level=logging.DEBUG, filename=logfile) logging.getLogger("pafy").setLevel(logging.DEBUG) if args.no_autosize: g.detectable_size = False g.command_line = "playurl" in args.commands or "dlurl" in args.commands if g.command_line: g.no_clear_screen = True if args.no_preload: g.preload_disabled = True g.argument_commands = args.commands def _get_version_info(): """ Return version and platform info. """ pafy_version = pafy.__version__ youtube_dl_version = None if tuple(map(int, pafy_version.split('.'))) >= (0, 5, 0): pafy_version += " (" + pafy.backend + " backend)" if pafy.backend == "youtube-dl": import youtube_dl youtube_dl_version = youtube_dl.version.__version__ out = "mpsyt version : " + __version__ out += "\n notes : " + __notes__ out += "\npafy version : " + pafy_version if youtube_dl_version: out += "\nyoutube-dl version : " + youtube_dl_version out += "\nPython version : " + sys.version out += "\nProcessor : " + platform.processor() out += "\nMachine type : " + platform.machine() out += "\nArchitecture : %s, %s" % platform.architecture() out += "\nPlatform : " + platform.platform() out += "\nsys.stdout.enc : " + sys.stdout.encoding out += "\ndefault enc : " + sys.getdefaultencoding() out += "\nConfig dir : " + paths.get_config_dir() for env in "TERM SHELL LANG LANGUAGE".split(): value = os.environ.get(env) out += "\nenv:%-15s: %s" % (env, value) if value else "" return out mps-youtube-0.2.7.1/mps_youtube/main.py000066400000000000000000000102031273726520200200230ustar00rootroot00000000000000""" mps-youtube. https://github.com/np1/mps-youtube Copyright (C) 2014, 2015 np1 and contributors This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import traceback import locale import sys import os import pafy from . import g, c, commands, screen, history, util from . import __version__, playlists, content try: import readline readline.set_history_length(2000) has_readline = True except ImportError: has_readline = False mswin = os.name == "nt" locale.setlocale(locale.LC_ALL, "") # for date formatting def matchfunction(func, regex, userinput): """ Match userinput against regex. Call func, return True if matches. """ # Not supported in python 3.3 or lower # match = regex.fullmatch(userinput) # if match: match = regex.match(userinput) if match and match.group(0) == userinput: matches = match.groups() util.dbg("input: %s", userinput) util.dbg("function call: %s", func.__name__) util.dbg("regx matches: %s", matches) try: func(*matches) except IndexError: if g.debug_mode: g.content = ''.join(traceback.format_exception( *sys.exc_info())) g.message = util.F('invalid range') g.content = g.content or content.generate_songlist_display() except (ValueError, IOError) as e: if g.debug_mode: g.content = ''.join(traceback.format_exception( *sys.exc_info())) g.message = util.F('cant get track') % str(e) g.content = g.content or\ content.generate_songlist_display(zeromsg=g.message) except pafy.GdataError as e: if g.debug_mode: g.content = ''.join(traceback.format_exception( *sys.exc_info())) g.message = util.F('no data') % e g.content = g.content return True def prompt_for_exit(): """ Ask for exit confirmation. """ g.message = c.r + "Press ctrl-c again to exit" + c.w g.content = content.generate_songlist_display() screen.update() try: userinput = input(c.r + " > " + c.w) except (KeyboardInterrupt, EOFError): commands.misc.quits(showlogo=False) return userinput def main(): """ Main control loop. """ util.set_window_title("mpsyt") if not g.command_line: g.content = content.logo(col=c.g, version=__version__) + "\n\n" g.message = "Enter /search-term to search or [h]elp" screen.update() # open playlists from file playlists.load() #open history from file history.load() arg_inp = ' '.join(g.argument_commands) prompt = "> " arg_inp = arg_inp.replace(r",,", "[mpsyt-comma]") arg_inp = arg_inp.split(",") while True: next_inp = "" if len(arg_inp): next_inp = arg_inp.pop(0).strip() next_inp = next_inp.replace("[mpsyt-comma]", ",") try: userinput = next_inp or input(prompt).strip() except (KeyboardInterrupt, EOFError): userinput = prompt_for_exit() for i in g.commands: if matchfunction(i.function, i.regex, userinput): break else: g.content = g.content or content.generate_songlist_display() if g.command_line: g.content = "" if userinput and not g.command_line: g.message = c.b + "Bad syntax. Enter h for help" + c.w elif userinput and g.command_line: sys.exit("Bad syntax") screen.update() mps-youtube-0.2.7.1/mps_youtube/mpris.py000066400000000000000000000424111273726520200202370ustar00rootroot00000000000000""" mps-youtube. https://github.com/np1/mps-youtube Copyright (C) 2014 nagev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import json import socket import time import copy import re import os from threading import Thread import dbus import dbus.service from dbus.mainloop.glib import DBusGMainLoop IDENTITY = 'mps-youtube' BUS_NAME = 'org.mpris.MediaPlayer2.' + IDENTITY + '.instance' + str(os.getpid()) ROOT_INTERFACE = 'org.mpris.MediaPlayer2' PLAYER_INTERFACE = 'org.mpris.MediaPlayer2.Player' PROPERTIES_INTERFACE = 'org.freedesktop.DBus.Properties' MPRIS_PATH = '/org/mpris/MediaPlayer2' class Mpris2Controller(object): """ Controller for various MPRIS objects. """ def __init__(self): """ Constructs an MPRIS controller. Note, you must call acquire() """ # Do not import in main process to prevent conflict with pyperclip # (https://github.com/mps-youtube/mps-youtube/issues/461) from gi.repository import GLib self.mpris = None self.bus = None self.main_loop = GLib.MainLoop() def release(self): """ Releases all objects from D-Bus and unregisters the bus """ if self.mpris is not None: self.mpris.remove_from_connection() self.mpris = None if self.bus is not None: self.bus.get_bus().release_name(self.bus.get_name()) def acquire(self): """ Connects to D-Bus and registers all components """ self._acquire_bus() self._add_interfaces() def run(self, connection): """ Runs main loop, processing all calls binds on connection (Pipe) and listens player changes """ t = Thread(target=self._run_main_loop) t.daemon = True t.start() self.listenstatus(connection) def listenstatus(self, conn): """ Notifies interfaces that player connection changed """ try: while True: data = conn.recv() if isinstance(data, tuple): name, val = data if name == 'socket': Thread(target=self.mpris.bindmpv, args=(val,)).start() elif name == 'mplayer-fifo': self.mpris.bindfifo(val) elif name == 'mpv-fifo': self.mpris.bindfifo(val, mpv=True) else: self.mpris.setproperty(name, val) except IOError: pass def _acquire_bus(self): """ Connect to D-Bus and set self.bus to be a valid connection """ if self.bus is not None: self.bus.get_bus().request_name(BUS_NAME) else: self.bus = dbus.service.BusName(BUS_NAME, bus=dbus.SessionBus(mainloop=DBusGMainLoop())) def _add_interfaces(self): """ Connects all interfaces to D-Bus """ self.mpris = Mpris2MediaPlayer(self.bus) def _run_main_loop(self): """ Runs glib main loop, ignoring keyboard interrupts """ while True: try: self.main_loop.run() except KeyboardInterrupt: pass class Mpris2MediaPlayer(dbus.service.Object): """ main dbus object for MPRIS2 implementing interfaces: org.mpris.MediaPlayer2 org.mpris.MediaPlayer2.Player """ def __init__(self, bus): """ initializes mpris object on dbus """ dbus.service.Object.__init__(self, bus, MPRIS_PATH) self.socket = None self.fifo = None self.mpv = False self.properties = { ROOT_INTERFACE : { 'read_only' : { 'CanQuit' : False, 'CanSetFullscreen' : False, 'CanRaise' : False, 'HasTrackList' : False, 'Identity' : IDENTITY, 'DesktopEntry' : 'mps-youtube', 'SupportedUriSchemes' : dbus.Array([], 's', 1), 'SupportedMimeTypes' : dbus.Array([], 's', 1), }, 'read_write' : { 'Fullscreen' : False, }, }, PLAYER_INTERFACE : { 'read_only' : { 'PlaybackStatus' : 'Stopped', 'Metadata' : { 'mpris:trackid' : dbus.ObjectPath( '/CurrentPlaylist/UnknownTrack', variant_level=1) }, 'Position' : dbus.Int64(0), 'MinimumRate' : 1.0, 'MaximumRate' : 1.0, 'CanGoNext' : True, 'CanGoPrevious' : True, 'CanPlay' : True, 'CanPause' : True, 'CanSeek' : True, 'CanControl' : True, }, 'read_write' : { 'Rate' : 1.0, 'Volume' : 1.0, }, }, } def bindmpv(self, sockpath): """ init JSON IPC for new versions of mpv >= 0.7 """ self.mpv = True self.socket = socket.socket(socket.AF_UNIX) # wait on socket initialization tries = 0 while tries < 10: time.sleep(.5) try: self.socket.connect(sockpath) break except socket.error: pass tries += 1 else: return try: observe_full = False self._sendcommand(["observe_property", 1, "time-pos"]) for line in self.socket.makefile(): resp = json.loads(line) # deals with bug in mpv 0.7 - 0.7.3 if resp.get('event') == 'property-change' and not observe_full: self._sendcommand(["observe_property", 2, "volume"]) self._sendcommand(["observe_property", 3, "pause"]) self._sendcommand(["observe_property", 4, "seeking"]) observe_full = True if resp.get('event') == 'property-change': self.setproperty(resp['name'], resp['data']) except socket.error: self.socket = None self.mpv = False def bindfifo(self, fifopath, mpv=False): """ init command fifo for mplayer and old versions of mpv """ time.sleep(1) # give it some time so fifo could be properly created try: self.fifo = open(fifopath, 'w') self._sendcommand(['get_property', 'volume']) self.mpv = mpv except IOError: self.fifo = None def setproperty(self, name, val): """ Properly sets properties on player interface don't use this method from dbus interface, all values should be set from player (to keep them correct) """ if name == 'pause': oldval = self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] newval = None if val: newval = 'Paused' else: newval = 'Playing' if newval != oldval: self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] = newval self.PropertiesChanged(PLAYER_INTERFACE, { 'PlaybackStatus': newval }, []) elif name == 'stop': oldval = self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] newval = None if val: newval = 'Stopped' else: newval = 'Playing' if newval != oldval: self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] = newval self.PropertiesChanged(PLAYER_INTERFACE, { 'PlaybackStatus': newval }, ['Metadata', 'Position']) elif name == 'volume' and val is not None: oldval = self.properties[PLAYER_INTERFACE]['read_write']['Volume'] newval = float(val) / 100 if newval != oldval: self.properties[PLAYER_INTERFACE]['read_write']['Volume'] = newval self.PropertiesChanged(PLAYER_INTERFACE, { 'Volume': newval }, []) elif name == 'time-pos' and val: oldval = self.properties[PLAYER_INTERFACE]['read_only']['Position'] newval = dbus.Int64(val * 10**6) if newval != oldval: self.properties[PLAYER_INTERFACE]['read_only']['Position'] = newval if abs(newval - oldval) >= 4 * 10**6: self.Seeked(newval) elif name == 'metadata' and val: trackid, title, length, arturl = val # sanitize ytid - it uses '-_' which are not valid in dbus paths trackid = re.sub('[^a-zA-Z0-9]', '', trackid) oldval = self.properties[PLAYER_INTERFACE]['read_only']['Metadata'] newval = { 'mpris:trackid' : dbus.ObjectPath( '/CurrentPlaylist/ytid/' + trackid, variant_level=1), 'mpris:length' : dbus.Int64(length * 10**6, variant_level=1), 'mpris:artUrl' : dbus.String(arturl, variant_level=1), 'xesam:title' : dbus.String(title, variant_level=1) } if newval != oldval: self.properties[PLAYER_INTERFACE]['read_only']['Metadata'] = newval self.PropertiesChanged(PLAYER_INTERFACE, { 'Metadata': newval }, []) elif name == 'seeking': # send signal to keep time-pos synced between player and client if not val: self.Seeked(self.properties[PLAYER_INTERFACE]['read_only']['Position']) def _sendcommand(self, command): """ sends commands to binded player """ if self.socket: self.socket.send(json.dumps({"command": command}).encode() + b'\n') elif self.fifo: command = command[:] for x, i in enumerate(command): if i is True: command[x] = 'yes' if self.mpv else 1 elif i is False: command[x] = 'no' if self.mpv else 0 cmd = " ".join([str(i) for i in command]) + '\n' self.fifo.write(cmd) self.fifo.flush() # # implementing org.mpris.MediaPlayer2 # @dbus.service.method(dbus_interface=ROOT_INTERFACE) def Raise(self): """ Brings the media player's user interface to the front using any appropriate mechanism available. """ pass @dbus.service.method(dbus_interface=ROOT_INTERFACE) def Quit(self): """ Causes the media player to stop running. """ pass # # implementing org.mpris.MediaPlayer2.Player # @dbus.service.method(dbus_interface=PLAYER_INTERFACE) def Next(self): """ Skips to the next track in the tracklist. """ self._sendcommand(["quit"]) @dbus.service.method(PLAYER_INTERFACE) def Previous(self): """ Skips to the previous track in the tracklist. """ self._sendcommand(["quit", 42]) @dbus.service.method(PLAYER_INTERFACE) def Pause(self): """ Pauses playback. If playback is already paused, this has no effect. """ if self.mpv: self._sendcommand(["set_property", "pause", True]) else: if self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] != 'Paused': self._sendcommand(['pause']) @dbus.service.method(PLAYER_INTERFACE) def PlayPause(self): """ Pauses playback. If playback is already paused, resumes playback. """ if self.mpv: self._sendcommand(["cycle", "pause"]) else: self._sendcommand(["pause"]) @dbus.service.method(PLAYER_INTERFACE) def Stop(self): """ Stops playback. """ self._sendcommand(["quit", 43]) @dbus.service.method(PLAYER_INTERFACE) def Play(self): """ Starts or resumes playback. """ if self.mpv: self._sendcommand(["set_property", "pause", False]) else: if self.properties[PLAYER_INTERFACE]['read_only']['PlaybackStatus'] != 'Playing': self._sendcommand(['pause']) @dbus.service.method(PLAYER_INTERFACE, in_signature='x') def Seek(self, offset): """ Offset - x (offset) The number of microseconds to seek forward. Seeks forward in the current track by the specified number of microseconds. """ self._sendcommand(["seek", offset / 10**6]) @dbus.service.method(PLAYER_INTERFACE, in_signature='ox') def SetPosition(self, track_id, position): """ TrackId - o (track_id) The currently playing track's identifier. If this does not match the id of the currently-playing track, the call is ignored as "stale". Position - x (position) Track position in microseconds. Sets the current track position in microseconds. """ if track_id == self.properties[PLAYER_INTERFACE]['read_only']['Metadata']['mpris:trackid']: self._sendcommand(["seek", position / 10**6, 2]) @dbus.service.method(PLAYER_INTERFACE, in_signature='s') def OpenUri(self, uri): """ Uri - s (uri) Uri of the track to load. Opens the Uri given as an argument. """ pass @dbus.service.signal(PLAYER_INTERFACE, signature='x') def Seeked(self, position): """ Position - x (position) The new position, in microseconds. Indicates that the track position has changed in a way that is inconsistant with the current playing state. """ pass # # implementing org.freedesktop.DBus.Properties # @dbus.service.method(dbus_interface=PROPERTIES_INTERFACE, in_signature='ss', out_signature='v') def Get(self, interface_name, property_name): """ getter for org.freedesktop.DBus.Properties on this object """ return self.GetAll(interface_name)[property_name] @dbus.service.method(dbus_interface=PROPERTIES_INTERFACE, in_signature='s', out_signature='a{sv}') def GetAll(self, interface_name): """ getter for org.freedesktop.DBus.Properties on this object """ if interface_name in self.properties: t = copy.copy(self.properties[interface_name]['read_only']) t.update(self.properties[interface_name]['read_write']) return t else: raise dbus.exceptions.DBusException( 'com.example.UnknownInterface', 'This object does not implement the %s interface' % interface_name) @dbus.service.method(dbus_interface=PROPERTIES_INTERFACE, in_signature='ssv') def Set(self, interface_name, property_name, new_value): """ setter for org.freedesktop.DBus.Properties on this object """ if interface_name in self.properties: if property_name in self.properties[interface_name]['read_write']: if property_name == 'Volume': self._sendcommand(["set_property", "volume", new_value * 100]) if self.fifo: # fix for mplayer (force update) self._sendcommand(['get_property', 'volume']) else: raise dbus.exceptions.DBusException( 'com.example.UnknownInterface', 'This object does not implement the %s interface' % interface_name) @dbus.service.signal(dbus_interface=PROPERTIES_INTERFACE, signature='sa{sv}as') def PropertiesChanged(self, interface_name, changed_properties, invalidated_properties): """ signal for org.freedesktop.DBus.Properties on this object this informs of changed properties """ pass def main(connection): """ runs mpris interface and listens for changes connection - pipe to communicate with this module """ mprisctl = Mpris2Controller() try: mprisctl.acquire() except dbus.exceptions.DBusException: print('mpris interface couldn\'t be initialized. Is dbus properly configured?') return mprisctl.run(connection) mprisctl.release() mps-youtube-0.2.7.1/mps_youtube/paths.py000066400000000000000000000030271273726520200202240ustar00rootroot00000000000000import os mswin = os.name == "nt" def get_default_ddir(): """ Get system default Download directory, append mps dir. """ user_home = os.path.expanduser("~") join, exists = os.path.join, os.path.exists if mswin: return join(user_home, "Downloads", "mps") USER_DIRS = join(user_home, ".config", "user-dirs.dirs") DOWNLOAD_HOME = join(user_home, "Downloads") # define ddir by (1) env var, (2) user-dirs.dirs file, # (3) existing ~/Downloads dir (4) ~ if 'XDG_DOWNLOAD_DIR' in os.environ: ddir = os.environ['XDG_DOWNLOAD_DIR'] elif exists(USER_DIRS): lines = open(USER_DIRS).readlines() defn = [x for x in lines if x.startswith("XDG_DOWNLOAD_DIR")] if len(defn) == 1: ddir = defn[0].split("=")[1].replace('"', '') ddir = ddir.replace("$HOME", user_home).strip() else: ddir = DOWNLOAD_HOME if exists(DOWNLOAD_HOME) else user_home else: ddir = DOWNLOAD_HOME if exists(DOWNLOAD_HOME) else user_home ddir = ddir return os.path.join(ddir, "mps") def get_config_dir(): """ Get user's configuration directory. Migrate to new mps name if old.""" if mswin: confdir = os.environ["APPDATA"] elif 'XDG_CONFIG_HOME' in os.environ: confdir = os.environ['XDG_CONFIG_HOME'] else: confdir = os.path.join(os.path.expanduser("~"), '.config') mps_confdir = os.path.join(confdir, "mps-youtube") os.makedirs(mps_confdir, exist_ok=True) return mps_confdir mps-youtube-0.2.7.1/mps_youtube/player.py000066400000000000000000000471021273726520200204030ustar00rootroot00000000000000import os import sys import random import tempfile import subprocess import logging import json import re import socket import math import time import shlex from urllib.error import HTTPError, URLError from . import g, screen, c, streams, history, content, paths, config, util mswin = os.name == "nt" not_utf8_environment = mswin or "UTF-8" not in sys.stdout.encoding def play_range(songlist, shuffle=False, repeat=False, override=False): """ Play a range of songs, exit cleanly on keyboard interrupt. """ if shuffle: random.shuffle(songlist) n = 0 while 0 <= n <= len(songlist)-1: song = songlist[n] g.content = _playback_progress(n, songlist, repeat=repeat) if not g.command_line: screen.update(fill_blank=False) hasnext = len(songlist) > n + 1 if hasnext: streams.preload(songlist[n + 1], override=override) util.set_window_title(song.title + " - mpsyt") try: returncode = _playsong(song, override=override) except KeyboardInterrupt: logging.info("Keyboard Interrupt") util.xprint(c.w + "Stopping... ") screen.reset_terminal() g.message = c.y + "Playback halted" + c.w break util.set_window_title("mpsyt") if returncode == 42: n -= 1 elif returncode == 43: break else: n += 1 if n == -1: n = len(songlist) - 1 if repeat else 0 elif n == len(songlist) and repeat: n = 0 def _playback_progress(idx, allsongs, repeat=False): """ Generate string to show selected tracks, indicate current track. """ # pylint: disable=R0914 # too many local variables cw = util.getxy().width out = " %s%-XXs%s%s\n".replace("XX", str(cw - 9)) out = out % (c.ul, "Title", "Time", c.w) show_key_help = (util.is_known_player(config.PLAYER.get) and config.SHOW_MPLAYER_KEYS.get) multi = len(allsongs) > 1 for n, song in enumerate(allsongs): length_orig = util.fmt_time(song.length) length = " " * (8 - len(length_orig)) + length_orig i = util.uea_pad(cw - 14, song.title), length, length_orig fmt = (c.w, " ", c.b, i[0], c.w, c.y, i[1], c.w) if n == idx: fmt = (c.y, "> ", c.p, i[0], c.w, c.p, i[1], c.w) cur = i out += "%s%s%s%s%s %s%s%s\n" % fmt out += "\n" * (3 - len(allsongs)) pos = 8 * " ", c.y, idx + 1, c.w, c.y, len(allsongs), c.w playing = "{}{}{}{} of {}{}{}\n\n".format(*pos) if multi else "\n\n" keys = _mplayer_help(short=(not multi and not repeat)) out = out if multi else content.generate_songlist_display(song=allsongs[0]) if show_key_help: out += "\n" + keys else: playing = "{}{}{}{} of {}{}{}\n".format(*pos) if multi else "\n" out += "\n" + " " * (cw - 19) if multi else "" fmt = playing, c.r, cur[0].strip()[:cw - 19], c.w, c.w, cur[2], c.w out += "%s %s%s%s %s[%s]%s" % fmt out += " REPEAT MODE" if repeat else "" return out def _mplayer_help(short=True): """ Mplayer help. """ # pylint: disable=W1402 volume = "[{0}9{1}] volume [{0}0{1}]" volume = volume if short else volume + " [{0}q{1}] return" seek = "[{0}\u2190{1}] seek [{0}\u2192{1}]" pause = "[{0}\u2193{1}] SEEK [{0}\u2191{1}] [{0}space{1}] pause" if not_utf8_environment: seek = "[{0}<-{1}] seek [{0}->{1}]" pause = "[{0}DN{1}] SEEK [{0}UP{1}] [{0}space{1}] pause" single = "[{0}q{1}] return" next_prev = "[{0}>{1}] next/prev [{0}<{1}]" # ret = "[{0}q{1}] %s" % ("return" if short else "next track") ret = single if short else next_prev fmt = " %-20s %-20s" lines = fmt % (seek, volume) + "\n" + fmt % (pause, ret) return lines.format(c.g, c.w) def _playsong(song, failcount=0, override=False): """ Play song using config.PLAYER called with args config.PLAYERARGS.""" # pylint: disable=R0911,R0912 if not config.PLAYER.get or not util.has_exefile(config.PLAYER.get): g.message = "Player not configured! Enter %sset player "\ "%s to set a player" % (c.g, c.w) return if config.NOTIFIER.get: subprocess.Popen(shlex.split(config.NOTIFIER.get) + [song.title]) # don't interrupt preloading: while song.ytid in g.preloading: screen.writestatus("fetching item..") time.sleep(0.1) try: streams.get(song, force=failcount, callback=screen.writestatus) except (IOError, URLError, HTTPError, socket.timeout) as e: util.dbg("--ioerror in _playsong call to streams.get %s", str(e)) if "Youtube says" in str(e): g.message = util.F('cant get track') % (song.title + " " + str(e)) return elif failcount < g.max_retries: util.dbg("--ioerror - trying next stream") failcount += 1 return _playsong(song, failcount=failcount, override=override) elif "pafy" in str(e): g.message = str(e) + " - " + song.ytid return except ValueError: g.message = util.F('track unresolved') util.dbg("----valueerror in _playsong call to streams.get") return try: video = ((config.SHOW_VIDEO.get and override != "audio") or (override in ("fullscreen", "window", "forcevid"))) m4a = "mplayer" not in config.PLAYER.get cached = g.streams[song.ytid] stream = streams.select(cached, q=failcount, audio=(not video), m4a_ok=m4a) # handle no audio stream available, or m4a with mplayer # by switching to video stream and suppressing video output. if (not stream or failcount) and not video: util.dbg(c.r + "no audio or mplayer m4a, using video stream" + c.w) override = "a-v" video = True stream = streams.select(cached, q=failcount, audio=False, maxres=1600) if not stream: raise IOError("No streams available") except (HTTPError) as e: # Fix for invalid streams (gh-65) util.dbg("----htterror in _playsong call to gen_real_args %s", str(e)) if failcount < g.max_retries: failcount += 1 return _playsong(song, failcount=failcount, override=override) else: g.message = str(e) return except IOError as e: # this may be cause by attempting to play a https stream with # mplayer # ==== errmsg = e.message if hasattr(e, "message") else str(e) g.message = c.r + str(errmsg) + c.w return size = streams.get_size(song.ytid, stream['url']) songdata = (song.ytid, stream['ext'] + " " + stream['quality'], int(size / (1024 ** 2))) songdata = "%s; %s; %s Mb" % songdata screen.writestatus(songdata) cmd = _generate_real_playerargs(song, override, stream, video) returncode = _launch_player(song, songdata, cmd) failed = returncode not in (0, 42, 43) if failed and failcount < g.max_retries: util.dbg(c.r + "stream failed to open" + c.w) util.dbg("%strying again (attempt %s)%s", c.r, (2 + failcount), c.w) screen.writestatus("error: retrying") time.sleep(1.2) failcount += 1 return _playsong(song, failcount=failcount, override=override) history.add(song) return returncode def _generate_real_playerargs(song, override, stream, isvideo): """ Generate args for player command. Return args. """ # pylint: disable=R0914 # pylint: disable=R0912 if "uiressl=yes" in stream['url'] and "mplayer" in config.PLAYER.get: ver = g.mplayer_version # Mplayer too old to support https if not (ver > (1,1) if isinstance(ver, tuple) else ver >= 37294): raise IOError("%s : Sorry mplayer doesn't support this stream. " "Use mpv or update mplayer to a newer version" % song.title) # pylint: disable=E1103 # pylint thinks PLAYERARGS.get might be bool args = config.PLAYERARGS.get.strip().split() known_player = util.is_known_player(config.PLAYER.get) if known_player: pd = g.playerargs_defaults[known_player] args.extend((pd["title"], song.title)) if pd['geo'] not in args: geometry = config.WINDOW_SIZE.get or "" if config.WINDOW_POS.get: wp = config.WINDOW_POS.get xx = "+1" if "left" in wp else "-1" yy = "+1" if "top" in wp else "-1" geometry += xx + yy if geometry: args.extend((pd['geo'], geometry)) # handle no audio stream available if override == "a-v": util.list_update(pd["novid"], args) elif ((config.FULLSCREEN.get and override != "window") or override == "fullscreen"): util.list_update(pd["fs"], args) # prevent ffmpeg issue (https://github.com/mpv-player/mpv/issues/579) if not isvideo and stream['ext'] == "m4a": util.dbg("%susing ignidx flag%s") util.list_update(pd["ignidx"], args) if "mplayer" in config.PLAYER.get: util.list_update("-really-quiet", args, remove=True) util.list_update("-noquiet", args) util.list_update("-prefer-ipv4", args) elif "mpv" in config.PLAYER.get: if "--ytdl" in g.mpv_options: util.list_update("--no-ytdl", args) msglevel = pd["msglevel"]["<0.4"] # undetected (negative) version number assumed up-to-date if g.mpv_version[0:2] < (0, 0) or g.mpv_version[0:2] >= (0, 4): msglevel = pd["msglevel"][">=0.4"] if not g.debug_mode: if g.mpv_usesock: util.list_update("--really-quiet", args) else: util.list_update("--really-quiet", args, remove=True) util.list_update(msglevel, args) elif "vlc" in config.PLAYER.get: util.list_update("--play-and-exit", args) return [config.PLAYER.get] + args + [stream['url']] def _get_input_file(): """ Check for existence of custom input file. Return file name of temp input file with mpsyt mappings included """ confpath = conf = '' if "mpv" in config.PLAYER.get: confpath = os.path.join(paths.get_config_dir(), "mpv-input.conf") elif "mplayer" in config.PLAYER.get: confpath = os.path.join(paths.get_config_dir(), "mplayer-input.conf") if os.path.isfile(confpath): util.dbg("using %s for input key file", confpath) with open(confpath) as conffile: conf = conffile.read() + '\n' conf = conf.replace("quit", "quit 43") conf = conf.replace("playlist_prev", "quit 42") conf = conf.replace("pt_step -1", "quit 42") conf = conf.replace("playlist_next", "quit") conf = conf.replace("pt_step 1", "quit") standard_cmds = ['q quit 43\n', '> quit\n', '< quit 42\n', 'NEXT quit\n', 'PREV quit 42\n', 'ENTER quit\n'] bound_keys = [i.split()[0] for i in conf.splitlines() if i.split()] for i in standard_cmds: key = i.split()[0] if key not in bound_keys: conf += i with tempfile.NamedTemporaryFile('w', prefix='mpsyt-input', delete=False) as tmpfile: tmpfile.write(conf) return tmpfile.name def _launch_player(song, songdata, cmd): """ Launch player application. """ util.dbg("playing %s", song.title) util.dbg("calling %s", " ".join(cmd)) # Fix UnicodeEncodeError when title has characters # not supported by encoding cmd = [util.xenc(i) for i in cmd] arturl = "http://i.ytimg.com/vi/%s/default.jpg" % song.ytid input_file = None if ("mplayer" in config.PLAYER.get) or ("mpv" in config.PLAYER.get): input_file = _get_input_file() sockpath = None fifopath = None try: if "mplayer" in config.PLAYER.get: cmd.append('-input') if mswin: # Mplayer does not recognize path starting with drive letter, # or with backslashes as a delimiter. input_file = input_file[2:].replace('\\', '/') cmd.append('conf=' + input_file) if g.mprisctl: fifopath = tempfile.mktemp('.fifo', 'mpsyt-mplayer') os.mkfifo(fifopath) cmd.extend(['-input', 'file=' + fifopath]) g.mprisctl.send(('mplayer-fifo', fifopath)) g.mprisctl.send(('metadata', (song.ytid, song.title, song.length, arturl))) p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1) _player_status(p, songdata + "; ", song.length) returncode = p.wait() elif "mpv" in config.PLAYER.get: cmd.append('--input-conf=' + input_file) if g.mpv_usesock: sockpath = tempfile.mktemp('.sock', 'mpsyt-mpv') cmd.append(g.mpv_usesock + '=' + sockpath) with open(os.devnull, "w") as devnull: p = subprocess.Popen(cmd, shell=False, stderr=devnull) if g.mprisctl: g.mprisctl.send(('socket', sockpath)) g.mprisctl.send(('metadata', (song.ytid, song.title, song.length, arturl))) else: if g.mprisctl: fifopath = tempfile.mktemp('.fifo', 'mpsyt-mpv') os.mkfifo(fifopath) cmd.append('--input-file=' + fifopath) g.mprisctl.send(('mpv-fifo', fifopath)) g.mprisctl.send(('metadata', (song.ytid, song.title, song.length, arturl))) p = subprocess.Popen(cmd, shell=False, stderr=subprocess.PIPE, bufsize=1) _player_status(p, songdata + "; ", song.length, mpv=True, sockpath=sockpath) returncode = p.wait() else: with open(os.devnull, "w") as devnull: returncode = subprocess.call(cmd, stderr=devnull) p = None return returncode except OSError: g.message = util.F('no player') % config.PLAYER.get return None finally: if input_file: os.unlink(input_file) # May not exist if mpv has not yet created the file if sockpath and os.path.exists(sockpath): os.unlink(sockpath) if fifopath: os.unlink(fifopath) if g.mprisctl: g.mprisctl.send(('stop', True)) if p and p.poll() is None: p.terminate() # make sure to kill mplayer if mpsyt crashes def _player_status(po_obj, prefix, songlength=0, mpv=False, sockpath=None): """ Capture time progress from player output. Write status line. """ # pylint: disable=R0914, R0912 re_mplayer = re.compile(r"A:\s*(?P\d+)\.\d\s*") re_mpv = re.compile(r".{,15}AV?:\s*(\d\d):(\d\d):(\d\d)") re_volume = re.compile(r"Volume:\s*(?P\d+)\s*%") re_player = re_mpv if mpv else re_mplayer last_displayed_line = None buff = '' volume_level = None last_pos = None if sockpath: s = socket.socket(socket.AF_UNIX) tries = 0 while tries < 10 and po_obj.poll() is None: time.sleep(.5) try: s.connect(sockpath) break except socket.error: pass tries += 1 else: return try: observe_full = False cmd = {"command": ["observe_property", 1, "time-pos"]} s.send(json.dumps(cmd).encode() + b'\n') volume_level = elapsed_s = None for line in s.makefile(): resp = json.loads(line) # deals with bug in mpv 0.7 - 0.7.3 if resp.get('event') == 'property-change' and not observe_full: cmd = {"command": ["observe_property", 2, "volume"]} s.send(json.dumps(cmd).encode() + b'\n') observe_full = True if resp.get('event') == 'property-change' and resp['id'] == 1: elapsed_s = int(resp['data']) elif resp.get('event') == 'property-change' and resp['id'] == 2: volume_level = int(resp['data']) if elapsed_s: line = _make_status_line(elapsed_s, prefix, songlength, volume=volume_level) if line != last_displayed_line: screen.writestatus(line) last_displayed_line = line except socket.error: pass else: elapsed_s = 0 while po_obj.poll() is None: stdstream = po_obj.stderr if mpv else po_obj.stdout char = stdstream.read(1).decode("utf-8", errors="ignore") if char in '\r\n': mv = re_volume.search(buff) if mv: volume_level = int(mv.group("volume")) match_object = re_player.match(buff) if match_object: try: h, m, s = map(int, match_object.groups()) elapsed_s = h * 3600 + m * 60 + s except ValueError: try: elapsed_s = int(match_object.group('elapsed_s') or '0') except ValueError: continue line = _make_status_line(elapsed_s, prefix, songlength, volume=volume_level) if line != last_displayed_line: screen.writestatus(line) last_displayed_line = line if buff.startswith('ANS_volume='): volume_level = round(float(buff.split('=')[1])) paused = ("PAUSE" in buff) or ("Paused" in buff) if (elapsed_s != last_pos or paused) and g.mprisctl: last_pos = elapsed_s g.mprisctl.send(('pause', paused)) g.mprisctl.send(('volume', volume_level)) g.mprisctl.send(('time-pos', elapsed_s)) buff = '' else: buff += char def _make_status_line(elapsed_s, prefix, songlength=0, volume=None): """ Format progress line output. """ # pylint: disable=R0914 display_s = elapsed_s display_h = display_m = 0 if elapsed_s >= 60: display_m = display_s // 60 display_s %= 60 if display_m >= 100: display_h = display_m // 60 display_m %= 60 pct = (float(elapsed_s) / songlength * 100) if songlength else 0 status_line = "%02i:%02i:%02i %s" % ( display_h, display_m, display_s, ("[%.0f%%]" % pct).ljust(6) ) if volume: vol_suffix = " vol: %d%%" % volume else: vol_suffix = "" cw = util.getxy().width prog_bar_size = cw - len(prefix) - len(status_line) - len(vol_suffix) - 7 progress = int(math.ceil(pct / 100 * prog_bar_size)) status_line += " [%s]" % ("=" * (progress - 1) + ">").ljust(prog_bar_size, ' ') return prefix + status_line + vol_suffix mps-youtube-0.2.7.1/mps_youtube/playlist.py000066400000000000000000000020461273726520200207460ustar00rootroot00000000000000import time class Playlist(object): """ Representation of a playist, has list of songs. """ def __init__(self, name=None, songs=None): """ class members. """ self.name = name self.creation = time.time() self.songs = songs or [] def __len__(self): """ Return number of tracks. """ return len(self.songs) def __getitem__(self, sliced): return self.songs[sliced] def __setitem__(self, position, item): self.songs[position] = item def __iter__(self): for i in self.songs: yield i @property def duration(self): """ Sum duration of the playlist. """ duration = sum(s.length for s in self.songs) duration = time.strftime('%H:%M:%S', time.gmtime(int(duration))) return duration class Video(object): """ Class to represent a YouTube video. """ def __init__(self, ytid, title, length): """ class members. """ self.ytid = ytid self.title = title self.length = int(length) mps-youtube-0.2.7.1/mps_youtube/playlists.py000066400000000000000000000050741273726520200211350ustar00rootroot00000000000000import os import sys import pickle from . import g, c, screen, util from .playlist import Playlist, Video def save(): """ Save playlists. Called each time a playlist is saved or deleted. """ with open(g.PLFILE, "wb") as plf: pickle.dump(g.userpl, plf, protocol=2) util.dbg(c.r + "Playlist saved\n---" + c.w) def load(): """ Open playlists. Called once on script invocation. """ _convert_playlist_to_v2() try: with open(g.PLFILE, "rb") as plf: g.userpl = pickle.load(plf) except IOError: # no playlist found, create a blank one if not os.path.isfile(g.PLFILE): g.userpl = {} save() except AttributeError: # playlist is from a time when this module was __main__ # https://github.com/np1/mps-youtube/issues/214 import __main__ __main__.Playlist = Playlist __main__.Video = Video from . import main main.Playlist = Playlist main.Video = Video with open(g.PLFILE, "rb") as plf: g.userpl = pickle.load(plf) save() screen.msgexit("Updated playlist file. Please restart mpsyt", 1) except EOFError: screen.msgexit("Error opening playlists from %s" % g.PLFILE, 1) # remove any cached urls from playlist file, these are now # stored in a separate cache file do_save = False for k, v in g.userpl.items(): for song in v.songs: if hasattr(song, "urls"): util.dbg("remove %s: %s", k, song.urls) del song.urls do_save = True if do_save: save() def _convert_playlist_to_v2(): """ Convert previous playlist file to v2 playlist. """ # skip if previously done if os.path.isfile(g.PLFILE): return # skip if no playlist files exist elif not os.path.isfile(g.OLD_PLFILE): return try: with open(g.OLD_PLFILE, "rb") as plf: old_playlists = pickle.load(plf) except IOError: sys.exit("Couldn't open old playlist file") # rename old playlist file backup = g.OLD_PLFILE + "_v1_backup" if os.path.isfile(backup): sys.exit("Error, backup exists but new playlist exists not!") os.rename(g.OLD_PLFILE, backup) # do the conversion for plname, plitem in old_playlists.items(): songs = [] for video in plitem.songs: v = Video(video['link'], video['title'], video['duration']) songs.append(v) g.userpl[plname] = Playlist(plname, songs) # save as v2 save() mps-youtube-0.2.7.1/mps_youtube/screen.py000066400000000000000000000031631273726520200203650ustar00rootroot00000000000000import subprocess import os import sys from . import g, content, config, util mswin = os.name == "nt" def update(fill_blank=True): """ Display content, show message, blank screen.""" clear() if isinstance(g.content, content.PaginatedContent): util.xprint(g.content.getPage(g.current_page)) g.rprompt = content.page_msg(g.current_page) elif g.content: util.xprint(g.content) g.content = False if g.message or g.rprompt: out = g.message or '' blanks = util.getxy().width - len(out) - len(g.rprompt or '') out += ' ' * blanks + (g.rprompt or '') util.xprint(out) elif fill_blank: util.xprint("") g.message = g.rprompt = False def clear(): """Clear all text from screen.""" if g.no_clear_screen: util.xprint('--\n') else: util.xprint('\n' * 200) def reset_terminal(): """ Reset terminal control character and modes for non Win OS's. """ if not mswin: subprocess.call(["tset", "-c"]) def writestatus(text, mute=False): """ Update status line. """ if not mute and config.SHOW_STATUS.get: _writeline(text) def _writeline(text): """ Print text on same line. """ width = util.getxy().width spaces = width - len(text) - 1 if mswin: # Avoids creating new line every time it is run # TODO: Figure out why this is needed spaces =- 1 text = text[:width - 3] sys.stdout.write(" " + text + (" " * spaces) + "\r") sys.stdout.flush() def msgexit(msg, code=0): """ Print a message and exit. """ util.xprint(msg) sys.exit(code) mps-youtube-0.2.7.1/mps_youtube/streams.py000066400000000000000000000134341273726520200205660ustar00rootroot00000000000000import time import threading from urllib.request import urlopen from . import g, c, screen, config, util def prune(): """ Keep cache size in check. """ while len(g.pafs) > g.max_cached_streams: g.pafs.popitem(last=False) while len(g.streams) > g.max_cached_streams: g.streams.popitem(last=False) # prune time expired items now = time.time() oldpafs = [k for k in g.pafs if g.pafs[k].expiry < now] if len(oldpafs): util.dbg(c.r + "%s old pafy items pruned%s", len(oldpafs), c.w) for oldpaf in oldpafs: g.pafs.pop(oldpaf, 0) oldstreams = [k for k in g.streams if g.streams[k]['expiry'] < now] if len(oldstreams): util.dbg(c.r + "%s old stream items pruned%s", len(oldstreams), c.w) for oldstream in oldstreams: g.streams.pop(oldstream, 0) util.dbg(c.b + "paf: %s, streams: %s%s", len(g.pafs), len(g.streams), c.w) def get(vid, force=False, callback=None, threeD=False): """ Get all streams as a dict. callback function passed to get_pafy. """ now = time.time() ytid = vid.ytid have_stream = g.streams.get(ytid) and g.streams[ytid]['expiry'] > now prfx = "preload: " if not callback else "" if not force and have_stream: ss = str(int(g.streams[ytid]['expiry'] - now) // 60) util.dbg("%s%sGot streams from cache (%s mins left)%s", c.g, prfx, ss, c.w) return g.streams.get(ytid)['meta'] p = util.get_pafy(vid, force=force, callback=callback) ps = p.allstreams if threeD else [x for x in p.allstreams if not x.threed] try: # test urls are valid [x.url for x in ps] except TypeError: # refetch if problem util.dbg("%s****Type Error in get_streams. Retrying%s", c.r, c.w) p = util.get_pafy(vid, force=True, callback=callback) ps = p.allstreams if threeD else [x for x in p.allstreams if not x.threed] streams = [{"url": s.url, "ext": s.extension, "quality": s.quality, "rawbitrate": s.rawbitrate, "mtype": s.mediatype, "size": -1} for s in ps] g.streams[ytid] = dict(expiry=p.expiry, meta=streams) prune() return streams def select(slist, q=0, audio=False, m4a_ok=True, maxres=None): """ Select a stream from stream list. """ maxres = maxres or config.MAX_RES.get slist = slist['meta'] if isinstance(slist, dict) else slist def okres(x): """ Return True if resolution is within user specified maxres. """ return int(x['quality'].split("x")[1]) <= maxres def getq(x): """ Return height aspect of resolution, eg 640x480 => 480. """ return int(x['quality'].split("x")[1]) def getbitrate(x): """Return the bitrate of a stream.""" return x['rawbitrate'] if audio: streams = [x for x in slist if x['mtype'] == "audio"] if not m4a_ok: streams = [x for x in streams if not x['ext'] == "m4a"] if not config.AUDIO_FORMAT.get == "auto": if m4a_ok and config.AUDIO_FORMAT.get == "m4a": streams = [x for x in streams if x['ext'] == "m4a"] if config.AUDIO_FORMAT.get == "webm": streams = [x for x in streams if x['ext'] == "webm"] if not streams: streams = [x for x in slist if x['mtype'] == "audio"] streams = sorted(streams, key=getbitrate, reverse=True) else: streams = [x for x in slist if x['mtype'] == "normal" and okres(x)] streams = sorted(streams, key=getq, reverse=True) util.dbg("select stream, q: %s, audio: %s, len: %s", q, audio, len(streams)) try: ret = streams[q] except IndexError: ret = streams[0] if q and len(streams) else None return ret def get_size(ytid, url, preloading=False): """ Get size of stream, try stream cache first. """ # try cached value stream = [x for x in g.streams[ytid]['meta'] if x['url'] == url][0] size = stream['size'] prefix = "preload: " if preloading else "" if not size == -1: util.dbg("%s%susing cached size: %s%s", c.g, prefix, size, c.w) else: screen.writestatus("Getting content length", mute=preloading) stream['size'] = _get_content_length(url, preloading=preloading) util.dbg("%s%s - content-length: %s%s", c.y, prefix, stream['size'], c.w) return stream['size'] def _get_content_length(url, preloading=False): """ Return content length of a url. """ prefix = "preload: " if preloading else "" util.dbg(c.y + prefix + "getting content-length header" + c.w) response = urlopen(url) headers = response.headers cl = headers['content-length'] return int(cl) def preload(song, delay=2, override=False): """ Get streams. """ args = (song, delay, override) t = threading.Thread(target=_preload, args=args) t.daemon = True t.start() def _preload(song, delay, override): """ Get streams (runs in separate thread). """ if g.preload_disabled: return ytid = song.ytid g.preloading.append(ytid) time.sleep(delay) video = config.SHOW_VIDEO.get video = True if override in ("fullscreen", "window", "forcevid") else video video = False if override == "audio" else video try: m4a = "mplayer" not in config.PLAYER.get streamlist = get(song) stream = select(streamlist, audio=not video, m4a_ok=m4a) if not stream and not video: # preload video stream, no audio available stream = select(streamlist, audio=False) get_size(ytid, stream['url'], preloading=True) except (ValueError, AttributeError, IOError) as e: util.dbg(e) # Fail silently on preload finally: g.preloading.remove(song.ytid) mps-youtube-0.2.7.1/mps_youtube/terminalsize.py000066400000000000000000000054631273726520200216210ustar00rootroot00000000000000# https://gist.github.com/jtriley/1108174 """ Terminal Size. """ import os import sys import shlex import shutil import struct import platform import subprocess def get_terminal_size(): """ getTerminalSize(). - get width and height of console - works on linux,os x,windows,cygwin(windows) originally retrieved from: http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python """ if sys.version_info >= (3,3): return shutil.get_terminal_size() current_os = platform.system() tuple_xy = None if current_os == 'Windows': tuple_xy = _get_terminal_size_windows() if tuple_xy is None: tuple_xy = _get_terminal_size_tput() # needed for window's python in cygwin's xterm! else: tuple_xy = _get_terminal_size_linux() if tuple_xy is None: tuple_xy = (80, 25) # default value return tuple_xy def _get_terminal_size_windows(): """ Get terminal size on MS Windows. """ # pylint: disable=R0914 # too many local variables try: from ctypes import windll, create_string_buffer # stdin handle is -10 # stdout handle is -11 # stderr handle is -12 h = windll.kernel32.GetStdHandle(-12) csbi = create_string_buffer(22) res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) if res: (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw) sizex = right - left + 1 sizey = bottom - top + 1 return sizex, sizey except: pass def _get_terminal_size_tput(): """ Get terminal size using tput. """ # src: http://stackoverflow.com/questions/263890/ # how-do-i-find-the-width-height-of-a-terminal-window try: cols = int(subprocess.check_call(shlex.split('tput cols'))) rows = int(subprocess.check_call(shlex.split('tput lines'))) return (cols, rows) except: pass def _get_terminal_size_linux(): """ Get terminal size Linux. """ def ioctl_GWINSZ(fd): """ ioctl_GWINSZ. """ try: import fcntl import termios cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) return cr except: pass cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) if not cr or cr == (0, 0): try: fd = os.open(os.ctermid(), os.O_RDONLY) cr = ioctl_GWINSZ(fd) os.close(fd) except: pass if not cr or cr == (0, 0): try: cr = (os.environ['LINES'], os.environ['COLUMNS']) except: return return int(cr[1]), int(cr[0]) mps-youtube-0.2.7.1/mps_youtube/test/000077500000000000000000000000001273726520200175105ustar00rootroot00000000000000mps-youtube-0.2.7.1/mps_youtube/test/test_main.py000066400000000000000000000017001273726520200220430ustar00rootroot00000000000000import unittest import mps_youtube.main as mps class TestMain(unittest.TestCase): def test_fmt_time(self): self.assertEqual(mps.fmt_time(0), '00:00') self.assertEqual(mps.fmt_time(59), '00:59') self.assertEqual(mps.fmt_time(100), '01:40') self.assertEqual(mps.fmt_time(1000), '16:40') self.assertEqual(mps.fmt_time(5000), '83:20') self.assertEqual(mps.fmt_time(6500), '1:48:20') def test_num_repr(self): self.assertEqual(mps.num_repr(0), '0') self.assertEqual(mps.num_repr(1001), '1001') self.assertEqual(mps.num_repr(10001), '10k') self.assertEqual(mps.num_repr(100001), '100k') self.assertEqual(mps.num_repr(1000001), '1.0m') self.assertEqual(mps.num_repr(10000001), '10m') self.assertEqual(mps.num_repr(100000001), '100m') self.assertEqual(mps.num_repr(1000000001), '1.0B') if __name__ == '__main__': unittest.main() mps-youtube-0.2.7.1/mps_youtube/util.py000066400000000000000000000263071273726520200200700ustar00rootroot00000000000000import os import re import sys import ctypes import logging import time import subprocess import collections import unicodedata import pafy from . import g, c, terminalsize from .playlist import Video mswin = os.name == "nt" not_utf8_environment = mswin or "UTF-8" not in sys.stdout.encoding XYTuple = collections.namedtuple('XYTuple', 'width height max_results') class IterSlicer(): """ Class that takes an iterable and allows slicing, loading from the iterable as needed.""" def __init__(self, iterable, length=None): self.ilist = [] self.iterable = iter(iterable) self.length = length if length is None: try: self.length = len(iterable) except TypeError: pass def __getitem__(self, sliced): if isinstance(sliced, slice): stop = sliced.stop else: stop = sliced # To get the last item in an iterable, must iterate over all items if (stop is None) or (stop < 0): stop = None while (stop is None) or (stop > len(self.ilist) - 1): try: self.ilist.append(next(self.iterable)) except StopIteration: break return self.ilist[sliced] def __len__(self): if self.length is None: self.length = len(self[:]) return self.length def has_exefile(filename): """ Check whether file exists in path and is executable. :param filename: name of executable :type filename: str :returns: Path to file or False if not found :rtype: str or False """ paths = [os.getcwd()] + os.environ.get("PATH", '').split(os.pathsep) paths = [i for i in paths if i] dbg("searching path for %s", filename) for path in paths: exepath = os.path.join(path, filename) if os.path.isfile(exepath): if os.access(exepath, os.X_OK): dbg("found at %s", exepath) return exepath return False def dbg(*args): """Emit a debug message.""" # Uses xenc to deal with UnicodeEncodeError when writing to terminal logging.debug(xenc(i) for i in args) def utf8_replace(txt): """ Replace unsupported characters in unicode string. :param txt: text to filter :type txt: str :returns: Unicode text without any characters unsupported by locale :rtype: str """ sse = sys.stdout.encoding txt = txt.encode(sse, "replace").decode(sse) return txt def xenc(stuff): """ Replace unsupported characters. """ return utf8_replace(stuff) if not_utf8_environment else stuff def xprint(stuff, end=None): """ Compatible print. """ print(xenc(stuff), end=end) def mswinfn(filename): """ Fix filename for Windows. """ if mswin: filename = utf8_replace(filename) if not_utf8_environment else filename allowed = re.compile(r'[^\\/?*$\'"%&:<>|]') filename = "".join(x if allowed.match(x) else "_" for x in filename) return filename def set_window_title(title): """ Set terminal window title. """ if mswin: ctypes.windll.kernel32.SetConsoleTitleW(xenc(title)) else: sys.stdout.write(xenc('\x1b]2;' + title + '\x07')) def list_update(item, lst, remove=False): """ Add or remove item from list, checking first to avoid exceptions. """ if not remove and item not in lst: lst.append(item) elif remove and item in lst: lst.remove(item) def get_near_name(begin, items): """ Return the closest matching playlist name that starts with begin. """ for name in sorted(items): if name.lower().startswith(begin.lower()): return name return begin def F(key, nb=0, na=0, textlib=None): """Format text. :param nb: newline before :type nb: int :param na: newline after :type na: int :param textlib: the dictionary to use (defaults to g.text if not given) :type textlib: dict :returns: A string, potentially containing one or more %s :rtype: str """ textlib = textlib or g.text assert key in textlib text = textlib[key] percent_fmt = textlib.get(key + "_") if percent_fmt: text = re.sub(r"\*", r"%s", text) % percent_fmt text = text.replace("&&", "%s") return "\n" * nb + text + c.w + "\n" * na def get_pafy(item, force=False, callback=None): """ Get pafy object for an item. :param item: video to retrieve :type item: :class:`mps_youtube.playlist.Video` or str :param force: ignore cache and retrieve anyway :type force: bool :param callback: callpack to pass to pafy :type callback: func :rtype: Pafy """ if isinstance(item, Video): ytid = item.ytid else: ytid = item callback_fn = callback or (lambda x: None) cached = g.pafs.get(ytid) if not force and cached and cached.expiry > time.time(): dbg("get pafy cache hit for %s", cached.title) cached.fresh = False return cached else: try: p = pafy.new(ytid, callback=callback_fn) except IOError as e: if "pafy" in str(e): dbg(c.p + "retrying failed pafy get: " + ytid + c.w) p = pafy.new(ytid, callback=callback) else: raise g.pafs[ytid] = p p.fresh = True thread = "preload: " if not callback else "" dbg("%s%sgot new pafy object: %s%s" % (c.y, thread, p.title[:26], c.w)) dbg("%s%sgot new pafy object: %s%s" % (c.y, thread, p.videoid, c.w)) return p def getxy(): """ Get terminal size, terminal width and max-results. :rtype: :class:`XYTuple` """ # Import here to avoid circular dependency from . import config if g.detectable_size: x, y = terminalsize.get_terminal_size() max_results = y - 4 if y < 54 else 50 max_results = 1 if y <= 5 else max_results else: x, max_results = config.CONSOLE_WIDTH.get, config.MAX_RESULTS.get y = max_results + 4 return XYTuple(x, y, max_results) def fmt_time(seconds): """ Format number of seconds to %H:%M:%S. """ hms = time.strftime('%H:%M:%S', time.gmtime(int(seconds))) H, M, S = hms.split(":") if H == "00": hms = M + ":" + S elif H == "01" and int(M) < 40: hms = str(int(M) + 60) + ":" + S elif H.startswith("0"): hms = ":".join([H[1], M, S]) return hms def uea_pad(num, t, direction="<", notrunc=False): """ Right pad with spaces taking into account East Asian width chars. """ direction = direction.strip() or "<" t = ' '.join(t.split('\n')) # TODO: Find better way of dealing with this? if num <= 0: return '' if not notrunc: # Truncate to max of num characters t = t[:num] if real_len(t) < num: spaces = num - real_len(t) if direction == "<": t = t + (" " * spaces) elif direction == ">": t = (" " * spaces) + t elif direction == "^": right = False while real_len(t) < num: t = t + " " if right else " " + t right = not right return t def real_len(u, alt=False): """ Try to determine width of strings displayed with monospace font. """ if not isinstance(u, str): u = u.decode("utf8") u = xenc(u) # Handle replacements of unsuported characters ueaw = unicodedata.east_asian_width if alt: # widths = dict(W=2, F=2, A=1, N=0.75, H=0.5) # original widths = dict(N=.75, Na=1, W=2, F=2, A=1) else: widths = dict(W=2, F=2, A=1, N=1, H=0.5) return int(round(sum(widths.get(ueaw(char), 1) for char in u))) def yt_datetime(yt_date_time): """ Return a time object and locale formated date string. """ time_obj = time.strptime(yt_date_time, "%Y-%m-%dT%H:%M:%S.%fZ") locale_date = time.strftime("%x", time_obj) # strip first two digits of four digit year short_date = re.sub(r"(\d\d\D\d\d\D)20(\d\d)$", r"\1\2", locale_date) return time_obj, short_date def parse_multi(choice, end=None): """ Handle ranges like 5-9, 9-5, 5- and -5 with optional repetitions number [n] eg. 2-4[2] is the same as 2 3 4 2 3 4 and 3[4] is 3 3 3 3 Return list of ints. """ end = end or str(len(g.model)) pattern = r'(?= 0.3.82, != 0.4.0, != 0.4.1, != 0.4.2'], classifiers=[ "Topic :: Utilities", "Topic :: Internet :: WWW/HTTP", "Topic :: Multimedia :: Sound/Audio :: Players", "Topic :: Multimedia :: Video", "Environment :: Console", "Environment :: Win32 (MS Windows)", "Environment :: MacOS X", "Operating System :: POSIX :: Linux", "Operating System :: MacOS", "Operating System :: MacOS :: MacOS 9", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft", "Operating System :: Microsoft :: Windows :: Windows 7", "Operating System :: Microsoft :: Windows :: Windows XP", "Operating System :: Microsoft :: Windows :: Windows Vista", "Intended Audience :: End Users/Desktop", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3 :: Only", "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)" ], options={ "py2exe": { "excludes": ("readline, win32api, win32con, dbus, gi," " urllib.unquote_plus, urllib.urlencode," " PyQt4, gtk"), "bundle_files": 1 } }, package_data={"": ["LICENSE", "README.rst", "CHANGELOG"]}, long_description=open("README.rst").read() ) if sys.platform.startswith('linux'): # Install desktop file. Required for mpris on Ubuntu options['data_files'] = [('share/applications/', ['mps-youtube.desktop'])] if os.name == "nt": try: import py2exe # Only setting these when py2exe imports successfully prevents warnings # in easy_install options['console'] = ['mpsyt'] options['zipfile'] = None except ImportError: pass setup(**options)