AppleScript名:なろう小説APIで各カテゴリごとの集計を実行(大カテゴリのみ) |
— Created 2017-10-10 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "GZIP" –https://github.com/nicklockwood/GZIP –http://dev.syosetu.com/man/api/ –1日の利用上限は80,000または転送量上限400MByte??? property |NSURL| : a reference to current application’s |NSURL| property NSString : a reference to current application’s NSString property NSArray : a reference to current application’s NSArray property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSURLQueryItem : a reference to current application’s NSURLQueryItem property NSURLComponents : a reference to current application’s NSURLComponents property NSJSONSerialization : a reference to current application’s NSJSONSerialization property NSMutableURLRequest : a reference to current application’s NSMutableURLRequest property NSURLConnection : a reference to current application’s NSURLConnection property NSSortDescriptor : a reference to current application’s NSSortDescriptor property NSNumber : a reference to current application’s NSNumber property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown set invList to {} set bgList to {1, 2, 3, 4, 99, 98} set bigGnereLabel to {"恋愛", "ファンタジー", "文芸", "SF", "その他", "ノンジャンル"} –全体の件数取得 set aRec to {gzip:"5", out:"json", lim:"1"} set aRESTres to callNarouAPI(aRec, "1", "1") of me set wholeCount to (allCount of first item of aRESTres) –カテゴリごとの集計 repeat with i in bgList –repeat with ii in gList set aRec to {gzip:"5", biggenre:i as string, out:"json", lim:"1"} set aRESTres to callNarouAPI(aRec, "1", "1") of me set aTotal to allCount of first item of aRESTres if aTotal is not equal to 0 then set big to contents of i set bigLabel to getLabelFromNum(bgList, bigGnereLabel, big) of me set aPerCentatge to roundingDownNumStr(((aTotal / wholeCount) * 100), 1) of me set the end of invList to {biggenre:bigLabel, totalNum:aTotal, percentage:aPerCentatge} end if –end repeat end repeat set bList to sortRecListByLabel(invList, "totalNum", false) of me –降順ソート –> {{totalNum:274075, percentage:53.1, biggenre:"ノンジャンル"}, {totalNum:68890, percentage:13.3, biggenre:"文芸"}, {totalNum:68426, percentage:13.2, biggenre:"ファンタジー"}, {totalNum:46165, percentage:8.9, biggenre:"その他"}, {totalNum:45965, percentage:8.9, biggenre:"恋愛"}, {totalNum:11733, percentage:2.2, biggenre:"SF"}} on callNarouAPI(aRec, callFrom, callNum) set reqURLStr to "http://api.syosetu.com/novelapi/api/" –通常API –set aRec to {gzip:"5", |st|:callFrom as string, out:"json", lim:callNum as string} set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to (responseCode of aRes) as integer if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes set aRESTres to (json of aRes) as list end callNarouAPI –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to NSMutableURLRequest’s requestWithURL:(|NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding" set aRes to NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) set resList to aRes as list set bRes to contents of (first item of resList) set rRes to bRes’s gunzippedData() –From GZIP.framework set resStr to NSString’s alloc()’s initWithData:rRes encoding:(NSUTF8StringEncoding) set jsonString to NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding) set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults on retURLwithParams(aBaseURL, aRec) set aDic to NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams –リストに入れたレコードを、指定の属性ラベルの値でソート on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean) set aArray to NSArray’s arrayWithArray:aRecList set sortDesc to NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF set sortDescArray to NSArray’s arrayWithObjects:sortDesc set sortedArray to aArray’s sortedArrayUsingDescriptors:sortDescArray set bList to sortedArray as list return bList end sortRecListByLabel on getLabelFromNum(aList, labelLIst, aNum) set aInd to offsetOf(aList, aNum) of me set anItem to contents of item aInd of labelLIst return anItem end getLabelFromNum on offsetOf(aList as list, aTarg) set aArray to current application’s NSArray’s arrayWithArray:aList set aIndex to aArray’s indexOfObjectIdenticalTo:aTarg return (aIndex + 1) end offsetOf on roundingDownNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingDownNumStr on roundingUpNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingUpNumStr |
More from my site
(Visited 88 times, 1 visits today)
ZipArchive Frameworkを使ってパスワード付きZipアーカイブ – AppleScriptの穴 says:
[…] 困ります(変数に入れたデータを圧縮したいとか)。以前に、なろう小説APIを呼び出したときに、GZIP.frameworkを用いて変数内のデータをZip展開しましたが、こういう用途には使えません。 […]