Last active
September 27, 2016 21:34
-
-
Save gitbricho/52074b8c2ca049edfe70 to your computer and use it in GitHub Desktop.
swift:r100
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
[ | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-title1.mp4", | |
"group": "artist1", | |
"title": "title1", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-[2013]title2.mp4", | |
"group": "artist1", | |
"title": "title2", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2013]", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-[2014]title3.mp4", | |
"group": "artist1", | |
"title": "title3", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2014]", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-[2000]title4(1).mp4", | |
"group": "artist1", | |
"title": "title4", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2000]", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-[2000]title4(2).mp4", | |
"group": "artist1", | |
"title": "title4", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2000]", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/jazz/artist1-[2000][email protected]", | |
"group": "artist1", | |
"title": "title4", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2000]", | |
"zWhere": "", | |
"tags": [ "jazz", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/Taylor/Taylor Swift - Last Christmas @Xmas.mp4", | |
"group": "Taylor Swift", | |
"title": "Last Christmas", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "Taylor", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/Taylor/Taylor Swift - Fearless @Live [2010].mp4", | |
"group": "Taylor Swift", | |
"title": "Fearless", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "[2010]", | |
"zWhere": "", | |
"tags": [ "Taylor", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Music/Avril/Avril-I Can Do Better @On Stage.mp4", | |
"group": "Avril", | |
"title": "I Can Do Better", | |
"fType": "video", | |
"cType": "Music", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "Avril", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/TV/mentalist/Mentalist06-06_011.mp4", | |
"group": "Mentalist06", | |
"title": "06_011", | |
"fType": "video", | |
"cType": "TV", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "mentalist", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/TV/mentalist/Mentalist06-06_012.mp4", | |
"group": "Mentalist06", | |
"title": "06_012", | |
"fType": "video", | |
"cType": "TV", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "mentalist", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Movie/AC-96時間レクイエム[2014].mp4", | |
"group": "AC", | |
"title": "96時間レクイエム", | |
"fType": "video", | |
"cType": "Movie", | |
"zWhen": "[2014]", | |
"zWhere": "", | |
"tags": [ "Movie", | |
] | |
}, | |
{ | |
"fPath": "/_contents/video/Movie/AN-メリダとおそろしの森.mp4", | |
"group": "AN", | |
"title": "メリダとおそろしの森", | |
"fType": "video", | |
"cType": "Movie", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "Movie", | |
] | |
}, | |
{ | |
"fPath": "/_contents/pdf/説明書/NAD11クレードル説明書.pdf", | |
"group": "説明書", | |
"title": "NAD11クレードル説明書", | |
"fType": "pdf", | |
"cType": "", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "説明書", | |
] | |
}, | |
{ | |
"fPath": "/_contents/pdf/譜面/憧憬のスタンダード/アメイジング・グレイス.pdf", | |
"group": "憧憬のスタンダード", | |
"title": "アメイジング・グレイス", | |
"fType": "pdf", | |
"cType": "", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "憧憬のスタンダード", | |
] | |
}, | |
{ | |
"fPath": "/_contents/pdf/譜面/譜面Jazz01003.pdf", | |
"group": "譜面", | |
"title": "譜面Jazz01003", | |
"fType": "pdf", | |
"cType": "", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "譜面", | |
] | |
}, | |
{ | |
"fPath": "/_contents/picture/movie_ticket/ある夜の出来事.jpg", | |
"group": "movie_ticket", | |
"title": "ある夜の出来事", | |
"fType": "picture", | |
"cType": "", | |
"zWhen": "", | |
"zWhere": "", | |
"tags": [ "movie_ticket", | |
] | |
}] |
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
//: [Previous](@previous) | |
import UIKit | |
//## 実用コード ##### | |
/** | |
* 処理内容: | |
* ディレクトリとファイル名の命名規約で分類したコンテンツをビューアー・アプリで | |
* 利用しやすいように、属性とタグを抽出して、JSONデータとしてファイルに保存する | |
*/ | |
let files:[String] = [ | |
"/_contents/video/Music/jazz/artist1-title1.mp4", | |
"/_contents/video/Music/jazz/artist1-[2013]title2.mp4", | |
"/_contents/video/Music/jazz/artist1-[2014]title3.mp4", | |
"/_contents/video/Music/jazz/artist1-[2000]title4(1).mp4", | |
"/_contents/video/Music/jazz/artist1-[2000]title4(2).mp4", | |
"/_contents/video/Music/jazz/artist1-[2000][email protected]", | |
"/_contents/video/Music/Taylor/Taylor Swift - Last Christmas @Xmas.mp4", | |
"/_contents/video/Music/Taylor/Taylor Swift - Fearless @Live [2010].mp4", | |
"/_contents/video/Music/Avril/Avril-I Can Do Better @On Stage.mp4", | |
"/_contents/video/TV/mentalist/Mentalist06-06_011.mp4", | |
"/_contents/video/TV/mentalist/Mentalist06-06_012.mp4", | |
"/_contents/video/Movie/AC-96時間レクイエム[2014].mp4", | |
"/_contents/video/Movie/AN-メリダとおそろしの森.mp4", | |
"/_contents/pdf/説明書/NAD11クレードル説明書.pdf", | |
"/_contents/pdf/譜面/憧憬のスタンダード/アメイジング・グレイス.pdf", | |
"/_contents/pdf/譜面/譜面Jazz01003.pdf", | |
"/_contents/picture/movie_ticket/ある夜の出来事.jpg", | |
] | |
//## 仕様:命名規約から属性を取得する | |
// # アーティスト名、シリーズ名、映画のジャンル(AC,AN,DR,LC...) | |
// # タイトル | |
// # [制作年/制作年代] | |
// # ファイル種別(video:mp4,avi,..),(picture:png,jpg,..),(pdf),(txt),.. | |
// # コンテンツ種別(Music:音楽, TV:テレビドラマ, Movie:映画,..) | |
// # @ライブ/場所 | |
// # %1出演者、%2共演者、%3監督 | |
// # タグ(ライブ,場所,制作年,制作年代,フォルダ名, | |
struct ContentDef { | |
var fPath: String = "" | |
var group: String = "" | |
var title: String = "" | |
var fType: String = "" | |
var cType: String = "" | |
var zWhen: String = "" | |
var zWhere: String = "" | |
var tags: Set<String> = [] | |
func toJsonString() -> [String] { | |
var rtnStr = [ | |
"{\n", | |
" \"fPath\": \"\(self.fPath)\",\n", | |
" \"group\": \"\(self.group)\",\n", | |
" \"title\": \"\(self.title)\",\n", | |
" \"fType\": \"\(self.fType)\",\n", | |
" \"cType\": \"\(self.cType)\",\n", | |
" \"zWhen\": \"\(self.zWhen)\",\n", | |
" \"zWhere\": \"\(self.zWhere)\",\n", | |
" \"tags\": ["] | |
for aTag in self.tags { | |
rtnStr.append(" \"\(aTag)\",\n") | |
} | |
rtnStr.append(" ]\n") | |
rtnStr.append("}") | |
return rtnStr | |
} | |
} | |
var contents: [ContentDef] = [] | |
let ftypes = ["video", "picture", "pdf", "txt"] | |
let ctypes = ["Movie", "Music", "TV"] | |
let mvGenue = ["AC","AN","CM","DR","HL","LC","LV","SF","TS"] | |
//属性サーチ正規表現 | |
let rgxYear:String = "\\[\\d{4}\\]" //[2004], [1988], ... | |
let rgxNo:String = "\\([0-9]+\\)" //(0),(1),(2)... | |
let rgxLoc:String = "@.+" //@1文字以上の任意文字列 | |
let rgxPerson:String = "%[0-9].+" //%1XXXX, %2XXXX, ... | |
for file in files { | |
//## 抽出情報からコンテンツ定義クラスを作成する | |
var content = ContentDef() | |
content.fPath = file | |
//## パスをフォルダ名に分割 | |
let dirs: [String] = file.components(separatedBy: "/") | |
//print(dirs), | |
for dir in dirs { | |
//# 分割フォルダからファイル種別を取り出す | |
if let index = ftypes.index(of: dir) { | |
content.fType = ftypes[index] | |
} | |
if let index = ctypes.index(of: dir) { | |
content.cType = ctypes[index] | |
} | |
} | |
//# 親フォルダを取得 | |
let pFolder = dirs[dirs.count-2] | |
content.tags.insert(pFolder) | |
//# ファイル名を取得 | |
var fName = dirs[dirs.count-1] | |
//## ファイル名を解析 | |
// [yymm] の抽出: 今の所 year 情報は1つだけとする | |
do { | |
var results:[String] = [] | |
let regxp = try NSRegularExpression(pattern: rgxYear, options: []) | |
let matches = regxp.matches( | |
in: fName, options:[], range:NSMakeRange(0, (fName as NSString).length)) | |
if matches.count > 0 { | |
for match in matches { | |
results.append( (fName as NSString).substring(with: match.range) ) | |
} | |
//print(results) | |
content.zWhen = results[0] | |
// ファイル名から [yymm] を削除 | |
fName = fName.replacingOccurrences(of: rgxYear, with: "", options:.regularExpression, range: nil) | |
} | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
// タイトルから 添字情報(NO) を削除 | |
fName = fName.replacingOccurrences(of: rgxNo, with: "", options: .regularExpression, range: nil) | |
// タイトルから場所情報を削除 | |
fName = fName.replacingOccurrences(of: rgxLoc, with: "", options:.regularExpression, range: nil) | |
//# グループ(アーティスト名、シーリーズ名、映画ジャンル、...)とタイトルに分割 | |
let fnames = fName.components(separatedBy: "-") | |
var group = "" | |
var title = fnames[0] | |
if fnames.count > 1 { | |
group = fnames[0] | |
title = fnames[1] | |
} | |
if group.isEmpty { | |
group = pFolder | |
} | |
// グループとタイトルの前後の余分なスペースを取る | |
group = group.trimmingCharacters(in:NSCharacterSet.whitespaces) | |
title = title.trimmingCharacters(in:NSCharacterSet.whitespaces) | |
//## アーティスト名部分の解析 | |
//# 映画ジャンルの取得 | |
//# | |
//## タイトル部分の解析 | |
let titles = title.components(separatedBy: ".") | |
let ext = titles[titles.count-1] | |
if titles.count > 1 { | |
title = titles[titles.count-2] | |
} | |
content.group = group | |
content.title = title | |
contents.append(content) | |
} | |
for content in contents { | |
//print(content.toJsonString().joinWithSeparator("")) | |
} | |
//## ファイル追加関数: | |
func Fファイルに追加(ファイルパス:String, 追加テキスト:String, 追加モード:Bool = true){ | |
let os = OutputStream(toFileAtPath: ファイルパス, append: true)! | |
os.open() | |
os.write(追加テキスト, maxLength: 追加テキスト.lengthOfBytes(using: String.Encoding.utf8)) | |
os.close() | |
} | |
//# ファイル作成関数: (String, String) -> (String) | |
func Fファイルの作成(ファイルパス:String, 出力文字列:String) -> String { | |
do { | |
try 出力文字列.write(toFile: ファイルパス, atomically: false, encoding: String.Encoding.utf8 ) | |
return "成功" | |
} catch let error as NSError { | |
return error.localizedDescription | |
} | |
} | |
// MyJson.txt に JSON形式で出力する | |
Fファイルの作成(ファイルパス: "MyJson.txt", 出力文字列: "[\n") | |
for ix in 0 ..< contents.count { | |
for line in contents[ix].toJsonString() { | |
Fファイルに追加(ファイルパス: "MyJson.txt", 追加テキスト: line) | |
} | |
if ix < contents.count - 1 { | |
Fファイルに追加(ファイルパス: "MyJson.txt", 追加テキスト: ",\n") | |
} | |
} | |
Fファイルに追加(ファイルパス: "MyJson.txt", 追加テキスト: "]") | |
//##### ファイルから JSONデータを読む ##### | |
//# ファイルリード関数: (String) -> (String) | |
func Fファイルリード(ファイルパス:String) -> (String) { | |
do { | |
let l入力内容 = try NSString( contentsOfFile: ファイルパス, encoding: String.Encoding.utf8.rawValue ) | |
return l入力内容 as String | |
} catch let error as NSError { | |
return(error.localizedDescription) | |
} | |
} | |
// <== /MyJson.txt | |
let l読み込んだ文字列 = Fファイルリード(ファイルパス: "MyJson.txt") | |
print("MyJson.txt の読み込み: \(l読み込んだ文字列)") | |
let fileHandle : FileHandle = FileHandle(forReadingAtPath: "MyJson.txt")! | |
let data: Data? = fileHandle.readDataToEndOfFile() | |
if data != nil { | |
var jsonArray: NSArray = try JSONSerialization.jsonObject( | |
with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray | |
//print(jsonArray.firstObject!) | |
var jsonElem: NSDictionary = jsonArray.lastObject as! NSDictionary | |
print("fPath = \(jsonElem["fPath"]!)") | |
//結果:fPath = /_contents/picture/movie_ticket/ある夜の出来事.jpg | |
print("title = \(jsonElem["title"]!)") | |
//結果:title = ある夜の出来事 | |
} | |
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
//##### ファイルから JSONデータを読む ##### | |
//# ファイルリード関数: (String) -> (String) | |
func Fファイルリード(ファイルパス:String) -> (String) { | |
do { | |
let l入力内容 = try NSString( contentsOfFile: ファイルパス, encoding: NSUTF8StringEncoding ) | |
return l入力内容 as String | |
} catch let error as NSError { | |
return(error.localizedDescription) | |
} | |
} | |
// <== /MyJson.txt | |
let l読み込んだ文字列 = Fファイルリード("MyJson.txt") | |
print("MyJson.txt の読み込み: \(l読み込んだ文字列)") | |
let json = JSON.parse(l読み込んだ文字列) | |
for elem in json.asArray! { | |
if elem["cType"].asString == "Music" { | |
print(elem["fPath"]) | |
} | |
} | |
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
/_contents/video/Music/jazz/artist1-title1.mp4 | |
/_contents/video/Music/jazz/artist1-[2013]title2.mp4 | |
/_contents/video/Music/jazz/artist1-[2014]title3.mp4 | |
/_contents/video/Music/jazz/artist1-[2000]title4(1).mp4 | |
/_contents/video/Music/jazz/artist1-[2000]title4(2).mp4 | |
/_contents/video/Music/jazz/artist1-[2000][email protected] | |
/_contents/video/Music/Taylor/Taylor Swift - Last Christmas @Xmas.mp4 | |
/_contents/video/Music/Taylor/Taylor Swift - Fearless @Live [2010].mp4 | |
/_contents/video/Music/Avril/Avril-I Can Do Better @On Stage.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment