Created
September 9, 2019 08:55
-
-
Save elarib/b2cc6dafba5e73778e64ccba4c7ea71e to your computer and use it in GitHub Desktop.
Json to M3U (Specific case)
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 org.json4s.DefaultFormats | |
import org.json4s.JsonAST.JValue | |
import org.json4s.jackson.JsonMethods._ | |
case class Channel(name: String, ch: String) | |
case class Category(id: String) | |
object JsonM3uTransformer extends App { | |
implicit val formats = DefaultFormats | |
val packListUrl = "xxx" | |
def getChannelListFromPackUrl(id: String) = "yyy" | |
def writeToFile(content: String) = { | |
import java.io._ | |
val pw = new PrintWriter(new File("/tmp/test.m3u")) | |
pw.write(content) | |
pw.close | |
} | |
def generateM3UFromPack(list: List[Channel]): String = { | |
list match { | |
case Nil => "" | |
case ::(head, tl) => | |
(s"""#EXTINF:-1,##===== ${head.name} =====## | |
|${head.ch}""".stripMargin :: tl.map { channel => | |
s"""#EXTINF:-1,${channel.name} | |
|${channel.ch} | |
""".stripMargin | |
}).mkString("\n") | |
} | |
} | |
def get(url: String) = scala.io.Source.fromURL(url).mkString | |
def getPackList = { | |
val content = parse(get(packListUrl), false) | |
(content \ "bouquets").extract[List[JValue]].map { content => | |
content.extract[Category] | |
} | |
} | |
def getChannelListFromPack(id: String) = { | |
val content = parse(get(getChannelListFromPackUrl(id)), false) | |
(content \ "channels") | |
.extract[List[JValue]] | |
.map { content => | |
content.extract[Channel] | |
} | |
.filter { channel => | |
Option(channel.name).isDefined | |
} | |
} | |
// val result = getPackList | |
val result = | |
("#EXTM3U" :: (getPackList ::).map(cat => generateM3UFromPack(getChannelListFromPack(cat.id)))) | |
.mkString("\n") | |
writeToFile(result) | |
println(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Elarib - Need your help in using this above script. json to m3u.
I have a json file which needs to be converted to m3u file. Can you help me and tell me to how do it. If possible if you have a workflow please assist.
https://gist.githubusercontent.com/naveenland4/414fde03d11bc824bc764984bc3067a6/raw/ch.json
Thanks
Naveen