Skip to content

Instantly share code, notes, and snippets.

@thash
Last active December 17, 2015 00:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thash/5520445 to your computer and use it in GitHub Desktop.
Save thash/5520445 to your computer and use it in GitHub Desktop.
Skype.appがローカルにSQLiteで保存しているチャット履歴を引っ張り出してEverNoteに突っ込むスクリプト. 1日の履歴をチャット窓ごとに分割されたNoteとして保存する usage: lein run 20130505
(defproject skype2ever "HEAD"
:repositories {"sonatype" "https://oss.sonatype.org/content/groups/public/"}
:local-repo "lib"
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/java.jdbc "0.3.0-alpha1"]
[org.xerial/sqlite-jdbc "3.7.2"]
[com.evernote/evernote-api "1.23"]
]
:main skype2ever.main)
(ns skype2ever.main
(:require [clojure.java.jdbc :as j]
[clojure.java.jdbc.sql :as s])
(:import [com.evernote.thrift.protocol TBinaryProtocol]
[com.evernote.thrift.transport THttpClient TTransportException]
[com.evernote.edam.userstore UserStore$Client]
[com.evernote.edam.notestore NoteStore$Client NoteFilter NoteList]
[com.evernote.edam.type Note Notebook]
[com.evernote.edam.error EDAMUserException]))
;; https://github.com/evernote/evernote-sdk-java/blob/master/sample/client/EDAMDemo.java
;; Developer Token
(def conf {:evernoteToken "XXXXXXXXXXXXXXXXXXXXXXXXX"
:evernoteHost "sandbox.evernote.com"
:skypeUser "t_hash"})
(def authToken (:evernoteToken conf))
(def userStore
(let [userStoreUrl (str "https://" (:evernoteHost conf) "/edam/user"),
userStoreProt (TBinaryProtocol. (THttpClient. userStoreUrl))]
(UserStore$Client. userStoreProt userStoreProt)))
(def noteStore
(let [noteStoreProt (TBinaryProtocol.
(THttpClient.
(. userStore getNoteStoreUrl authToken)))]
(NoteStore$Client. noteStoreProt noteStoreProt)))
(defn msgFormatter [message]
(str "<tr>"
"<td>" (. (java.text.SimpleDateFormat. "HH:mm:ss") format (java.util.Date. (* 1000 (:timestamp message)))) "</td>"
"<td>" (:from_dispname message) "</td>"
"<td>" (:body_xml message) "</td>"
"</tr>"))
(defn saveMessages [title messages]
(let [note (Note.),
tableStyle (str "color:#535353;"
"background-color:#ECECEC;"
"border:1px solid rgba(0, 0, 0, 0.2);"
"width:100%;"
"font-size: 11px;"
"margin: 0 0 0 3px;"),
thStyle (str "padding:3px;"
"text-align:center;"
"border:1px solid rgba(0, 0, 0, 0.2);"
"color:#ECECEC;"
"background-color:#636363;")]
(. note setTitle title)
(. note addToTagNames "skype")
(. note setContent
(str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
"<en-note>"
"<table style='" tableStyle "'>"
"<tr>"
"<th style='" thStyle "'>time</th>"
"<th style='" thStyle "'>from</th>"
"<th style='" thStyle "'>text</th>"
"</tr>"
(clojure.string/join (map msgFormatter messages))
"</table>"
"</en-note>"))
(println note)
(. noteStore createNote authToken note)
))
(defn -main [& args]
(if (nil? (first args)) (System/exit 1))
(let [db {:classname "org.sqlite.JDBC" :subprotocol "sqlite"
:subname (str "/Users/hash/Library/Application Support/Skype/" (:skypeUser conf) "/main.db")}
targetDate (. (java.text.SimpleDateFormat. "yyyyMMdd") parse (first args)),
targetTimestamp (/ (. targetDate getTime) 1000), ;; milliseconds to seconds
messages (j/query db [(str "SELECT *"
" FROM messages"
" WHERE timestamp BETWEEN "
targetTimestamp
" AND "
(+ targetTimestamp (* 24 60 60))
" ORDER BY timestamp ASC")])]
(defn friendlyChatname [chatname]
(:friendlyname (first (j/query db [(str "SELECT * FROM chats WHERE name='" chatname "'")]))))
(map (fn [groupedMsg]
(saveMessages
(str "Skype["
(. (java.text.SimpleDateFormat. "yyyy/MM/dd") format targetDate)
"] "
(friendlyChatname (first groupedMsg)))
(last groupedMsg)))
(group-by :chatname messages))))
;; TODO
;; * confの部分を設定ファイルで切り替えられるようにする
;; * Mac以外のOSでSkype DB所在地を調べて対応
;; * 常駐アプリにするかcron的機能をつける
@thash
Copy link
Author

thash commented May 5, 2013

結果:

@syou6162
Copy link

syou6162 commented May 6, 2013

(. note addToTagNames "skype")みたいなところはdotoとか..みたいなマクロがもっと綺麗にしてくれると思うよん。似たような感じでthreading macro->>とかはhash-mapとかlistに対してhaskellの$が左から右に読めるようにしたような感じで書けるのでオススメです。

@thash
Copy link
Author

thash commented May 6, 2013

ありがとう!改修していきます

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment