Skip to content

Instantly share code, notes, and snippets.

@yllan
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save yllan/45d09a72e66303798714 to your computer and use it in GitHub Desktop.

Select an option

Save yllan/45d09a72e66303798714 to your computer and use it in GitHub Desktop.
Perfume LOCKS fetcher script in swift
import Cocoa
var quotations: Dictionary<String, String[]> = Dictionary()
for page in 1...76 {
println("page \(page)")
let url = NSURL(string: "http://www.tfm.co.jp/lock/perfume/index.php?catid=23&page=\(page)")
let data = NSData(contentsOfURL: url)
let htmlOpt: NSString? = NSString(data: data, encoding: NSJapaneseEUCStringEncoding)
// On page 39, the html cannot be converted using NSJapaneseEUCStringEncoding and we got a nil.
if let html = htmlOpt {
// If you copy the pattern あ~ちゃん and paste it in Xcode, it won't match anything because
// Safari will translate the U+301C(〜) to U+FF5E(~). So we use "あ.ちゃん" as pattern.
// Besides, "あ.ちゃん" also matches "あ~ちゃん".
let pattern = NSRegularExpression(pattern: "(あ.ちゃん|かしゆか|のっち)</b>\\s*「([^」]+)」", options: nil, error: nil)
for match in pattern.matchesInString(html, options: nil, range: NSMakeRange(0, html.length())) as NSTextCheckingResult[] {
let name = html.substringWithRange(match.rangeAtIndex(1))
let sentence = html.substringWithRange(match.rangeAtIndex(2))
if !quotations[name] {
quotations[name] = [sentence]
} else {
var sentences = quotations[name]!
sentences.append(sentence)
quotations[name] = sentences
}
}
}
}
var dict = quotations as NSDictionary
dict.writeToFile("quotes.plist", atomically: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment