├── LICENSE ├── README.md ├── setup.py └── src └── YouTubeMusicAPI.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Praveen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YouTube Music API 2 | An unofficial search API for YouTube Music. 3 | 4 | [![Downloads](https://static.pepy.tech/badge/youtubemusicapi)](https://pepy.tech/project/youtubemusicapi) 5 | 6 | **Installation:** 7 | ``` 8 | pip install -U YouTubeMusicAPI 9 | ``` 10 | 11 | ## Get Basic 12 | ```py 13 | # Import the YouTubeMusicAPI module 14 | import YouTubeMusicAPI 15 | 16 | # Example query 17 | query = "alan walker faded" 18 | 19 | # Call the search function with the query 20 | result = YouTubeMusicAPI.search(query) 21 | 22 | # Check if a result was found 23 | if result: 24 | # Print the retrieved result 25 | print(result) 26 | else: 27 | # Print a message if no result was found 28 | print("No Result Found") 29 | ``` 30 | 31 | **Example Output:** 32 | ```json 33 | { 34 | "title": "Alan Walker - Faded", 35 | "id": "60ItHLz5WEA", 36 | "url": "https://music.youtube.com/watch?v=60ItHLz5WEA", 37 | "artwork": "https://img.youtube.com/vi/60ItHLz5WEA/0.jpg", 38 | "author": { 39 | "name": "Alan Walker", 40 | "url": "https://www.youtube.com/@Alanwalkermusic" 41 | } 42 | } 43 | ``` 44 | ## License 45 | Free and open-source under [MIT License](LICENSE). 46 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | setuptools.setup( 4 | name="YouTubeMusicAPI", 5 | version="2.9", 6 | description="An unofficial search API for YouTube Music.", 7 | long_description=open("README.md", "r", encoding="utf-8").read(), 8 | long_description_content_type="text/markdown", 9 | url="https://github.com/cj-praveen/YouTube-Music-API/", 10 | keywords="youtube music api, YouTubeMusicAPI, python youtube music api, youtube music api python", 11 | package_dir={"": "src"}, 12 | python_requires=">=3.9", 13 | install_requires=[ 14 | "httpx" 15 | ] 16 | ) 17 | -------------------------------------------------------------------------------- /src/YouTubeMusicAPI.py: -------------------------------------------------------------------------------- 1 | import warnings, httpx, re 2 | 3 | warnings.filterwarnings("ignore", category=DeprecationWarning) 4 | 5 | def search(query: str) -> dict: 6 | headers = { 7 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0.1) Gecko/20100101 Firefox/123.0.1" 8 | } 9 | 10 | try: 11 | page = httpx.get(f"https://music.youtube.com/search?q={query}", headers=headers).content.decode("unicode_escape") 12 | 13 | trackId = re.search(r'"videoId":"(.*?)"', page) 14 | 15 | if not trackId: 16 | return {} 17 | 18 | trackId = trackId.group(1) 19 | 20 | track_info = httpx.get(f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={trackId}", 21 | headers=headers).json() 22 | 23 | return { 24 | "title": track_info["title"], 25 | "id": trackId, 26 | "url": f"https://music.youtube.com/watch?v={trackId}", 27 | "artwork": f"https://img.youtube.com/vi/{trackId}/0.jpg", 28 | "author": { 29 | "name": track_info["author_name"], 30 | "url": track_info["author_url"] 31 | } 32 | } 33 | 34 | except Exception as e: 35 | print(f"An error occurred: {e}") 36 | return {} 37 | 38 | 39 | if __name__ == "__main__": 40 | result = search("alan walker faded") 41 | print(result) 42 | --------------------------------------------------------------------------------