MediaListModel.go 2.93 KB
Newer Older
吴贤德's avatar
吴贤德 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package Wasu

import (
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"src/Common/Request"
	"src/Config"
	"unsafe"
)

/**
	{
	"currentPage":1,
	"pageNum":1,
	"pageSize":2,
	"total":2,
	"list":[
	{
	"jsonUrl":"http://115.29.7.64:8092/XmlData/incrementJson?date=20190625&version=20190625150313",
	"md5":"a3e0d11328e0feb3114921851f39ac41",
	"newsId":"2018239",
	"newsName":"冰雪女王",
	"playUrl":"http://downloader.media.wasu.tv/data13/ott/344/2015-08/10/adc0650a31a5211ec6d0b1c9fdb10274/5d1496be/1439187357125_958916.ts"
	}
	]
	}
**/

type Media struct {
31 32
	Spid string `json:"spid"`
	PushMode string `json:"push_mode"` //是否自动转推:0手动,1自动
吴贤德's avatar
吴贤德 committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	JsonUrl string `json:"jsonUrl"`
	//Md5			string 	`json:"md5"`
	//NewsId		string	`json:"newsId"`
	//NewsName	string	`json:"newsName"`
	//PlayUrl		string	`json:"playUrl"`
}

type MediaList struct {
	CurrentPage int   `json:"currentPage"`
	PageNum     int   `json:"pageNum"`
	PageSize    int   `json:"pageSize"`
	Total       int   `json:"total"`
	List        []*Media `json:"list"`
}

type MediaListModel struct {
	Config *Config.Config
50
	Spid string
吴贤德's avatar
吴贤德 committed
51 52 53
}

func (this *MediaListModel) FetchList(date string, page int) *MediaList {
54 55 56 57 58 59 60 61 62 63 64 65 66 67
	//wasu_api_incrementList, ok := this.Config.Sys.Get("wasu_api_incrementList")
	wasu_api_incrementList := ""
	config,ok := this.Config.Sps.Get(this.Spid)
	if ok {
		conf,ok:=config.(map[string]interface{})
		if ok {
			wasu_api_incrementList=conf["wasu_api_incrementList"].(string)
		}
	}

	if wasu_api_incrementList == "" {
		logrus.Error("PULL SP :",this.Spid," 缺少配置项:wasu_api_incrementList")
	}

吴贤德's avatar
吴贤德 committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	mediaList := new(MediaList)
	if ok {
		url := fmt.Sprintf("%s?date=%s&page=%d", wasu_api_incrementList, date, page)

		data, err := Request.Get(url)
		if err == nil {
			json.Unmarshal([]byte(data), mediaList)
		}
	}
	return mediaList
}

func (this *MediaListModel) UpdateMediaList(mediaList *MediaList) bool {
	l := len(mediaList.List)
	nOk := 0
	for _, media := range mediaList.List {
		if this.UpdateMedia(media) {
			nOk++
		}
	}
	b := false
	if nOk == l {
		b = true
	}
	logrus.Debugln("UpdateMediaList : ok/total :", nOk, "/", l)
	return b
}

func (this *MediaListModel) UpdateMedia(media *Media) bool {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	media.Spid = this.Spid
	wasu_incrementList_push_mode:="";
	config,ok := this.Config.Sps.Get(this.Spid)
	if ok {
		conf,ok:=config.(map[string]interface{})
		if ok {
			wasu_incrementList_push_mode=conf["wasu_incrementList_push_mode"].(string)
		}
	}

	if wasu_incrementList_push_mode == "" {
		logrus.Error("PULL SP :",this.Spid," 缺少配置项:wasu_incrementList_push_mode")
		wasu_incrementList_push_mode="0"
	}

	media.PushMode=wasu_incrementList_push_mode;


吴贤德's avatar
吴贤德 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128
	center_api_media_update, ok := this.Config.Sys.Get("center_api_media_update")
	b := false
	if ok {
		inData, _ := json.Marshal(media)
		data, err := Request.Post(center_api_media_update.(string), *(*string)(unsafe.Pointer(&inData)))
		if err == nil {
			if data == "true" {
				b = true
			}
		}
	}

	return b
}