Created
September 21, 2018 02:14
-
-
Save fromkk/dc454051df2f793600920c9fbc5a98c8 to your computer and use it in GitHub Desktop.
Print session list for tweet of #iOSDCRC
This file contains 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
import Foundation | |
extension Date { | |
func toString(with dateFormat: String) -> String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = Locale(identifier: "ja_JP") | |
dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo") | |
dateFormatter.dateFormat = dateFormat | |
return dateFormatter.string(from: self) | |
} | |
} | |
struct Event: Codable, Equatable { | |
let title: String | |
let description: String | |
let startAt: Date | |
let endAt: Date | |
let tracks: [Track] | |
private enum CodingKeys: String, CodingKey { | |
case title | |
case description | |
case startAt = "start_at" | |
case endAt = "end_at" | |
case tracks | |
} | |
struct Track: Codable, Equatable { | |
let name: String | |
let hashtag: String | |
let sessions: [Session] | |
private enum CodingKeys: String, CodingKey { | |
case name | |
case hashtag | |
case sessions | |
} | |
struct Session: Codable, Equatable { | |
let title: String | |
let description: String | |
let author: String? | |
let url: URL? | |
let startAt: Date | |
let endAt: Date | |
private enum CodingKeys: String, CodingKey { | |
case title | |
case description | |
case author | |
case url | |
case startAt = "start_at" | |
case endAt = "end_at" | |
} | |
} | |
} | |
} | |
let bundle = Bundle.main | |
guard let path = bundle.path(forResource: "iosdcrc", ofType: "json") else { | |
fatalError("path not found") | |
} | |
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else { | |
fatalError("data get failed") | |
} | |
do { | |
let jsonDecoder = JSONDecoder() | |
jsonDecoder.dateDecodingStrategy = .iso8601 | |
let events = try jsonDecoder.decode([Event].self, from: data) | |
events.forEach { (event) in | |
print(event.description) | |
event.tracks.forEach({ (track) in | |
print(track.name) | |
track.sessions.forEach({ (session) in | |
var components: [String] = [session.title.replacingOccurrences(of: "\n", with: " ")] | |
if let author = session.author { | |
components.append(author) | |
} | |
components.append(track.hashtag) | |
print("Next: \(session.startAt.toString(with: "HH:mm~")) [\(track.name)] \(components.joined(separator: " "))\n") | |
}) | |
}) | |
} | |
} catch { | |
fatalError("timetable get failed \(error)") | |
} |
Author
fromkk
commented
Sep 21, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment