Created
January 12, 2015 14:29
-
-
Save alphaKAI/82dd23c8b37cf0dcea44 to your computer and use it in GitHub Desktop.
Twitter Notification tool, twitnotify for mac (original: https://github.com/alphaKAI/twitnotify this program require https://github.com/alloy/terminal-notifier)
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
| /* | |
| TwitNotify | |
| The Twitter Notification tool. | |
| Copyright (C) 2014-2015 alphaKAI http://alpha-kai-net.info | |
| The MIT License | |
| using : My Twitter API Wrapper Twitter4D(twitter4d.d) | |
| Twitter4D's Repository : https://github.com/alphaKAI/twitter4d | |
| Twitter4D's License : The MIT License | |
| Installation | |
| 1 - clone this gist | |
| 2 - clone Twitter4D | |
| 3 - Copy twitter4d.d | |
| 4 - create setting.json and edit | |
| //setting.json | |
| { | |
| "consumerKey" : "Your CK", | |
| "consumerSecret" : "Your CKS", | |
| "accessToken" : "Your AT", | |
| "accessTokenSecret" : "Your ATS" | |
| } | |
| 5 - compile | |
| $ dmd twitnotify.d twitter4d.d -L-lcurl | |
| */ | |
| import twitter4d; | |
| import std.net.curl, | |
| std.process, | |
| std.string, | |
| std.stdio, | |
| std.regex, | |
| std.file, | |
| std.conv, | |
| std.json; | |
| import std.c.linux.linux; | |
| class TwitNotify{ | |
| Twitter4D t4d; | |
| string iconBasePath; | |
| string userName; | |
| string defaultTime = "1500"; | |
| this(){ | |
| string jsonString = readSettingFile(); | |
| auto parsed = parseJSON(jsonString); | |
| iconBasePath = getcwd() ~ "/icons"; | |
| t4d = new Twitter4D([ | |
| "consumerKey" : getJsonData(parsed, "consumerKey"), | |
| "consumerSecret" : getJsonData(parsed, "consumerSecret"), | |
| "accessToken" : getJsonData(parsed, "accessToken"), | |
| "accessTokenSecret" : getJsonData(parsed, "accessTokenSecret")]); | |
| userName = getJsonData(parseJSON(t4d.request("GET", "account/verify_credentials.json", ["" : ""])), "screen_name"); | |
| } | |
| void startWatchingService(){ | |
| foreach(line; t4d.stream()){ | |
| try{ | |
| if(match(line.to!string, regex(r"\{.*\}"))){ | |
| auto parsed = parseJSON(line.to!string); | |
| if("event" in parsed.object) | |
| execNotification(parseEvent(parsed)); | |
| if("text" in parsed.object){//For retweet notify | |
| string textData = getJsonData(parsed, "text"); | |
| if(match(textData, regex(r"@" ~ userName))){//For Reply | |
| JSONValue userJson = parsed.object["user"]; | |
| string name = getJsonData(userJson, "name"); | |
| string screenName = getJsonData(userJson, "screen_name"); | |
| string[string] message; | |
| if(match(textData, regex(r"^RT @" ~ userName))){ | |
| message["event"] = "retweet"; | |
| message["icon"] = getIconPath(userJson); | |
| message["urgency"] = "normal"; | |
| message["wait"] = defaultTime; | |
| message["title"] = name ~ "(@" ~ screenName ~ ") retweet your tweet!"; | |
| message["body"] = getJsonData(parsed, "text").replace(regex(r"^ RT @" ~ screenName ~ r": \s"), ""); | |
| } else { | |
| message["event"] = "reply"; | |
| message["icon"] = getIconPath(userJson); | |
| message["urgency"] = "critical"; | |
| message["wait"] = defaultTime; | |
| message["title"] = "Reply From " ~ name ~ "(@" ~ screenName ~ ")"; | |
| message["body"] = textData; | |
| } | |
| execNotification(message); | |
| } | |
| } | |
| } | |
| } | |
| catch(Exception ignored){}//To avoid crash : If target tweet include pictograph, this program will be crash. | |
| } | |
| } | |
| private{ | |
| string readSettingFile(){ | |
| passwd *pw = getpwuid(getuid); | |
| string homeDir = pw.pw_dir.to!string; | |
| string settingFilePath = homeDir ~ "/.myscripts/twitterConf/setting.json"; | |
| if(!exists(settingFilePath)) | |
| throw new Error("Please create file of setting.json and configure your consumer & access tokens"); | |
| auto file = File(settingFilePath, "r"); | |
| string buf; | |
| foreach(line; file.byLine) | |
| buf = buf ~ cast(string)line; | |
| return buf; | |
| } | |
| string getJsonData(JSONValue parsedJson, string key){ | |
| return parsedJson.object[key].to!string.replace(regex("\"", "g") ,""); | |
| } | |
| string[string] parseEvent(JSONValue parsedJson){ | |
| string eventName = getJsonData(parsedJson, "event"); | |
| JSONValue targetJson = parsedJson.object["target"]; | |
| JSONValue sourceJson = parsedJson.object["source"]; | |
| string name = getJsonData(sourceJson, "name"); | |
| string screenName = getJsonData(sourceJson, "screen_name"); | |
| string[string] message = ["" : ""]; | |
| switch(eventName){ | |
| case "favorite": | |
| if(screenName == userName) | |
| goto default; | |
| message["event"] = eventName; | |
| message["icon"] = getIconPath(sourceJson); | |
| message["urgency"] = "normal"; | |
| message["wait"] = defaultTime; | |
| message["title"] = name ~ "(@" ~ screenName ~ ") favorite your tweet!"; | |
| message["body"] = getJsonData(parsedJson.object["target_object"], "text"); | |
| break; | |
| case "unfavorite": | |
| if(screenName == userName) | |
| goto default; | |
| message["event"] = eventName; | |
| message["icon"] = getIconPath(sourceJson); | |
| message["urgency"] = "critical"; | |
| message["wait"] = defaultTime; | |
| message["title"] = name ~ "(@" ~ screenName ~ ") unfavorite your tweet"; | |
| message["body"] = getJsonData(parsedJson.object["target_object"], "text"); | |
| break; | |
| case "follow": | |
| if(screenName == userName) | |
| goto default; | |
| message["event"] = eventName; | |
| message["icon"] = getIconPath(sourceJson); | |
| message["urgency"] = "normal"; | |
| message["wait"] = defaultTime; | |
| message["title"] = "<span size=\"10500\">" ~ name ~ "(@" ~ screenName ~ ") follow you!" ~ "</span>"; | |
| message["body"] = ""; | |
| break; | |
| default: | |
| break; | |
| } | |
| return message; | |
| } | |
| string getIconPath(JSONValue sourceJson){ | |
| string iconUrl = getJsonData(sourceJson, "profile_image_url_https").replace(regex(r"\\", "g"), ""); | |
| string screenName = getJsonData(sourceJson, "screen_name"); | |
| string iconPath; | |
| if(saveIconImage(iconUrl, screenName)) | |
| iconPath = iconBasePath ~ "/" ~ screenName ~ ".jpeg"; | |
| else | |
| iconPath = "NULL"; | |
| return iconPath; | |
| } | |
| bool saveIconImage(string iconUrl, string screenName){ | |
| string iconPath = iconBasePath ~ "/" ~ screenName ~ ".jpeg"; | |
| writeln(iconUrl); | |
| download(iconUrl, iconPath); | |
| if(!exists(iconPath)) | |
| return false; | |
| return true; | |
| } | |
| void execNotification(string[string] sendMessage){ | |
| if(sendMessage == ["" : ""]) | |
| return; | |
| writeln("[EVENT] => ", sendMessage["event"]); | |
| string notifyCommandString; | |
| version(OSX) | |
| notifyCommandString = "terminal-notifier "; | |
| if(notifyCommandString is null) | |
| notifyCommandString = "notify-send "; | |
| bool flag; | |
| version(OSX){ | |
| notifyCommandString ~= "-appIcon " ~ "\'" ~ sendMessage["icon"] ~ "\' "; | |
| notifyCommandString ~= "-title " ~ "\'" ~ sendMessage["title"] ~ "\' "; | |
| notifyCommandString ~= "-message " ~ "\'" ~ sendMessage["body"] ~ "\'"; | |
| flag = true; | |
| } | |
| if(!flag){ | |
| if(sendMessage["icon"] != "NULL") | |
| notifyCommandString ~= "-i " ~ sendMessage["icon"] ~ " "; | |
| notifyCommandString ~= "-u " ~ sendMessage["urgency"] ~ " "; | |
| notifyCommandString ~= "-t " ~ sendMessage["wait"] ~ " "; | |
| notifyCommandString ~= "\'" ~ sendMessage["title"] ~ "\'" ~ " "; | |
| notifyCommandString ~= "\'" ~ sendMessage["body"] ~ "\'"; | |
| } | |
| notifyCommandString.writeln; | |
| system(notifyCommandString); | |
| } | |
| } | |
| } | |
| void main(){ | |
| TwitNotify tn = new TwitNotify(); | |
| tn.startWatchingService(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment