Esempio n. 1
0
func generateYACrawlerKeywords(appCtx *lib.AppContext) ([]*yauction.CrawlerKeyword, error) {
	var showList []event.Show
	var logger = appCtx.Logger
	q := NewShowDriver(appCtx).NewQuery()
	q = q.Filter("StartAt >=", util.Date(now())).Order("StartAt")
	_, err := q.GetAll(&showList)
	if err != nil {
		return nil, err
	}
	logger.Debug("Found %d EventShow entities to be checked.", len(showList))

	keywordDriver := yauctionapp.NewCrawlerKeywordDriver(appCtx)
	crawlerKeywrodKeys := make([]*ds.Key, 0)
	for _, show := range showList {
		logger.Debug("Show: %q", show.Id)
		if show.YAKeyword == "" {
			logger.Debug("EventShow %q doesn't have YAKeyword, skipped.", show.Id)
			continue
		}
		crawlerKeywrodKeys = append(
			crawlerKeywrodKeys,
			keywordDriver.NewKey(show.YAKeyword, 0, nil),
		)
	}

	crawlerKeywords := make([]*yauction.CrawlerKeyword, len(crawlerKeywrodKeys))
	err = keywordDriver.GetMulti(crawlerKeywrodKeys, crawlerKeywords)
	if err != nil && datastore.IsDatastoreError(err) {
		return nil, err
	}

	updateKeys := make([]*ds.Key, 0)
	updates := make([]*yauction.CrawlerKeyword, 0)
	for i, ck := range crawlerKeywords {
		if ck == nil {
			logger.Debug(
				"No CrawlerKeyword entity is registered for EventShow %q, creating.",
				showList[i].Id,
			)
			keyword := yauctionapp.NormalizeKeyword(showList[i].YAKeyword)
			updateKeys = append(updateKeys, keywordDriver.NewKey(keyword, 0, nil))
			updates = append(updates,
				&yauction.CrawlerKeyword{
					Keyword:   keyword,
					StartAt:   showList[i].OpenAt.AddDate(0, -3, 0), // start monitoring 3 months before
					EndAt:     showList[i].OpenAt,
					CreatedAt: now(),
				},
			)
		} else {
			if ck.StartAt != showList[i].OpenAt.AddDate(0, -3, 0) ||
				ck.EndAt != showList[i].OpenAt {
				logger.Debug(
					"CrawlerKeyword for %q already exisits and needs updating.",
					showList[i].Id,
				)
				ck.StartAt = showList[i].OpenAt.AddDate(0, -3, 0)
				ck.EndAt = showList[i].OpenAt
				updateKeys = append(updateKeys, keywordDriver.NewKey(ck.Keyword, 0, nil))
				updates = append(updates, ck)
			} else {
				logger.Debug(
					"CrawlerKeyword for %q already exisits but no need to update, skipped.",
					showList[i].Id,
				)
			}
		}
	}
	_, err = keywordDriver.PutMulti(updateKeys, updates)
	return updates, err
}
Esempio n. 2
0
func setupEventPages(app *App) {
	app.Page.Navi("/events/", "イベント").
		Templates("./artistapp/events.html", "./artistapp/show_part.html").
		AssetPath("/artistapp/events.js").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				var appCtx = lib.NewAppContextFromRequest(req)
				var all = req.Query("all") == "1"
				q := NewShowDriver(appCtx).NewQuery()
				if all {
					q = q.Order("-StartAt")
				} else {
					q = q.Filter("StartAt >=", util.Date(now())).Order("StartAt")
				}
				if result, err := queryShows(res, req, q); err != nil {
					lib.Error(res, req, err)
					return
				} else {
					p.Ogp("type", "blog")
					res.SetLocal("Shows", result.Shows)
					if all {
						res.SetLocal("Future", result.Current)
						res.SetLocal("Next", fmt.Sprintf("%s&all=1", result.Next))
						res.SetLocal("Previous", fmt.Sprintf("%s&all=1", result.Previous))
					} else {
						res.SetLocal("All", fmt.Sprintf("%s&all=1", result.Current))
						res.SetLocal("Next", result.Next)
						res.SetLocal("Previous", result.Previous)
					}
				}
			})

	app.Page.Page("/events/:name.html", "イベント").
		Templates("./artistapp/events.html").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				//d := NewShowDriver(app.Key, gae.NewContext(req), req.Logger)
			})

	app.Page.Navi("/admin/events.html", "イベント管理").
		Templates("./artistapp/admin/events.html").
		AssetPath("/artistapp/admin/events.js").
		RequireAuth(lib.Admin)

	app.Page.Page("/admin/events/:id.html", "").
		Templates("./artistapp/admin/events.html").
		AssetPath("/artistapp/admin/shows.js").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				var e event.Event
				d := NewEventDriver(lib.NewAppContextFromRequest(req))
				key := d.NewKey(req.Param("id"), 0, nil)
				if err := d.Get(key, &e); err != nil {
					lib.Error(res, req, err)
					return
				}
				p.Title = fmt.Sprintf("公演一覧 - %s", e.Title)
			})

	app.Page.Page("/venues/:id/", "").
		AssetPath("/artistapp/venues.js").
		Templates("./artistapp/venues.html").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				var appCtx = lib.NewAppContextFromRequest(req)
				var showList []event.Show
				q := NewShowDriver(appCtx).NewQuery()
				q = q.Filter("VenueId =", req.Param("id"))
				if _, err := q.Limit(1).GetAll(&showList); err != nil {
					lib.Error(res, req, err)
					return
				}
				if len(showList) == 0 {
					app.Page.NotFound(res, req)
					return
				}
				if list, err := NewEventShowList(appCtx, showList); err != nil {
					lib.Error(res, req, err)
					return
				} else {
					ev := list[0]
					p.Title = fmt.Sprintf("会場情報 - %s", ev.VenueName)
					res.SetLocal("VenueName", ev.VenueName)
					res.SetLocal("Latitude", ev.Latitude)
					res.SetLocal("Longitude", ev.Longitude)
				}
			})

}