Created
December 3, 2008 07:37
-
-
Save ymnk/31466 to your computer and use it in GitHub Desktop.
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
| /* | |
| Copyright (c) 2008 ymnk, JCraft,Inc. All rights reserved. | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions are met: | |
| 1. Redistributions of source code must retain the above copyright notice, | |
| this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in | |
| the documentation and/or other materials provided with the distribution. | |
| 3. The names of the authors may not be used to endorse or promote products | |
| derived from this software without specific prior written permission. | |
| THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
| INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, | |
| INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
| EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| /** | |
| * This program will work as the gateway between the Skype Chat and the Twitter. | |
| * Skype4Java[1] is required to be compiled. | |
| * [1] https://developer.skype.com/wiki/Java_API | |
| */ | |
| import java.net.{Authenticator, PasswordAuthentication} | |
| import java.net.{URL, URLEncoder, HttpURLConnection} | |
| import com.skype.{Skype, Chat, ChatMessage, ChatMessageAdapter} | |
| import scala.xml.XML | |
| import java.text.SimpleDateFormat | |
| import java.util.Locale | |
| import scala.actors.Actor | |
| import scala.actors.Actor.loop | |
| import scala.concurrent.ops.spawn | |
| object SkypeChat2Twitter { | |
| Skype.setDeamon(false) | |
| def main(arg:Array[String]){ | |
| val (username, passwd) = (arg(0), arg(1)) // Twitter id and password. | |
| val chatId = arg(2) // chatid | |
| setAutenticator(username, passwd) | |
| val Array(chat) = Skype.getAllChats.filter(_.getId.toString==chatId) | |
| var lastSkypeWriteTime:Long = -1 | |
| val interval:Long = 3*1000 | |
| Skype.addChatMessageListener( | |
| new ChatMessageAdapter{ | |
| override def chatMessageReceived(m:ChatMessage)=chatMessage(m) | |
| override def chatMessageSent(m:ChatMessage)={ | |
| if(System.currentTimeMillis - lastSkypeWriteTime > interval){ | |
| chatMessage(m) | |
| } | |
| } | |
| private def chatMessage(m:ChatMessage){ | |
| val c=m.getChat | |
| if(chat==c){ | |
| val sender = m.getSender | |
| val senderId = m.getSenderId | |
| val senderDisplayName = m.getSenderDisplayName | |
| m.getType.toString match { | |
| case "SAID" => { | |
| val content = m.getContent | |
| writeToTwitter(senderDisplayName+": "+content) | |
| } | |
| case "ADDEDMEMBERS" => { | |
| writeToTwitter(senderDisplayName+" joined this chat.") | |
| } | |
| case "LEFT" => { | |
| writeToTwitter(senderDisplayName+" left this chat.") | |
| } | |
| case _ => | |
| } | |
| } | |
| } | |
| } | |
| ) | |
| def writeToSkypeChat(message: String){ | |
| lastSkypeWriteTime=System.currentTimeMillis | |
| chat.send(message) | |
| } | |
| new FriendsTimeline(username, | |
| new { | |
| def apply(t:String):Unit=writeToSkypeChat(t) | |
| }).start | |
| } | |
| private def writeToTwitter(text:String){ | |
| def chop(text:String):String={ | |
| text match { | |
| case e if e.length>140 => chop(text.substring(0, text.length-1)) | |
| case e => URLEncoder.encode(e, "UTF-8") | |
| } | |
| } | |
| val url = "http://twitter.com/statuses/update.xml?source=SkypeChat2Twitter&status="+chop(text) | |
| val urlConn = new URL(url).openConnection.asInstanceOf[HttpURLConnection] | |
| urlConn.setRequestMethod("POST") | |
| urlConn.connect() | |
| urlConn.getResponseCode | |
| } | |
| class FriendsTimeline(username:String, | |
| sender:{def apply(t:String):Unit}) extends Actor{ | |
| sealed abstract class MESSAGE | |
| case object GETTIMELINE extends MESSAGE | |
| val url = "http://twitter.com/statuses/replies.xml" | |
| val df = new SimpleDateFormat("EEE MMM dd HH:mm:ss +0000 yyyy", Locale.US) | |
| var lastTime:Long = -1 | |
| val interval = 60 * 1000 | |
| def act { | |
| this ! GETTIMELINE | |
| loop { | |
| react { | |
| case GETTIMELINE => { | |
| try{ lastTime = timeline(lastTime) } | |
| catch{case e:java.io.IOException => } | |
| ping(interval, GETTIMELINE) | |
| } | |
| } | |
| } | |
| } | |
| private def timeline(_lastTime:Long)={ | |
| var lastTime=_lastTime | |
| val urlConn = new URL(url).openConnection.asInstanceOf[HttpURLConnection] | |
| urlConn.connect(); | |
| urlConn.getResponseCode | |
| for (s <- XML.load(urlConn.getInputStream) \ "status" reverse ; | |
| created_at = s \ "created_at" text ; | |
| time = df.parse(created_at).getTime if(time>lastTime)) { | |
| lastTime=time | |
| var (text, user_name, screen_name) = | |
| (s \ "text" text, | |
| s \ "user" \ "name" text, | |
| s \ "user" \ "screen_name" text) | |
| if(username!=screen_name && | |
| text.startsWith("@"+username)){ | |
| if(_lastTime != -1){ // At first time, we will skip it. | |
| println(created_at+" ["+user_name+"] "+ | |
| text.substring(("@"+username).length).trim) | |
| sender(user_name+": "+ | |
| text.substring(("@"+username).length).trim) | |
| } | |
| } | |
| } | |
| lastTime | |
| } | |
| private def ping(sleep:Long, m:MESSAGE) { | |
| spawn { Thread.sleep(sleep); this ! m } | |
| } | |
| } | |
| private def setAutenticator(u:String, p:String){ | |
| Authenticator.setDefault( | |
| new Authenticator { | |
| override def getPasswordAuthentication = { | |
| new PasswordAuthentication(u, p.toCharArray); | |
| } | |
| } | |
| ) | |
| } | |
| } | |
| object ListChats { | |
| def main(arg:Array[String]){ | |
| Skype.getAllChats.foreach { c => | |
| import c._ | |
| println(getId+" | "+getWindowTitle) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment