動機
・スマホの目覚ましや目覚まし時計は毎朝同じ音で飽きる
・音楽サービスはSpotifyやら色々あるけれど、自分はGooglePlayMusic(以下GPMに略)が気に入っている。
→GPMのプレイリストのランダム再生を目覚ましに設定したい!でもWeb版もスマホアプリ版にもそういった機能はない。。。
Webサービスなんだし、Seleniumとかでなんとかなるんじゃない?
2019年4月2日変更
- sideファイルの必要バージョンが上がったので1.1→2.0に変更
- chromedriverのインストール手順追加
環境
PC: Mac (Windowsでも可能ですが、若干やり方が異なります)
ブラウザ: Chrome (Firefoxでもできそう)
準備
1.chrome拡張でSelenium IDEをインストールする
2.macにnpmが入っていなかったらインストールしておく
https://nodejs.org/en/download/
からダウンロードしてインストール
macの場合はhomebrewを使って以下のコマンドでも可。
$brew install node
3.chromedriverをインストールする
$brew tap homebrew/cask
$brew cask install chromedriver
4.npmでselenium-side-runnerをインストールする
$npm install -g selenium-side-runner
やり方
※前提:ここではファイル置き場のディレクトリを
/Users/[ユーザ名]/AlarmMusic
としていますが、適当な場所で構いません。
Selenium IDEでGPMの操作手順を記録し、sideファイルを保存する
・完全にログアウトした状態からGoogleにログインし、希望のプレイリスト等を動かすところまでを操作して記録してください。(※私のサンプルを下に載せます。)
/Users/[ユーザ名]/AlarmMusic/music.side
上記を起動するスクリプトを用意する
# !/bin/sh
osascript -e "set volume 70/100*7"
/usr/local/bin/selenium-side-runner --timeout 7200000 /Users/[ユーザ名]/AlarmMusic/music.side
・ここで、osascriptの行は、音量調整です。
70/100というのが音量の%になるので、各自好みの音量に設定してください。
・timeoutはミリ秒です(7200000で二時間)。ここでの値は長めに設定しておくと良いかと思います。
cronを設定する
$crontab -e
0,40 6 * * * bash -l -c 'sh /Users/[ユーザ名]/AlarmMusic/start.sh'
・上で用意したスクリプトを指定します。
・cronの設定は各々好きなようにしてください。↑の例だと毎日6:00と6:40にアラーム起動します。
操作としては以上です。
補足:私のmusic.side
・ID/PWは各々のものを指定してください。
・「高く評価」した曲をシャッフル再生する様になっています
・あくまで私の環境で動作しているものであり、これをこのまま適用して動作することを補償するものではありません。
{
"id": "7ebfd287-a042-4e94-aa24-dfc42721707c",
"version": "2.0",
"name": "music",
"url": "https://play.google.com/music",
"tests": [{
"id": "163cb9c0-6610-4e34-aab6-2844cc7f15ed",
"name": "play",
"commands": [{
"id": "de15c437-d1f2-4fd5-9a20-a297be382f94",
"comment": "",
"command": "open",
"target": "https://play.google.com/music/listen",
"targets": [],
"value": ""
}, {
"id": "da73f4ba-0d15-4942-bd34-6342686dd733",
"comment": "",
"command": "setWindowSize",
"target": "1420x857",
"targets": [],
"value": ""
}, {
"id": "211c24b3-24b2-4027-8026-845966525a07",
"comment": "",
"command": "click",
"target": "id=gb_70",
"targets": [
["id=gb_70", "id"],
["linkText=ログイン", "linkText"],
["css=#gb_70", "css"],
["css=#gb_70", "css:finder"],
["xpath=//a[contains(text(),'ログイン')]", "xpath:link"],
["xpath=//a[@id='gb_70']", "xpath:attributes"],
["xpath=//div[@id='gb']/div/div/div[3]/div/a", "xpath:idRelative"],
["xpath=//a[contains(@href, 'https://accounts.google.com/ServiceLogin?service=sj&passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FServiceLogin%3Fservice%3Dsj%26continue%3Dhttps%253A%252F%252Fplay.google.com%252Fmusic%252Flisten%26utm_medium%3Dsigned_out_warm_welcome%26welcome%3D0&followup=https://play.google.com/music/listen')]", "xpath:href"],
["xpath=//div[3]/div/a", "xpath:position"]
],
"value": ""
}, {
"id": "86468053-e46e-4ec0-8406-87f13c07711e",
"comment": "",
"command": "pause",
"target": "2000",
"targets": [],
"value": "2000"
}, {
"id": "dbab7d6a-6c2d-49ef-a57c-0d7ea7dca504",
"comment": "",
"command": "type",
"target": "id=identifierId",
"targets": [
["id=identifierId", "id"],
["name=identifier", "name"],
["css=#identifierId", "css"],
["css=#identifierId", "css:finder"],
["xpath=//input[@id='identifierId']", "xpath:attributes"],
["xpath=//div[@id='view_container']/div/div/div[2]/div/div/div/form/content/section/div/content/div/div/div/div/div/input", "xpath:idRelative"],
["xpath=//input", "xpath:position"]
],
"value": "あなたのGoole ID"
}, {
"id": "b7df84fd-a2fc-4c9f-8640-8ec9cdbdfe64",
"comment": "",
"command": "click",
"target": "id=identifierNext",
"targets": [
["css=span.RveJvd.snByac", "css"],
["css=#identifierNext .RveJvd", "css:finder"],
["xpath=//div[@id='identifierNext']/content/span", "xpath:idRelative"],
["xpath=//content/span", "xpath:position"]
],
"value": ""
}, {
"id": "ef431222-4431-4bff-b807-4ceea21a40dc",
"comment": "",
"command": "pause",
"target": "3000",
"targets": [],
"value": "3000"
}, {
"id": "e8acfe44-3f16-4cfd-a44e-4c6d3b949e16",
"comment": "",
"command": "click",
"target": "name=password",
"targets": [],
"value": ""
}, {
"id": "d717d5e2-9df9-4e56-8248-a1365fca6bce",
"comment": "",
"command": "type",
"target": "name=password",
"targets": [
["name=password", "name"],
["css=input[name=\"password\"]", "css"],
["css=.I0VJ4d .zHQkBf", "css:finder"],
["xpath=//input[@name='password']", "xpath:attributes"],
["xpath=//div[@id='password']/div/div/div/input", "xpath:idRelative"],
["xpath=//div/input", "xpath:position"]
],
"value": "あなたのGoogleパスワード"
}, {
"id": "a3d989f1-3090-444c-a550-ae06f050fcb9",
"comment": "",
"command": "click",
"target": "id=passwordNext",
"targets": [
["css=content.CwaK9", "css"],
["css=#passwordNext > .CwaK9", "css:finder"],
["xpath=//div[@id='passwordNext']/content", "xpath:idRelative"],
["xpath=//div[2]/div/div/div/content", "xpath:position"]
],
"value": ""
}, {
"id": "53ac7cc4-fa44-4e42-aa2e-fd80944bb37f",
"comment": "",
"command": "pause",
"target": "15000",
"targets": [],
"value": "15000"
}, {
"id": "e74c958d-8082-4e84-adce-13bc4a2f6960",
"comment": "",
"command": "click",
"target": "css=#playlist-drawer-button > #icon",
"targets": [
["css=#playlist-drawer-button > #icon", "css"],
["css=#playlist-drawer-button > #icon", "css:finder"],
["xpath=(//iron-icon[@id='icon'])[3]", "xpath:attributes"],
["xpath=//paper-icon-button[@id='playlist-drawer-button']/iron-icon", "xpath:idRelative"],
["xpath=//div[4]/paper-icon-button/iron-icon", "xpath:position"]
],
"value": ""
}, {
"id": "77cc1dca-9da8-448c-8c77-dea2e2198591",
"comment": "",
"command": "pause",
"target": "5000",
"targets": [],
"value": "5000"
}, {
"id": "65bdcc17-704b-41e8-ad24-c9c04d6573b7",
"comment": "",
"command": "click",
"target": "css=div.playlist-title",
"targets": [
["css=div.playlist-title", "css"],
["css=.autoplaylist-section > .playlist-drawer-item:nth-child(1) .playlist-title", "css:finder"],
["xpath=//div[@id='mainContainer']/div/div/a/div", "xpath:idRelative"],
["xpath=//paper-header-panel/div/div/div/div/a/div", "xpath:position"]
],
"value": ""
}, {
"id": "584d6ee6-19f8-4348-94ff-fc04b3972df1",
"comment": "",
"command": "pause",
"target": "3000",
"targets": [],
"value": "3000"
}, {
"id": "15a491cb-a4d8-4306-9b67-0d8bfd126909",
"comment": "",
"command": "click",
"target": "css=.button-wrapper > .paper-button-1",
"targets": [
["css=.button-wrapper > .paper-button-1", "css:finder"],
["xpath=//div[@id='music-content']/div[3]/div/div/gpm-detail-page-header/div[2]/div[5]/paper-button", "xpath:idRelative"],
["xpath=//div[2]/div[5]/paper-button", "xpath:position"]
],
"value": ""
}, {
"id": "7eca9718-1058-42b6-a9cc-5fb71d9b0d2d",
"comment": "",
"command": "pause",
"target": "1440000",
"targets": [],
"value": "1440000"
}]
}],
"suites": [{
"id": "01ed6946-5b84-4ed1-b16d-cf0c8a1ab099",
"name": "Default Suite",
"persistSession": false,
"parallel": false,
"timeout": 144000,
"tests": ["163cb9c0-6610-4e34-aab6-2844cc7f15ed"]
}],
"urls": ["https://play.google.com/", "https://play.google.com/music"],
"plugins": []
}
注意事項1: selenium-side-runnerとブラウザログイン状態
selenium-side-runnerで開かれるブラウザは通常ブラウザを開いた時と異なり、Googleへのログイン状態などは引き継がれません。シークレットモードのような感じです。
なので、Seleniumの操作もログインする所から記録する必要があります。
注意事項2: timeoutについて
ちゃんと調査していないのですが、selenium ideで作られるsideファイルそのままだと数分ででタイムアウトして目覚まし(立ち上がったブラウザ)が止まってしまいます。
sideファイル内のtimeout設定を適宜調整してください。
私のmusic.sideだと
"timeout": 144000
という部分の指定により20分になっています。