Created
December 4, 2015 19:28
-
-
Save thomaspockrandt/6febc9ae8ee95c82f8f8 to your computer and use it in GitHub Desktop.
Simple Swift M3U Parser
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
if | |
let path = NSBundle.mainBundle().pathForResource("tvhd", ofType: "m3u"), | |
let content = try? String(contentsOfFile: path, encoding: NSUTF8StringEncoding) | |
{ | |
let lines = content.componentsSeparatedByCharactersInSet(.newlineCharacterSet()) | |
var streams = [String: NSURL]() | |
var streamName: String? | |
for line in lines { | |
if line.containsString("#") { | |
if let match = line.rangeOfString("^#EXTINF:-1,", options: .RegularExpressionSearch) { | |
streamName = line.substringWithRange(Range<String.Index>(start: match.endIndex, end: line.endIndex)) | |
} | |
} else if !line.isEmpty { | |
if | |
let _streamName = streamName, | |
let url = NSURL(string: line) | |
{ | |
streams[_streamName] = url | |
} | |
} | |
} | |
return streams | |
} else { | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment