Created
June 23, 2017 06:59
-
-
Save 4noha/3f91474c0277b1d7422663d5cab4d638 to your computer and use it in GitHub Desktop.
[WIP] slackにNow Playingするやつ(Win用)。アクセストークンの差異で発言元をbotと自アカウント(未実装)で選べる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8" ?> | |
| <package> | |
| <job> | |
| <runtime></runtime> | |
| <script language="JScript"> | |
| <![CDATA[ | |
| /****************************************************************************** | |
| ** | |
| ** now_playing_win.wsf | |
| ** Created by Takuya Noaki on 17/06/20. | |
| ** | |
| ** 使い方1: cscript.exe now_playing.wsf ユーザ名 | |
| ** 使い方2: slackAccessTokenに自分のアクセストークンを設定(未実装) | |
| ** cscript.exe now_playing.wsf | |
| ** | |
| ******************************************************************************/ | |
| /* JSON2 */ | |
| var JSON;JSON||(JSON={}); | |
| (function(){function k(a){return a<10?"0"+a:a}function o(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=r[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function l(a,j){var c,d,h,m,g=e,f,b=j[a];b&&typeof b==="object"&&typeof b.toJSON==="function"&&(b=b.toJSON(a));typeof i==="function"&&(b=i.call(j,a,b));switch(typeof b){case "string":return o(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null"; | |
| e+=n;f=[];if(Object.prototype.toString.apply(b)==="[object Array]"){m=b.length;for(c=0;c<m;c+=1)f[c]=l(c,b)||"null";h=f.length===0?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(i&&typeof i==="object"){m=i.length;for(c=0;c<m;c+=1)typeof i[c]==="string"&&(d=i[c],(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h))}else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h);h=f.length===0?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+ | |
| "}";e=g;return h}}if(typeof Date.prototype.toJSON!=="function")Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()};var q=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, | |
| p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,e,n,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},i;if(typeof JSON.stringify!=="function")JSON.stringify=function(a,j,c){var d;n=e="";if(typeof c==="number")for(d=0;d<c;d+=1)n+=" ";else typeof c==="string"&&(n=c);if((i=j)&&typeof j!=="function"&&(typeof j!=="object"||typeof j.length!=="number"))throw Error("JSON.stringify");return l("", | |
| {"":a})};if(typeof JSON.parse!=="function")JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&typeof b==="object")for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),f!==void 0?b[g]=f:delete b[g]);return e.call(a,d,b)}var d,a=String(a);q.lastIndex=0;q.test(a)&&(a=a.replace(q,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, | |
| "]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),typeof e==="function"?c({"":d},""):d;throw new SyntaxError("JSON.parse");}})(); | |
| /********************************************************/ | |
| var iTunes = WScript.CreateObject( 'iTunes.Application' ); | |
| // 自アカウント投稿機能未実装 | |
| var slackAccessToken = 'Your Token'; | |
| var webHookURL = 'Webhook URL'; | |
| var username = null; | |
| var reqData = { | |
| channel: '#now-playing', | |
| text: null | |
| }; | |
| var pollingLoop = function() { | |
| if ( iTunes.PlayerState === 1 ) { | |
| var track = iTunes.CurrentTrack; | |
| var trackInfo = 'Now Playing - ' + track.Name + ' '; | |
| trackInfo += ( track.Artist != '' ? ( '/ ' + track.Artist ) : '' ) + ' ♫♫ '; | |
| trackInfo += username != null ? username : ''; | |
| if ( reqData['text'] != trackInfo ) { | |
| reqData['text'] = trackInfo; | |
| } else { | |
| // 前回のポーリングと同じ曲 | |
| return; | |
| } | |
| } else { | |
| // 再生していない | |
| return; | |
| } | |
| WScript.Echo( trackInfo ); | |
| //Bot or User post | |
| try { | |
| var http = new ActiveXObject( 'Msxml2.ServerXMLHTTP' ); | |
| if ( slackAccessToken == 'Your Token' ) { | |
| http.open('POST', webHookURL ); | |
| } else { | |
| http.open('POST', 'https://slack.com/api/chat.postMessage' ); | |
| } | |
| http.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); | |
| http.send( 'payload=' + JSON.stringify( reqData ) ); | |
| } catch( e ) { | |
| WScript.Echo( 'Error(' + e.number & 0xFFFF + '):' + e.message ); | |
| } | |
| } | |
| var slackAccessToken = 'Your Token'; | |
| var oArgs = WScript.Arguments; | |
| if ( oArgs.length == 1 ) { | |
| username = oArgs(0); | |
| } else { | |
| if ( slackAccessToken == 'Your Token' ) { | |
| WScript.Echo('コマンドライン引数はユーザネームだけお願いします~!' ); | |
| WScript.Quit( 1 ); | |
| } | |
| reqData['token'] = slackAccessToken; | |
| } | |
| WScript.Echo( 'Ctrl+Cで終了' ); | |
| for (;;) { | |
| pollingLoop(); | |
| WScript.Sleep( 2000 ); | |
| } | |
| ]]> | |
| </script> | |
| </job> | |
| </package> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****************************************************************************** | |
| ** | |
| ** now_playing_win.js | |
| ** Created by Takuya Noaki on 17/06/20. | |
| ** | |
| ** Shift-JISじゃないと動かんのでwsfでラップする必要がある | |
| ** 使い方1: cscript.exe now_playing_win.js ユーザ名 | |
| ** 使い方2: slackAccessTokenに自分のアクセストークンを設定(未実装) | |
| ** cscript.exe now_playing_win.js | |
| ** | |
| ******************************************************************************/ | |
| /* JSON2 */ | |
| var JSON;JSON||(JSON={}); | |
| (function(){function k(a){return a<10?"0"+a:a}function o(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=r[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function l(a,j){var c,d,h,m,g=e,f,b=j[a];b&&typeof b==="object"&&typeof b.toJSON==="function"&&(b=b.toJSON(a));typeof i==="function"&&(b=i.call(j,a,b));switch(typeof b){case "string":return o(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null"; | |
| e+=n;f=[];if(Object.prototype.toString.apply(b)==="[object Array]"){m=b.length;for(c=0;c<m;c+=1)f[c]=l(c,b)||"null";h=f.length===0?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(i&&typeof i==="object"){m=i.length;for(c=0;c<m;c+=1)typeof i[c]==="string"&&(d=i[c],(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h))}else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h);h=f.length===0?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+ | |
| "}";e=g;return h}}if(typeof Date.prototype.toJSON!=="function")Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()};var q=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, | |
| p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,e,n,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},i;if(typeof JSON.stringify!=="function")JSON.stringify=function(a,j,c){var d;n=e="";if(typeof c==="number")for(d=0;d<c;d+=1)n+=" ";else typeof c==="string"&&(n=c);if((i=j)&&typeof j!=="function"&&(typeof j!=="object"||typeof j.length!=="number"))throw Error("JSON.stringify");return l("", | |
| {"":a})};if(typeof JSON.parse!=="function")JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&typeof b==="object")for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),f!==void 0?b[g]=f:delete b[g]);return e.call(a,d,b)}var d,a=String(a);q.lastIndex=0;q.test(a)&&(a=a.replace(q,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, | |
| "]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),typeof e==="function"?c({"":d},""):d;throw new SyntaxError("JSON.parse");}})(); | |
| /********************************************************/ | |
| var iTunes = WScript.CreateObject( 'iTunes.Application' ); | |
| // 自アカウント投稿機能未実装 | |
| var slackAccessToken = 'Your Token'; | |
| var webHookURL = 'Webhook URL'; | |
| var username = null; | |
| var reqData = { | |
| channel: '#now-playing', | |
| text: null | |
| }; | |
| var pollingLoop = function() { | |
| if ( iTunes.PlayerState === 1 ) { | |
| var track = iTunes.CurrentTrack; | |
| var trackInfo = 'Now Playing - ' + track.Name + ' '; | |
| trackInfo += ( track.Artist != '' ? ( '/ ' + track.Artist ) : '' ) + ' ♫♫ '; | |
| trackInfo += username != null ? username : ''; | |
| if ( reqData['text'] != trackInfo ) { | |
| reqData['text'] = trackInfo; | |
| } else { | |
| // 前回のポーリングと同じ曲 | |
| return; | |
| } | |
| } else { | |
| // 再生していない | |
| return; | |
| } | |
| WScript.Echo( trackInfo ); | |
| //Bot or User post | |
| try { | |
| var http = new ActiveXObject( 'Msxml2.ServerXMLHTTP' ); | |
| if ( slackAccessToken == 'Your Token' ) { | |
| http.open('POST', webHookURL ); | |
| } else { | |
| http.open('POST', 'https://slack.com/api/chat.postMessage' ); | |
| } | |
| http.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); | |
| http.send( 'payload=' + JSON.stringify( reqData ) ); | |
| } catch( e ) { | |
| WScript.Echo( 'Error(' + e.number & 0xFFFF + '):' + e.message ); | |
| } | |
| } | |
| var slackAccessToken = 'Your Token'; | |
| var oArgs = WScript.Arguments; | |
| if ( oArgs.length == 1 ) { | |
| username = oArgs(0); | |
| } else { | |
| if ( slackAccessToken == 'Your Token' ) { | |
| WScript.Echo('コマンドライン引数はユーザネームだけお願いします~!' ); | |
| WScript.Quit( 1 ); | |
| } | |
| reqData['token'] = slackAccessToken; | |
| } | |
| WScript.Echo( 'Ctrl+Cで終了' ); | |
| for (;;) { | |
| pollingLoop(); | |
| WScript.Sleep( 2000 ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment