Skip to content

Instantly share code, notes, and snippets.

@tnaka
Last active August 19, 2017 15:09
Show Gist options
  • Save tnaka/8157138 to your computer and use it in GitHub Desktop.
Save tnaka/8157138 to your computer and use it in GitHub Desktop.
しょぼいカレンダーから指定されたtidのサブタイリストを取得し整形して表示する
package main
import "fmt"
import "sort"
import "io/ioutil"
import "net/http"
import "encoding/json"
import "strconv"
import "flag"
import "strings"
import "os"
func perror(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}
func main() {
var tidflag = flag.String("tid", "1", "TID to get subtitles")
var storyflag = flag.Int("story", -1, "story number to get a subtitle")
flag.Parse()
tid := *tidflag
story := *storyflag
url := "http://cal.syoboi.jp/json.php?Req=TitleMedium,SubTitles&TID=" + tid
//fmt.Println(url)
res, err := http.Get(url)
perror(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
perror(err)
//fmt.Printf("%s\n", body)
var f interface{}
err = json.Unmarshal(body, &f)
perror(err)
//printJsonInterface(f)
title := getInterfaceByKeyFromJsonInterface(f, "Title")
//fmt.Println(title)
if _, ok := title.(string); !ok {
fmt.Fprintln(os.Stderr, "error: title not found")
os.Exit(-1)
}
tmp := getInterfaceByKeyFromJsonInterface(f, "SubTitles")
subtitles, ok := getInterfaceByKeyFromJsonInterface(tmp, tid).(map[string]interface{})
if !ok {
fmt.Println(escapeForFilename(title.(string)))
if story >= 0 {
fmt.Fprintf(os.Stderr, "error: subtitles for '%s' not found while -story is specified\n", title)
os.Exit(-1)
}
fmt.Fprintf(os.Stderr, "warning: subtitles for '%s' not found\n", title)
os.Exit(0)
}
var titles []string
for k, v := range subtitles {
i, _ := strconv.Atoi(k)
if story >= 0 && story != i {
continue
}
if i < 10 {
titles = append(titles, fmt.Sprintf("%s 第0%s話 「%s」", title, k, v))
} else {
titles = append(titles, fmt.Sprintf("%s 第%s話 「%s」", title, k, v))
}
}
sort.Strings(titles)
for _, v := range titles {
fmt.Println(escapeForFilename(v))
}
}
func escapeForFilename(s string) string {
s = strings.Replace(s, "!", "!", -1)
s = strings.Replace(s, "?", "?", -1)
s = strings.Replace(s, "〜", "~", -1)
s = strings.Replace(s, "*", "*", -1)
s = strings.Replace(s, "<", "<", -1)
s = strings.Replace(s, ">", ">", -1)
s = strings.Replace(s, "%", "%", -1)
s = strings.Replace(s, "'", "’", -1)
s = strings.Replace(s, "/", "/", -1)
s = strings.Replace(s, "`", "‘", -1)
s = strings.Replace(s, ":", ":", -1)
s = strings.Replace(s, "+", "+", -1)
s = strings.Replace(s, "=", "=", -1)
s = strings.Replace(s, "[", "[", -1)
s = strings.Replace(s, "]", "]", -1)
s = strings.Replace(s, "#", "#", -1)
s = strings.Replace(s, ";", ";", -1)
s = strings.Replace(s, "&", "&", -1)
s = strings.Replace(s, "―", "-", -1)
return s
}
func getInterfaceByKeyFromJsonInterface(data interface{}, key string) interface{} {
switch data.(type) {
case map[string]interface{}:
for k, v := range data.(map[string]interface{}) {
if k == key {
return v
}
i := getInterfaceByKeyFromJsonInterface(v, key)
if i != nil {
return i
}
}
case []interface{}:
for _, v := range data.([]interface{}) {
i := getInterfaceByKeyFromJsonInterface(v, key)
if i != nil {
return i
}
}
default:
}
return nil
}
func printJsonInterface(data interface{}) {
printJsonInterfaceIndent(data, 0)
}
func printJsonInterfaceIndent(data interface{}, indent int) {
for i := 0; i < indent; i++ {
fmt.Print("\t")
}
switch data.(type) {
case string:
fmt.Printf("\"%s\"", data.(string))
case float64:
fmt.Print(data.(float64))
case bool:
fmt.Print(data.(bool))
case nil:
fmt.Print("null")
case []interface{}:
fmt.Print("[\n")
for _, v := range data.([]interface{}) {
printJsonInterfaceIndent(v, indent+1)
}
for i := 0; i < indent; i++ {
fmt.Print("\t")
}
fmt.Print("]")
case map[string]interface{}:
fmt.Print("{\n")
for k, v := range data.(map[string]interface{}) {
for i := 0; i < indent; i++ {
fmt.Print("\t")
}
fmt.Print(k + ":\n")
printJsonInterfaceIndent(v, indent+1)
}
for i := 0; i < indent; i++ {
fmt.Print("\t")
}
fmt.Print("}")
default:
}
fmt.Print("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment