gbf-raiderの劣化版をpythonで作ってみた話
グラブルにあらゆる時間を奪われ,グラブルが生活の中心になっている鈴瑚です.グラブルを始めるならこちら→
www.andapp.jp
グラブルにはマルチシステムがありtwitterを介して他の人の救援に入れるようになっています.そうなれば当然twitterのAPIを用いた救援に入るのを援助するアプリケーションも存在します.自分も今の今までそのアプリケーションに頼っていたのですが,サイトが閉鎖されることになりこのままでは生活に支障が生じそうだったので自分で作ることにしました.Mac専用
準備
まずpythonでtwitterAPIを使えるようにTwythonを導入します.やり方などはググってくださればすぐに出てくると思います.
TwitterのAPIを使うためにキーを取得します.これも検索すれば簡単です.
コード
#!/usr/bin/env python from twython import Twython, TwythonError from twython import TwythonStreamer import sys from multiprocessing import Pool import time import subprocess global CONSUMER_KEY global CONSUMER_SECRET global ACCESS_KEY global ACCESS_SECRET #twitter_api_key CONSUMER_KEY = '' CONSUMER_SECRET = '' ACCESS_KEY = '' ACCESS_SECRET = '' argvs = sys.argv del argvs[0] argc = len(argvs) #ヘルプオブション,適宜足してください if argvs[0] == '-h': print ("1:Lv50 ティアマト・マグナ\n\ 2:Lv70 コロッサス・マグナ\n\ 3:Lv60 リヴァイアサン・マグナ\n\ 4:Lv60 ユグドラシル・マグナ\n\ 5:Lv75 シュヴァリエ・マグナ\n\ 6:Lv75 セレスト・マグナ\n\ ") exit() #敵の名前を正確に打ち込む,英語名を調べるのがめんどくさければ空欄で enemy_info_dict = {\ "1":["Lv50 ティアマト・マグナ","Lv50 Tiamat Omega"],\ "2":["Lv70 コロッサス・マグナ","Lv70 Colossus Omega"],\ "3":["Lv60 リヴァイアサン・マグナ","Lv60 Leviathan Omega"],\ "4":["Lv60 ユグドラシル・マグナ","Lv60 Yggdrasil Omega"],\ "5":["Lv75 シュヴァリエ・マグナ","Lv75 Luminiera Omega"],\ "6":["Lv75 セレスト・マグナ","Lv75 Celeste Omega"],\ "7":["Lv75 スーペルヒガンテ"," "]\ } class MyStreamer(TwythonStreamer): def on_success(self, data): if 'text' in data: if (u'参加者募集' in data['text']): tweet_id = data['id'] text = data['text'] text = text.split(u':',1) text = text[1].split("L",1) #idだけにする,text[0]はid battle_id = text[0].strip() print(battle_id) process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) process.communicate(battle_id.encode('utf-8')) def on_error(self, status_code, data): print (status_code, data) def write_to_clipboard(self,output): process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) process.communicate(output.encode('utf-8')) for x in (argvs): print (enemy_info_dict[x][0]) stream = MyStreamer(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) #stream.statuses.filter(track= n) def serch(enemy_name): stream.statuses.filter(track = (enemy_name[0] or enemy_name[1])) pool = Pool(4) for x in (argvs): result = pool.apply_async(serch, (enemy_info_dict[x], )) result.wait()
使い方
1.適当な名前で保存する.(ここではgbf-raider.py)
2terminalでgbf-raider.py 1 (1だとティアマト,2だとコロッサスと番号で管理)
3terminalの画面に敵の名前とidが表示される
4クリップボードにidがコピーされるのであとは貼り付けるだけ
iTunesのポンコツ"アルバムアートワーク入手する"をAppleScriptでマシにする話
同人系のCDだとiTunesの”アルバムアートワークを入手する”を使っても見つかりませんでした,と毎回言われます.自分はそれなりの数のCDを買うのでその度にわざわざ画像を拾って来て,iTunesに貼り付けるのがバカらしく思えて来たのでAppleScriptで試してみました.
すごくテキトーにやってしまったので役に立つか微妙です.
コード
--アルバムの情報取得とSafariを用いた画像検索 tell application "iTunes" set theAlbumArtist to item 1 of album artist of selection set theAlbum to item 1 of album of selection set theArtist to item 1 of artist of selection set searchURL to "https://www.google.co.jp/search?q=" & theAlbum & "+" & theAlbumArtist & "&source=lnms&tbm=isch&sa=X" as string tell application "Safari" to open location searchURL end tell display dialog "OK?" --画像検索の検索結果をhtmlで入手 tell application "Safari" set html to (source of document 1) set the clipboard to html end tell --画像情報ごとにリストで分け高さと横幅の取得 set imageInfo to devText(html, "画像検索結果") set imageInfo to rest of imageInfo set imageHeight to {} set imageWidth to {} --上から20枚の画像の情報を扱う(この20は適当) repeat with i from 1 to 20 set end of the imageHeight to (item 1 of devText((item 2 of devText(item i of imageInfo, "\"oh\":")), ",")) end repeat repeat with i from 1 to 20 set end of the imageWidth to (item 1 of devText((item 2 of devText(item i of imageInfo, "\"ow\":")), ",")) end repeat --自分が欲しい画像の条件,(今回は高さ400,横幅400より大きく,高さと横幅の比) repeat with i from 20 to 1 by -1 if (item i of imageHeight > 400) and (item i of imageWidth > 400) and (((item i of imageHeight) / (item i of imageWidth) < 1.1) or ((item i of imageHeight) / (item i of imageWidth > 0.9))) then --display dialog i & "," & (item i of imageHeight as text) & "," & item i of imageWidth as text set getNumber to i end if end repeat set imageUrl to item 1 of devText((item 2 of devText(item getNumber of imageInfo, "\"ou\":\"")), "\"") set theDestination to "ダウンロードする場所を指定する" set thePath to theDestination & ":" & theAlbum & ".png" do shell script "curl -L " & imageUrl & " -o \"" & POSIX path of thePath & "\"" set targetImageFile to thePath tell application "iTunes" set theTrack to selection repeat with i in theTrack set data of artwork 1 of i to (read (file targetImageFile) as picture) end repeat end tell --デリミタで分割 on devText(theText, aDelimiter) set tmp to AppleScript's text item delimiters set AppleScript's text item delimiters to aDelimiter set theList to every text item of theText set AppleScript's text item delimiters to tmp return theList end devText
使い方
1.iTunesでアルバムアートを適用したいアルバムをcmd + クリックで青色にする.
2.上のコードを実行する.
3.OKと表示されるので画像検索結果でお目当の画像があるか確認してあればOK,なければ検索結果を変えてOKを押す
4.~完~
所感
どうしてもマイナーどころだとアートワークが見つからない可能性もあるのでその時は諦め
Karabiner-ElementsをアップデートするとUS配列になって絶望した話
少し前の記事でKarabiner-Elementsについてまとめました.しばらくはうまく行ってましたが,最近アップデートがあり強制的にUS配列になってしまいました.
やったこと
- バックアップから前のバージョンに戻す.バージョン0.90.46ではうまく行っていたのでこのバージョンに戻した
→直らず.キャッシュファイルや設定ファイルも消してみたがうまくいかず.
- Githubから過去バージョンをクローンしてbuild
→やり方通りにしてもエラーを吐かれてしまいbuildできず...
- issueが建てられていたためそこでパッケージをダウンロードしてインストール
https://github.com/tekezo/Karabiner-Elements/issues/278
→成功.とりあえずはバージョン0.90.46でしのぎたいと思います.
AppleScriptで曲の変わり目に通知が来るようにするまで(part2)
少し前の記事でiTunesの曲の変わり目に通知を入れるアプリケーションを作成しました.
rinringo429.hatenablog.com
しかしDockに表示される時と通知に使われるアイコンがデフォルトのアイコンなのでどうも見栄えがよろしくありません.そこでデフォルトのアイコンをアルバムアートワークに差し替えてみました.アルバムが変わるごとにアプリケーションのアイコンを変えることで通知のアイコンとDockのアイコンを変えてみました.かなりゴリ押しなのでご了承ください.
コード
property theName : "" tell application "iTunes" if theName is "" then set theName to name of current track end tell on idle tell application "iTunes" try set tmpName to name of current track on error set tmpName to theName end try if tmpName is not theName then if (count of artwork of current track) ≥ 1 then set artworkExists to true tell application "iTunes" to tell artwork 1 of current track set d to raw data end tell set thePath to ((path to pictures folder) as text) & "applet.iconset" set theIconPath to thePath & ":icon_512x512.png" set icon_512 to open for access file (theIconPath) with write permission --アイコンの書き込み set eof icon_512 to 0 write d to icon_512 close access icon_512 tell application "Image Events" launch set myImage to open file (theIconPath) save myImage in file (theIconPath) as PNG -- 上でアートワークを適当な拡張子で保存したためここでpng形式で再び上書き保存する end tell delay 1 do shell script "sips -z 512 512 " & (POSIX path of theIconPath) do shell script "/usr/local/bin/convert " & (POSIX path of theIconPath) & " -alpha Set " & (POSIX path of theIconPath) --iconファイルを作成するためにImageMagicのconvertコマンドでalphaをオンにしている.(よく分かってない) do shell script "iconutil -c icns " & (POSIX path of thePath) do shell script "mv " & POSIX path of (((path to pictures folder) as text)) & "applet.icns " & quoted form of "/Applications/Track Dialog.app/Contents/Resources/" try do shell script "mv \"/Applications/Track Dialog.app/Contents/changeTheArtwork\" \"/Applications/Track Dialog.app/\"" delay 1 do shell script "mv \"/Applications/Track Dialog.app/changeTheArtwork\" \"/Applications/Track Dialog.app/Contents/\"" do shell script "killall Dock" --Dockのアイコンの更新 delay 7 --あまり早すぎるとアルバムの変わり目の一曲目のアイコンが一つ前のアルバムのアートワークになってしまう end try else set artworkExists to false end if display notification "Album Name :" & album of current track with title "Album Artist :" & album artist of current track subtitle "Track Name :" & name of current track end if set theName to tmpName return 1 end tell end idle
使い方
- 上のコードを拡張子をapp,ハンドラを終了させないにチェックを入れて"Track Dialog"という名でアプリケーションフォルダに保存する.
- imageMagicのインストール
- picturesフォルダに"applet.iconset"という名のフォルダを作成
- Track Dialog のパッケージを表示しContentsフォルダの中に,"changeTheArtwork"という名の空のファイルを入れる
- 後はTrack Dialogを起動させるだけ
所感
そもそもアップルスクリプトでするべきではないのでは,,,,
圧倒的初心者なので根本的なことが分かってないためすごいゴリ押しをしてしまっている
AppleScriptでSoundの出力先を変えるまで
普段はPCにUSB DACを繋げて音楽を聴いています.しかしSkypeなどをする際にはマイク付きのイヤホンに変えることが多々あります.その時USB DACが間にあるとマイクが使えないのでMacのジャックに差す必要があります.そうすると音の出力先をいちいち設定から開かなければならないため非常に効率が悪いです.そこでAppleScriptで簡単にしてみました.
コード
tell application "System Preferences" quit delay 2 reveal anchor "output" of pane id "com.apple.preference.sound" activate tell application "System Events" tell process "System Preferences" tell tab group 1 of window "サウンド" if (selected of row 1 of table 1 of scroll area 1) then set selected of row 3 of table 1 of scroll area 1 to true --出力が1番目だったら3番目に変える else set selected of row 1 of table 1 of scroll area 1 to true --出力が3番目だったら1番目に変える end if end tell end tell end tell quit end tell
設定
自分の環境では下のようになっているので上のコードで1番目と3番目を入れ替えるようにしました.
ここは自分の設定でコードを書き換えてください.
macOS Sierraでkarabinerが使えなくて絶望した話
macOS Sierraではサードパーティ製のキーボードの無変換,変換を英数,かなに変えるkarabinerが使えなくなっていた.ダウングレードまで考えたが,karabiner-elementsというkarabinerの機能の簡単なところだけを実装したものを開発してくださったため,それを利用してみた.
方法
- 次のURLからdmgファイルをダウンロードしてインストール https://pqrs.org/latest/karabiner-elements-latest.dmg
- 設定ファイル(karabiner.json)は各自で用意しなければならない.今回は無変換,変換にかな,英数.capslockにcontrol.ファンクションキーにfn+F1~fn+F12,optionキーとcommandキーの交換を当てはめた
- mkdir -p ~/.karabiner.d/configuration/
- cd ~/.karabiner.d/configuration/
- touch karabiner.json
ソース
"profiles": [ { "name": "Default profile", "selected": true, "simple_modifications": { "f1": "vk_consumer_brightness_down", "f2": "vk_consumer_brightness_up", "f3": "vk_mission_control", "f4": "vk_launchpad", "f5": "vk_consumer_illumination_down", "f6": "vk_consumer_illumination_up", "f7": "vk_consumer_previous", "f8": "vk_consumer_play", "f9": "vk_consumer_next", "f10": "mute", "f11": "volume_down", "f12": "volume_up", "caps_lock": "left_control", "japanese_pc_nfer": "japanese_eisuu", "japanese_pc_xfer": "japanese_kana", "left_option": "left_command", "left_command": "left_option", "right_command": "right_option", "right_option": "right_command" } } ] }
アートワークを半自動でつけるスクリプトも作ってるので見てね
rinringo429.hatenablog.com
AppleScriptでiTunesのアートワークからアイコンを作成するまで
アイコンを作成する際に戸惑ってしまったので簡単にメモしておくことにします.
今回はiconutil というMac特有のアイコン作成コマンドを用いることにします.
Optimizing for High Resolution
iconutilを使う条件として,
- 画像の形式がPNG
- カラープロファイルがsRGB
- アルファチャンネルがON ← これを忘れてて悲惨だった
基本的にはこのサイトのようにすればアイコンを作成できます.
MacアプリのRetina対応アイコン(*.icns)を作成する方法 | DOTAPON Blog
慣れれば簡単ですが,上の3つの条件を満たしてないと"Unsuported image format"とエラーを吐きます.
今回ではiTunesのraw data から作成した画像は全てsRGBを満たしていたので(人によって違うかも)アルファチャンネルだけなんとかしました.
アルファチャンネルをセットする方法として,
- Photoshop
- imageMagick の-alpha オプション
の2つを案としてあげましたが,今回はコマンドベースで実行できるimageMagickで行いました.
imageMagickのインストールはhomebrew もしくは Macportがあれば簡単にできるので省略します.
ソース
tell application "iTunes" if (count of artwork of current track) ≥ 1 then tell application "iTunes" to tell artwork 1 of current track set d to raw data end tell set thePath to ((path to pictures folder) as text) & "applet.iconset" set icon_512 to open for access file (thePath & ":icon_512x512.png") with write permission set eof icon_512 to 0 write d to icon_512 close access icon_512 tell application "Image Events" launch set myImage to open file (thePath & ":icon_512x512.png") save myImage in file (thePath & ":icon_512x512.png") as PNG --iTunesのアートワークから作成した画像は拡張子がpngだけではないためPNGで保存する end tell delay 1 do shell script "sips -z 512 512 " & (POSIX path of thePath) & "/icon_512x512.png" --512x512のサイズにリサイズ do shell script "/usr/local/bin/convert " & (POSIX path of thePath) & "/icon_512x512.png -alpha Set " & (POSIX path of thePath) & "/icon_512x512.png" --imageMagickのconvertコマンドを使用,アルファチャンネルを追加している do shell script "iconutil -c icns " & (POSIX path of thePath) --iconsを作成 end if end tell
アートワークからアイコンを作りたいなんて機会は滅多にありませんが,次回は前回作成した曲の変わり目に通知を入れるスクリプトと組み合わせて通知にアートワークを表示してみたいと思います.
rinringo429.hatenablog.com