Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created May 30, 2020 04:36
Show Gist options
  • Select an option

  • Save mhewedy/964f6ba3075bd4b4f3f308f481f8787d to your computer and use it in GitHub Desktop.

Select an option

Save mhewedy/964f6ba3075bd4b4f3f308f481f8787d to your computer and use it in GitHub Desktop.
group ip tv
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
)
var ignorePattern, _ = regexp.Compile("S[0-9]{2} E[0-9]{2}")
var m = make(map[string][]entry)
type entry struct {
title, link string
}
func main() {
// accept input from #EXTINF:-1,Unda (2019) (the end of UK group)
f, _ := os.Open("/users/mhewedy/file.m3u")
defer f.Close()
b, _ := ioutil.ReadAll(f)
content := strings.Split(string(b), "\n")
for i := 0; i < len(content); i += 2 {
title := content[i]
link := content[i+1]
key := ignorePattern.ReplaceAllString(title, "")
key = strings.ReplaceAll(key, "#EXTINF:-1,", "")
entries, ok := m[key]
if ok {
entries = append(entries, entry{title: title, link: link})
m[key] = entries
} else {
m[key] = []entry{{title: title, link: link}}
}
}
// sort keys
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return len(m[keys[i]]) < len(m[keys[j]])
})
output := "#EXTGRP: Movies\n"
for _, k := range keys {
v := m[k]
if len(v) > 1 { // only group TV series
output += fmt.Sprintf("#EXTGRP:%s\n", k)
}
for _, vv := range v {
output += fmt.Sprintln(vv.title)
output += fmt.Sprintln(vv.link)
}
}
//printStatistics()
_ = ioutil.WriteFile("/users/mhewedy/out.txt", []byte(output), 0755)
}
func printStatistics() {
var w, wo int
for _, v := range m {
if len(v) > 1 {
w++
} else {
wo++
}
}
fmt.Println("TV series", w)
fmt.Println("movies", wo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment