(defMacroVar ASSET_SOURCES_FILE "assets/sources.tsv") (defMacroFunction appendToSources [query resultNumber apiName author apiQuery] (unless (FileSystem.exists ASSET_SOURCES_FILE) (File.saveContent ASSET_SOURCES_FILE "")) (let [newLine "${query}\t${resultNumber}\t${apiName}\t${author}\t${apiQuery}\n" existingSources (File.getContent ASSET_SOURCES_FILE)] (unless !(= -1 (existingSources.indexOf newLine)) (File.saveContent ASSET_SOURCES_FILE (+ existingSources (if (StringTools.endsWith existingSources "\n") "" "\n") newLine))))) (defMacroFunction downloadToImages [query resultNumber apiName url] (let [ext (Path.extension url) file "assets/images/$(StringTools.replace query " " "")-${apiName}-${resultNumber}.${ext}"] (unless (FileSystem.exists file) (assertProcess "curl" ["--output" file url])) file)) // This macro downloads an image from the Pixabay.com API and puts it in // your projects AssetPaths, returning the path for loadAsset(). (defMacro pixabayImage [searchQuery resultNumber] (_pixabayImage (eval searchQuery) (eval resultNumber))) (defMacroVar PIXABAY_PUBLIC_KEY "24159230-ce68ffe06827131f3f991f4f6") (defMacroFunction _pixabayImage [searchQuery resultNumber] // TODO tryCatch (letThrow (let [apiUrl "https://pixabay.com/api/" perPage 20 // this many will be queried for ID and memoized page (+ 1 (Math.ceil (/ resultNumber perPage))) _request ->url (Http.requestUrl url) request (fsMemoize _request "request") apiQuery "${apiUrl}?key=${PIXABAY_PUBLIC_KEY}&q=$(StringTools.urlEncode searchQuery)&per_page=${perPage}&page=${page}" result (Json.parse (request apiQuery)) imageInfo (if result.hits (nth result.hits (% resultNumber perPage)) (throw "No Pixabay results for $searchQuery"))] // Document where the assets came from: (appendToSources searchQuery resultNumber "Pixabay" "${imageInfo.user}:${imageInfo.user_id}" apiQuery) (ReaderExp.StrExp (downloadToImages searchQuery resultNumber "Pixabay" imageInfo.largeImageURL))) (catch [error] (throw "Error from pixabay query $searchQuery ${resultNumber}: $error")))) // DEPRECATED/INCOMPLETE: Unsplash asset macros, because Unsplash requires hotlinking, which is // incompatible with making offline-enabled HaxeFlixel games // This macro downloads an image from the Unsplash.com API and puts it in // your projects AssetPaths, returning the path for loadAsset(). // USAGE: First, make an account on unsplash.com. Make a Demo app, // then put your access key and secret key in environment variables // UNSPLASH_ACCESS_KEY and UNSPLASH_SECRET_KEY (defMacro unsplashImage [searchQuery resultNumber] (_unsplashImage (eval searchQuery) (eval resultNumber))) (defMacroFunction _unsplashImage [searchQuery resultNumber] // TODO tryCatch (letThrow (let [apiUrl "https://api.unsplash.com" query "/search/photos" clientId (let [id (Sys.getEnv "UNSPLASH_ACCESS_KEY")] (if id id (throw "UNSPLASH_ACCESS_KEY not defined"))) perPage 10 // this many will be queried for ID and memoized page (Math.ceil (/ resultNumber perPage)) _request ->url (Http.requestUrl url) request (fsMemoize _request "request") result (Json.parse (request "${apiUrl}${query}?client_id=${clientId}&query=$(StringTools.urlEncode searchQuery)&per_page=${perPage}&page=${page}"))] imageResultId .id (nth result.results (% resultNumber perPage)) (ReaderExp.StrExp (Json.stringify result))) (catch [error] (throw "Error from unsplash query $searchQuery ${resultNumber}: $error"))))