]> gitweb.factorcode.org Git - factor.git/blob - extra/itunes/itunes.factor
inverse: Fix docs
[factor.git] / extra / itunes / itunes.factor
1 ! Copyright (C) 2020 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs biassocs combinators formatting
4 http.client json.reader kernel literals sequences ;
5 IN: itunes
6
7 ERROR: http-get-error url res json ;
8
9 : get-json ( url -- json )
10     dup http-get over code>> 200 = [
11         2nip json>
12     ] [
13         http-get-error
14     ] if ;
15
16 : ?key ( key assoc -- key ? ) dupd key? ; inline
17
18 : find-first-value ( assoc keys -- value key )
19     [ of ] with map-find ; inline
20
21 CONSTANT: apple-languages $[
22     H{
23         { "af" "Afrikaans" }
24         { "sq" "Albanian" }
25         { "sq" "Albanian" }
26         { "eu" "Basque" }
27         { "be" "Belarusian" }
28         { "bg" "Bulgarian" }
29         { "ca" "Catalan" }
30         { "Chinese (Simplified)" "zh-cn" }
31         { "Chinese (Traditional)" "zh-tw" }
32         { "hr" "Croatian" }
33         { "cs" "Czech" }
34         { "da" "Danish" }
35         { "nl" "Dutch" }
36         { "nl-be" "Dutch (Belgium)" }
37         { "nl-nl" "Dutch (Netherlands)" }
38         { "en" "English" }
39         { "en-au" "English (Australia)" }
40         { "en-bz" "English (Belize)" }
41         { "en-ca" "English (Canada)" }
42         { "en-ie" "English (Ireland)" }
43         { "en-jm" "English (Jamaica)" }
44         { "en-nz" "English (New Zealand)" }
45         { "en-ph" "English (Phillipines)" }
46         { "en-za" "English (South Africa)" }
47         { "en-tt" "English (Trinidad)" }
48         { "en-gb" "English (United Kingdom)" }
49         { "en-us" "English (United States)" }
50         { "en-zw" "English (Zimbabwe)" }
51         { "et" "Estonian" }
52         { "fo" "Faeroese" }
53         { "fi" "Finnish" }
54         { "fr" "French" }
55         { "fr-be" "French (Belgium)" }
56         { "fr-ca" "French (Canada)" }
57         { "fr-fr" "French (France)" }
58         { "fr-lu" "French (Luxembourg)" }
59         { "fr-mc" "French (Monaco)" }
60         { "fr-ch" "French (Switzerland)" }
61         { "gl" "Galician" }
62         { "gd" "Gaelic" }
63         { "de" "German" }
64         { "de-at" "German (Austria)" }
65         { "de-de" "German (Germany)" }
66         { "de-li" "German (Liechtenstein)" }
67         { "de-lu" "German (Luxembourg)" }
68         { "de-ch" "German (Switzerland)" }
69         { "el" "Greek" }
70         { "haw" "Hawaiian" }
71         { "hu" "Hungarian" }
72         { "is" "Icelandic" }
73         { "in" "Indonesian" }
74         { "ga" "Irish" }
75         { "it" "Italian" }
76         { "it-it" "Italian (Italy)" }
77         { "it-ch" "Italian (Switzerland)" }
78         { "ja" "Japanese" }
79         { "ko" "Korean" }
80         { "mk" "Macedonian" }
81         { "no" "Norwegian" }
82         { "pl" "Polish" }
83         { "pt" "Portugese" }
84         { "pt-br" "Portugese (Brazil)" }
85         { "pt-pt" "Portugese (Portugal" }
86         { "ro" "Romanian" }
87         { "ro-mo" "Romanian (Moldova)" }
88         { "ro-ro" "Romanian (Romania" }
89         { "ru" "Russian" }
90         { "ru-mo" "Russian (Moldova)" }
91         { "ru-ru" "Russian (Russia)" }
92         { "sr" "Serbian" }
93         { "sk" "Slovak" }
94         { "sl" "Slovenian" }
95         { "es" "Spanish" }
96         { "es-ar" "Spanish (Argentinia)" }
97         { "es=bo" "Spanish (Bolivia)" }
98         { "es-cl" "Spanish (Chile)" }
99         { "es-co" "Spanish (Colombia)" }
100         { "es-cr" "Spanish (Costa Rica)" }
101         { "es-do" "Spanish (Dominican Republic)" }
102         { "es-ec" "Spanish (Ecuador)" }
103         { "es-sv" "Spanish (El Salvador)" }
104         { "es-gt" "Spanish (Guatemala)" }
105         { "es-hn" "Spanish (Honduras)" }
106         { "es-mx" "Spanish (Mexico)" }
107         { "es-ni" "Spanish (Nicaragua)" }
108         { "es-pa" "Spanish (Panama)" }
109         { "es-py" "Spanish (Paraguay)" }
110         { "es-pe" "Spanish (Peru)" }
111         { "es-pr" "Spanish (Puerto Rico)" }
112         { "es-es" "Spanish (Spain)" }
113         { "es-uy" "Spanish (Uruguay)" }
114         { "es-ve" "Spanish (Venezuela)" }
115         { "sv" "Swedish" }
116         { "sv-fi" "Swedish (Finland)" }
117         { "sv-se" "Swedish (Sweden)" }
118         { "tr" "Turkish" }
119         { "uk" "Ukranian" }
120     } >biassoc
121 ]
122
123 : >language-code ( name/code -- abbrev )
124     apple-languages from>> ?key [ ] [ apple-languages to>> ?at drop ] if ;
125
126 : search-apple-podcasts ( terms -- json )
127     "https://itunes.apple.com/search?media=podcast&term=%s" sprintf get-json ;
128
129 ! https://podcasts.apple.com/de/genre/podcasts/id26
130
131 : >top-apple-podcasts ( hash -- json )
132     {
133         [ { "language" "code" } find-first-value drop >language-code "US" or ]
134         [ { "genre" } find-first-value drop "26" or ] ! Genre "26" is "top"
135         [ { "limit" } find-first-value drop "10" or ]
136         [ { "explicit" } find-first-value drop "true" or ]
137     } cleave
138     "https://itunes.apple.com/%s/rss/toppodcasts/genre=%s/limit=%s/explicit=%s/json"
139     sprintf get-json  ;
140
141 : top-100-apple-podcasts ( code/f -- json )
142     "US" or
143     'H{
144         { "limit" "100" }
145         { "code" _ }
146         { "explicit" "true" }
147         { "genre" "26" }
148     } >top-apple-podcasts ;
149
150 : id>podcast ( id -- podcast )
151     "https://itunes.apple.com/lookup?id=%s" sprintf get-json ;
152
153
154 ! TODO
155 ! https://rss.itunes.apple.com/api/v1/us/apple-music/coming-soon/all/10/explicit.json
156 ! https://rss.itunes.apple.com/api/v1/us/podcasts/top-podcasts/all/10/explicit.json
157 ! https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=26&popId=28
158 ! https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/