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 ;
7 ERROR: http-get-error url res json ;
9 : get-json ( url -- json )
10 dup http-get over code>> 200 = [
16 : ?key ( key assoc -- key ? ) dupd key? ; inline
18 : find-first-value ( assoc keys -- value key )
19 [ of ] with map-find ; inline
21 CONSTANT: apple-languages $[
30 { "Chinese (Simplified)" "zh-cn" }
31 { "Chinese (Traditional)" "zh-tw" }
36 { "nl-be" "Dutch (Belgium)" }
37 { "nl-nl" "Dutch (Netherlands)" }
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)" }
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)" }
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)" }
76 { "it-it" "Italian (Italy)" }
77 { "it-ch" "Italian (Switzerland)" }
84 { "pt-br" "Portugese (Brazil)" }
85 { "pt-pt" "Portugese (Portugal" }
87 { "ro-mo" "Romanian (Moldova)" }
88 { "ro-ro" "Romanian (Romania" }
90 { "ru-mo" "Russian (Moldova)" }
91 { "ru-ru" "Russian (Russia)" }
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)" }
116 { "sv-fi" "Swedish (Finland)" }
117 { "sv-se" "Swedish (Sweden)" }
123 : >language-code ( name/code -- abbrev )
124 apple-languages from>> ?key [ ] [ apple-languages to>> ?at drop ] if ;
126 : search-apple-podcasts ( terms -- json )
127 "https://itunes.apple.com/search?media=podcast&term=%s" sprintf get-json ;
129 ! https://podcasts.apple.com/de/genre/podcasts/id26
131 : >top-apple-podcasts ( hash -- json )
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 ]
138 "https://itunes.apple.com/%s/rss/toppodcasts/genre=%s/limit=%s/explicit=%s/json"
141 : top-100-apple-podcasts ( code/f -- json )
146 { "explicit" "true" }
148 } >top-apple-podcasts ;
150 : id>podcast ( id -- podcast )
151 "https://itunes.apple.com/lookup?id=%s" sprintf get-json ;
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/