Exemplo n.º 1
0
func getIEpgListFromCrawlerConfig(res *wcg.Response, req *wcg.Request, cfg *tv.CrawlerConfig, channels []*tv.TvChannel) ([]*tv.IEpg, error) {
	iepglist := make([]*tv.IEpg, 0)
	crawler := tv.NewCrawler(gae.NewHttpClient(req))
	if metalist, err := crawler.GetIEpgList(cfg.Keyword, cfg.Scope); err != nil {
		req.Logger.Warn("Could not get IEPG list with %q (scope: %d): %v", cfg.Keyword, cfg.Scope, err)
		return nil, err
	} else {
		req.Logger.Info("Found %d IEPG entries in %q (scope: %d)", len(metalist), cfg.Keyword, cfg.Scope)
		for j := range metalist {
			meta := metalist[j]
			iepg, err1 := crawler.GetIEpg(meta)
			iepg.Category = cfg.Category
			ch := findChannelByStationId(iepg.StationId, channels)
			if ch == nil {
				req.Logger.Warn("Could not find the channel %q(%s) in the current channel list. (Program Title: %s, Id: %s)", iepg.StationId, iepg.StationName, iepg.ProgramTitle, iepg.Id)
				continue
			}
			req.Logger.Debug("Resolved Sid/Cid for %s (Program Title: %s)", iepg.ProgramTitle, iepg.Id)
			iepg.Sid = ch.Sid
			iepg.Cid = ch.Cid
			if err1 != nil {
				req.Logger.Warn("Could not get IEPG data with %q", iepg.Id)
			} else {
				req.Logger.Debug("Adding %s into iepg update list.", iepg.Id)
				iepglist = append(iepglist, iepg)
			}
		}
		return iepglist, nil
	}
}
Exemplo n.º 2
0
func registerJsonpProxyHandlers(routes *wcg.Router) {
	routes.Get("/jsonp", func(res *wcg.Response, req *wcg.Request) {
		target := req.Query("_jsonpurl")
		if target == "" {
			res.WriteJsonWithStatus(400, nil, map[string]interface{}{
				"error": "_jsonpurl must be specified.",
			})
			return
		}
		if _url, err := url.Parse(target); err != nil {
			res.WriteJsonWithStatus(400, nil, map[string]interface{}{
				"error": "_jsonpurl cannot be parsed.",
			})
			return
		} else {
			if _url.Scheme != "http" && _url.Scheme != "https" {
				res.WriteJsonWithStatus(400, nil, map[string]interface{}{
					"error": "non http(s) scheme is allowed.",
				})
				return
			}
			if _, ok := JsonpProxyWhiteList[_url.Host]; !ok {
				res.WriteJsonWithStatus(400, nil, map[string]interface{}{
					"error": fmt.Sprintf("%s is not allowed to proxy.", _url.Host),
				})
				return
			}
		}
		query := req.HttpRequest().URL.Query()
		query.Del("_jsonpurl")
		client := gae.NewHttpClient(req)
		resp, err := client.Get(fmt.Sprintf("%s?%s", target, query.Encode()))
		if resp == nil {
			Error(res, req, err)
			return
		}
		defer resp.Body.Close()
		res.WriteHeader(resp.StatusCode)
		io.Copy(res, resp.Body)
	})
}
Exemplo n.º 3
0
func updatePostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPostForUpdate(req, res, blog)
	if err != nil {
		return
	}
	post.Title = req.Form("title")
	post.Content = req.Form("content")
	post.IsDraft = req.Form("is_draft") == "true"
	post.IsNew = false
	post.PostDate, err = time.Parse(wcg.FormDateFormat, req.Form("post_date"))
	post.Tags = strings.Split(req.Form("tags"), ",")
	for i, v := range post.Tags {
		post.Tags[i] = strings.TrimSpace(v)
	}

	if err != nil {
		res.WriteHeader(400)
		res.WriteString("Invalid date format.")
		res.End()
	}
	driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
	if AppConfig.GithubMarkdown {
		driver.HttpClient = gae.NewHttpClient(req)
	}
	err = driver.SavePost(post)
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}

	req.Logger.Info("A post is created at %s.", post.Id)
	res.WriteJsonWithStatus(200, nil, ok)
}