Skip to content

Instantly share code, notes, and snippets.

@hkitago
Last active June 3, 2021 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkitago/33bf0141ae8ac12e34fd53157c95658f to your computer and use it in GitHub Desktop.
Save hkitago/33bf0141ae8ac12e34fd53157c95658f to your computer and use it in GitHub Desktop.
メルカリ検索結果一覧の個数の変化をメッセに通知
property BASE_URL : "https://www.mercari.com/jp/search/?sort_order=price_asc&keyword={検索語}&status_on_sale=1"
property itemsCnt : 0
set theTargetFile to ((((path to me as text) & "::") as alias) as string) & "checkMercari.txt"
set prevData to my read_from_file(theTargetFile)
try
set prevCnt to prevData as integer
on error
set prevCnt to itemsCnt
end try
tell application "Safari"
make new document at end of documents with properties {URL:BASE_URL}
set bounds of front window to {0, 0, 0, 0}
tell front document
repeat until (do JavaScript "document.readyState") is "complete"
end repeat
delay 1.0
set itemsCnt to do JavaScript "document.querySelectorAll('.items-box').length"
set theCnt to itemsCnt as integer
if theCnt is not prevCnt then
set theData to theCnt as string
my write_to_file(theData, theTargetFile, false)
if prevCnt is not 0 then my sendMessage("+8180XXXXXXX", "メルカリでの出品数が " & prevCnt & " から " & theCnt & " に変わりました " & BASE_URL)
end if
end tell
close front window
end tell
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean) https://stackoverflow.com/a/3781066/579429
try
set target_file to target_file as text
set open_target_file to open for access file target_file with write permission
if append_data is false then set eof of open_target_file to 0
write this_data to open_target_file starting at eof
close access open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
on read_from_file(target_file)
try
set target_file to target_file as text
set open_target_file to open for access file target_file
set theText to read open_target_file
close access open_target_file
return theText
end try
end read_from_file
on sendMessage(targetBuddyPhone, targetMessage)
tell application "Messages"
set targetService to 1st account whose service type = iMessage
set targetBuddy to participant targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end sendMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment