アーケードゲーム「戦場の絆」が設置されている日本国内のゲームセンターの件数をカウントするAppleScriptです。
指定ページを取得し、本文内容をプレーンテキスト化して正規表現でデータ抽出して加算を行うという処理内容です。
部品がありきたりなので、新規作成部分はほとんどありません(メイン部分だけ)。
AppleScript名:戦場の絆の日本国内の設置店舗数をカウント.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/03/03 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property NSURLQueryItem : a reference to current application’s NSURLQueryItem property NSAttributedString : a reference to current application’s NSAttributedString property NSURLComponents : a reference to current application’s NSURLComponents property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSUnicodeStringEncoding : a reference to current application’s NSUnicodeStringEncoding set aTotal to 0 –「戦場の絆」公式ページに掲載されている店舗一覧の都道府県別コード set areaList to {"JP-01", "JP-02", "JP-03", "JP-04", "JP-05", "JP-06", "JP-07", "JP-08", "JP-09", "JP-10", "JP-11", "JP-12", "JP-13", "JP-14", "JP-15", "JP-16", "JP-17", "JP-18", "JP-19", "JP-20", "JP-21", "JP-22", "JP-23", "JP-24", "JP-25", "JP-26", "JP-27", "JP-28", "JP-29", "JP-30", "JP-31", "JP-32", "JP-33", "JP-34", "JP-35", "JP-36", "JP-37", "JP-38", "JP-39", "JP-40", "JP-41", "JP-42", "JP-43", "JP-44", "JP-45", "JP-46", "JP-47"} –「戦場の絆」公式ページに掲載されている店舗一覧のBase URL set baseURL to "https://gundam-kizuna.jp/locations/list" –都道府県コードでループ repeat with i in areaList –店舗一覧の都道府県別ページをダウンロード set j to contents of i set aRec to {area:j} set aRes to retURLwithParams(baseURL, aRec) of me set shellText to "curl -s " & aRes set htmlRes to (do shell script shellText) set aPlainText to (HTMLDecode(htmlRes) of me) –店舗件数が入っている箇所を抽出して加算 set aList to paragraphs of aPlainText set anArray to (NSArray’s arrayWithArray:aList) set aPred to (NSPredicate’s predicateWithFormat:"SELF MATCHES ’[0-9]{1,3}件の店舗が見つかりました。’") set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list if bRes is not equal to {} then set a to repChar(contents of first item of bRes, "件の店舗が見つかりました。", "") of me try set aNum to a as integer set aTotal to aTotal + aNum on error log {"Error in ", j} end try end if end repeat return aTotal –> 622 (2019/3/3) (* set countryList to {"HKG", "TWN"}–香港と台湾の店舗のコード set baseURL2 to "https://gundam-kizuna.jp/locations/list" *) –パラメータつきURLを作成 on retURLwithParams(aBaseURL as string, aRec as record) 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 bLen to length of aValList if aLen is not equal to bLen then return false set qList to {} repeat with i from 1 to aLen set aName to (contents of item i of aKeyList) as string set aVal to (contents of item i of aValList) as string 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 –テキストをHTMLとして解釈しプレーンテキスト化 on HTMLDecode(HTMLString) set theString to NSString’s stringWithString:HTMLString set theData to theString’s dataUsingEncoding:(current application’s NSUnicodeStringEncoding) set attStr to NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set aStr to attStr’s |string|() return aStr as string end HTMLDecode –文字置換 on repChar(origText as string, targStr as string, repStr as string) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar |
More from my site
(Visited 44 times, 1 visits today)