Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created November 28, 2010 12:27
Show Gist options
  • Save brianhsu/718878 to your computer and use it in GitHub Desktop.
Save brianhsu/718878 to your computer and use it in GitHub Desktop.
噗浪河道備份工具 by SPlurk.
package org.bone.plurkbackup
import org.bone.splurk._
import org.bone.splurk.constant._
import org.bone.splurk.model._
import java.util.Date
import java.text.SimpleDateFormat
object Main
{
val apiKey = "wQsqUmmcp69aCblemZ34pM5wywnlAyS9"
val plurkClient = new PlurkClient(apiKey)
val dateFormatter = new SimpleDateFormat ("yyyy-MM-dd")
val timeFormatter = new SimpleDateFormat ("hh:mm:ss")
var allUsers: Map[Long, PlurkUser] = Map()
lazy val username = {
print ("Enter your Plurk username:")
Console.readLine()
}
lazy val password = {
print ("Enter your Plurk password:")
Console.readLine()
}
// 將噗文轉成 HTML 格式
def convertToHTML (plurk: Plurk) = {
val userInfo = allUsers(plurk.ownerID)
val dateWithLink = <a href={plurk.plurkURL}>{dateFormatter.format(plurk.posted)}</a>
val time = timeFormatter.format(plurk.posted)
val userURL = "http://plurk.com/" + userInfo.nickName
val username = userInfo.displayName.getOrElse(userInfo.fullName.getOrElse(userInfo.nickName))
val userLink = <a href={userURL}>{username}</a>
val qualifier = plurk.qualifierTranslated.getOrElse(plurk.qualifier.text)
val content = plurk.content;
"""
|<tr>
| <td>%s</td>
| <td>%s</td>
| <td>%s</td>
| <td>%s</td>
| <td>%s</td>
|</tr>
""".stripMargin.format(dateWithLink, time, userLink, qualifier, content)
}
// 輸出成檔案
def outputHTMLFile (filename: String, plurks: List[Plurk])
{
import java.io._
val printWriter = new PrintWriter(new File(filename), "utf-8")
printWriter.println("<html>")
printWriter.println("<body>")
printWriter.println("<table border=\"1\" cellpadding=\"3\">")
printWriter.println(plurks.map(convertToHTML).mkString)
printWriter.println("</table>")
printWriter.println("</body>")
printWriter.println("</html>")
printWriter.close()
}
// 遞迴取得河道上所有的噗文
def fetchPlurks (dateBound: Date, filter: Option[PlurkFilter.PlurkFilter] = None): List[Plurk] = {
val (users, plurks) = plurkClient.Timeline.getPlurks (limit = 30,
olderThan = Some(dateBound),
filter = filter)
allUsers = allUsers ++ users
plurks match {
case Nil => Nil
case xs: List[_] =>
println ("Chunk:" + xs.length)
xs.foreach {x => println ("Processing plurkID %d..." format(x.plurkID)) }
fetchPlurks(xs.last.posted, filter) ++ xs
}
}
def main (args: Array[String]) {
val profile = plurkClient.Users.login (username, password)
val allPlurks = fetchPlurks (new Date)
// 河道上所有的噗
val allPlurksBackup = allPlurks.sortWith(_.posted.getTime > _.posted.getTime)
// 只有自己的噗
val myPlurksBackup = allPlurks.sortWith(_.posted.getTime > _.posted.getTime).
filter(_.ownerID == profile.userInfo.userID)
// 備份啦
outputHTMLFile ("allPlurks.html", allPlurksBackup)
outputHTMLFile ("myPlurks.html", myPlurksBackup)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment