使用Python搜索电影,书籍和播客

Podsearch包,用于查找播客



Apple并没有真正宣传iTunes Store和其他目录具有一个古怪但简单的搜索API-因此,我决定写这篇文章。在本文中,您将学习API可以做什么以及如何使用它。



搜索苹果目录



该API搜索iTunes Store,iBooks Store,Apple Podcast和App Store的内容。因此,您可以找到歌曲,电影,书籍,播客和应用程序。



API   JSONP,   Content-Type: text/javascript application/json.



GET /search?media=podcast&term=python HTTP/1.1
Host: itunes.apple.com
Accept: application/json

HTTP/2 200
content-type: text/javascript; charset=utf-8

{...}


, :



import requests

def search(term, media):
    url = "https://itunes.apple.com/search"
    payload = {"term": term, "media": media}
    response = requests.get(url, params=payload)
    response.raise_for_status()
    results = response.json().get("results", [])
    return results


>>> results = search("python", media="podcast")
>>> results[0]["collectionName"]
'Talk Python To Me'




:



  • term — , ;
  • media — (movie, podcast, music, audiobook, software, ebook, all),   all;
  • country — , ISO- (us, ru, ...), us;
  • limit — , 50.


, :



import requests

def search(term, media="all", country="us", limit=10):
    url = "https://itunes.apple.com/search"
    payload = {"term": term, "media": media, "country": country, "limit": limit}
    response = requests.get(url, params=payload)
    response.raise_for_status()
    results = response.json().get("results", [])
    return results




:



{
    "resultCount": 10,
    "results": [
        {
            "wrapperType": "track",
            "kind": "song",
            "artistId": 1495668306,
            "collectionId": 527039271,
            "trackId": 527039276,
            "artistName": "Dodge & Fuski",
            "collectionName": "Never Say Die (Deluxe Edition)",
            "trackName":"Python",
            ...
        }, 
        {
            "wrapperType": "track",
            "kind": "podcast",
            "collectionId": 979020229,
            "trackId": 979020229,
            "artistName": "Michael Kennedy (@mkennedy)",
            "collectionName": "Talk Python To Me"
            ...
        },
        ...
    ]
}


  (kind)    , . :



  • artistId — ;
  • artistName — ;
  • artistViewUrl —  Apple;
  • collectionId — ;
  • collectionName —  ;
  • collectionViewUrl —  Apple;
  • trackId —  ;
  • trackName —  ;
  • artworkUrl100 — 100x100 ;
  • country — ;
  • primaryGenreName — .


   — podsearch,  .



 



(artistId, collectionId, trackId), lookup  :



import requests

def lookup(object_id):
    url = "https://itunes.apple.com/lookup"
    payload = {"id": object_id}
    response = requests.get(url, params=payload)
    response.raise_for_status()
    results = response.json().get("results", [])
    return results


>>> results = lookup(979020229)
>>> results[0]["collectionName"]
'Talk Python To Me'




Content-Type . :



  • (country)   ,  .   ISO- (ru),   —  (RUS).
  • 没有响应模式。
  • Apple将API调用的频率限制为每分钟20个请求。


Apple Podcast搜索包上的API描述



如果您想在Python中做更多有趣的事情-订阅@ohmypy频道




All Articles