作者:sh4
项目:mediabas
func (self *Pruner) Start() {
mlog.Info("starting Pruner service ...")
go self.react()
mlog.Info("Pruner service started")
}
作者:sh4
项目:mediabas
func (self *Core) Start() {
mlog.Info("starting core service ...")
events := []fsm.EventDesc{
{Name: "import", Src: []string{"idle", "scanning"}, Dst: "scanning"},
{Name: "found", Src: []string{"scanning"}, Dst: "scanning"},
{Name: "scraped", Src: []string{"scanning"}, Dst: "scanning"},
{Name: "status", Src: []string{"idle", "scanning"}, Dst: "scanning"},
{Name: "finish", Src: []string{"scanning"}, Dst: "idle"},
}
// some initialization
self.fsm = fsm.NewFSM(
"idle",
events,
fsm.Callbacks{
"import": self.importer,
"found": self.found,
"scraped": self.scraped,
"finish": self.finish,
},
)
self.context = message.Context{Message: "Idle", Backdrop: "/mAwd34SAC8KqBKRm2MwHPLhLDU5.jpg", Completed: false}
go self.react()
mlog.Info("core service started")
}
作者:sh4
项目:mediabas
func (self *Scanner) Start() {
mlog.Info("starting scanner service ...")
mlog.Info("compiling regular expressions ...")
// test:="I am leaving from home in a while"
// prepositionsRegex := make([]*regexp.Regexp, len(preps))
// for i := 0; i < len(preps); i++ {
// prepositionsRegex[i]=regexp.MustCompile(`\b`+preps[i]+`\b`)
// }
// for i := 0; i < len(prepositionsRegex); i++ {
// fmt.Println(prepositionsRegex[i].String())
// if loc := prepositionsRegex[i].FindStringIndex(test); loc != nil{
// fmt.Println(test[loc[0]:loc[1]], "found at: ", loc[0])
// break
self.re = make([]*helper.Rexp, 0)
for _, regex := range self.Config.MediaRegexs {
self.re = append(self.re, &helper.Rexp{Exp: regexp.MustCompile(regex)})
}
// self.re[0] = &helper.Rexp{Exp: regexp.MustCompile(`(?i)/volumes/.*?/(?P<Resolution>.*?)/(?P<Name>.*?)\s\((?P<Year>\d\d\d\d)\)/(?:.*/)*bdmv/index.(?P<FileType>bdmv)$`)}
// self.re[1] = &helper.Rexp{Exp: regexp.MustCompile(`(?i)/volumes/.*?/(?P<Resolution>.*?)/(?P<Name>.*?)\s\((?P<Year>\d\d\d\d)\)/(?:.*/)*.*\.(?P<FileType>iso|img|nrg|mkv|avi|xvid|ts|mpg|dvr-ms|mdf|wmv)$`)}
// self.re[2] = &helper.Rexp{Exp: regexp.MustCompile(`(?i)/volumes/.*?/(?P<Resolution>.*?)/(?P<Name>.*?)\s\((?P<Year>\d\d\d\d)\)/(?:.*/)*(?:video_ts|hv000i01)\.(?P<FileType>ifo)$`)}
self.includedMask = ".bdmv|.iso|.img|.nrg|.mkv|.avi|.xvid|.ts|.mpg|.dvr-ms|.mdf|.wmv|.ifo"
go self.react()
mlog.Info("scanner service started")
}
作者:sh4
项目:mediabas
func (self *Cache) Start() {
mlog.Info("starting cache service ...")
self.workpool = workpool.New(4, 2000)
go self.react()
mlog.Info("cache service started")
}
作者:sh4
项目:mediabas
func (self *Core) doMovieRescraped(media *message.Media) {
go func() {
mlog.Info("UPDATING MOVIE [%s]", media.Movie.Title)
self.Bus.UpdateMovie <- media.Movie
}()
go func() {
mlog.Info("CACHING MEDIA [%s]", media.Movie.Title)
media.BasePath = self.Config.DataDir
self.Bus.CacheMedia <- media
}()
}
作者:sh4
项目:mediabas
func (self *Scanner) walker(folder string) error {
if folder[len(folder)-1] != '/' {
folder = folder + "/"
}
err := filepath.Walk(folder, func(path string, f os.FileInfo, err error) error {
if err != nil {
mlog.Info("from-start err: %s", err)
}
// mlog.Info("maldito: %s", path)
if !strings.Contains(self.includedMask, strings.ToLower(filepath.Ext(path))) {
// mlog.Info("[%s] excluding %s", filepath.Ext(path), path)
return nil
}
comparePath := strings.TrimPrefix(path, folder)
// mlog.Info("folder: %s, comparePath: %s", folder, comparePath)
// TODO: remove folder from path to match against rexp
for i := 0; i < 3; i++ {
// match := self.re[i].FindStringSubmatch(strings.ToLower(path))
// if match == nil {
// continue
// }
var rmap = self.re[i].Match(comparePath)
if rmap == nil {
continue
}
var resolution string
var ok bool
if resolution, ok = rmap["Resolution"]; !ok {
resolution = ""
}
movie := &message.Movie{Title: rmap["Name"], File_Title: rmap["Name"], Year: rmap["Year"], Resolution: resolution, FileType: rmap["FileType"], Location: path}
mlog.Info("FOUND [%s] (%s)", movie.Title, movie.Location)
self.Bus.MovieFound <- movie
return nil
}
return nil
})
return err
}
作者:sh4
项目:mediabas
func (self *Core) doMovieScraped(media *message.Media) {
go func() {
mlog.Info("STORING MOVIE [%s]", media.Movie.Title)
self.Bus.StoreMovie <- media.Movie
}()
go func() {
mlog.Info("CACHING MEDIA [%s]", media.Movie.Title)
media.BasePath = self.Config.DataDir
self.Bus.CacheMedia <- media
self.fsm.Event("scrape", media.Movie)
}()
}
作者:sh4
项目:mediabas
func (self *Config) Save() {
b, err := json.MarshalIndent(self, "", " ")
if err != nil {
mlog.Info("couldn't marshal: %s", err)
return
}
path := filepath.Join(self.DataDir, "config.json")
err = ioutil.WriteFile(path, b, 0644)
if err != nil {
mlog.Info("WriteFileJson ERROR: %+v", err)
}
mlog.Info("saved as: %s", string(b))
}
作者:sh4
项目:mediabas
func (self *Scanner) doScanMovies(reply chan string) {
mlog.Info("inside ScanMovies")
reply <- "Movie scannning process started ..."
for _, folder := range self.Config.MediaFolders {
err := self.walker(folder)
if err != nil {
mlog.Info("Unable to scan movies: %s", err)
}
mlog.Info("Completed scan of folder: %s", folder)
}
self.Bus.ImportMoviesFinished <- 1
}
作者:sh4
项目:mediabas
func (self *Config) Init(version string) {
self.Version = version
runtime := runtime.GOOS
switch runtime {
case "darwin":
self.DataDir = filepath.Join(os.Getenv("HOME"), ".mediabase/")
case "linux":
self.DataDir = filepath.Join(os.Getenv("HOME"), ".mediabase/")
case "windows":
self.DataDir = filepath.Join(os.Getenv("APPDATA"), "mediabase\\")
}
path := filepath.Join(self.DataDir, "log")
if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.Mkdir(path, 0755); err != nil {
log.Printf("FATAL: Unable to create folder %s: %s", path, err)
os.Exit(255)
}
}
// os.Setenv("GIN_MODE", "release")
mlog.Start(mlog.LevelInfo, filepath.Join(self.DataDir, "log", "mediabase.log"))
mlog.Info("mediabase v%s starting up on %s ...", self.Version, runtime)
self.setupOperatingEnv()
self.LoadConfig()
self.LoadRegex()
}
作者:sh4
项目:mediabas
func (self *Server) Start() {
mlog.Info("starting server service")
self.r = gin.New()
self.r.Use(gin.Recovery())
// self.r.Use(helper.Logging())
path := filepath.Join(".", "web")
var abs string
var err error
if abs, err = filepath.Abs(path); err != nil {
mlog.Info("unable to get absolute path: %s, ", err)
return
}
mlog.Info("server root path is: %s", abs)
// self.r.Use(static.Serve("./web/"))
self.r.Use(static.Serve(path))
self.r.NoRoute(self.redirect)
api := self.r.Group(apiVersion)
{
api.GET("/movies/cover", self.getCover)
api.POST("/movies", self.getMovies)
api.POST("/movies/search", self.searchMovies)
api.GET("/movies/duplicates", self.getDuplicates)
api.PUT("/movies/watched", self.watchedMovie)
api.POST("/movies/fix", self.fixMovie)
api.POST("/movies/prune", self.pruneMovies)
api.GET("/import", self.importMovies)
api.GET("/import/status", self.importMoviesStatus)
api.GET("/config", self.getConfig)
api.PUT("/config", self.saveConfig)
}
mlog.Info("service started listening on %s:%s", self.Config.Host, self.Config.Port)
go self.r.Run(fmt.Sprintf("%s:%s", self.Config.Host, self.Config.Port))
}
作者:sh4
项目:mediabas
func (self *Scraper) Start() {
mlog.Info("starting scraper service ...")
var err error
self.tmdb, err = tmdb.NewClient("e610ded10c3f47d05fe797961d90fea6", false)
if err != nil {
mlog.Fatalf("unable to create tmdb client: %s", err)
}
self.workpool = workpool.New(12, 4000)
go self.react()
// go self.workpool.Balance()
mlog.Info("scraper service started")
}
作者:sh4
项目:mediabas
func (self *Server) searchMovies(c *gin.Context) {
mlog.Info("searchMovies: are you a head honcho ?")
var options message.Options
c.Bind(&options)
mlog.Info("anyway the wind blows: %+v", options)
msg := message.Movies{Options: options, Reply: make(chan *message.MoviesDTO)}
self.Bus.SearchMovies <- &msg
reply := <-msg.Reply
// mlog.Info("%s", reply)
c.JSON(200, &reply)
}
作者:sh4
项目:mediabas
func (self *Dal) doGetMovies(msg *message.Movies) {
tx, err := self.db.Begin()
if err != nil {
mlog.Fatalf("unable to begin transaction: %s", err)
}
options := msg.Options
mlog.Info("what is: %+v", options)
stmt, err := tx.Prepare(fmt.Sprintf("select rowid, title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched, score, director, writer, actors, awards, imdb_rating, imdb_votes from movie order by %s %s limit ? offset ?", options.SortBy, options.SortOrder))
if err != nil {
mlog.Fatalf("unable to prepare transaction: %s", err)
}
defer stmt.Close()
rows, err := stmt.Query(options.Limit, options.Current)
if err != nil {
mlog.Fatalf("unable to prepare transaction: %s", self.err)
}
items := make([]*message.Movie, 0)
if self.count == 0 {
err = self.countRows.QueryRow().Scan(&self.count)
if err != nil {
mlog.Fatalf("unable to count rows: %s", err)
}
}
var count = 0
for rows.Next() {
movie := message.Movie{}
rows.Scan(&movie.Id, &movie.Title, &movie.Original_Title, &movie.File_Title, &movie.Year, &movie.Runtime, &movie.Tmdb_Id, &movie.Imdb_Id, &movie.Overview, &movie.Tagline, &movie.Resolution, &movie.FileType, &movie.Location, &movie.Cover, &movie.Backdrop, &movie.Genres, &movie.Vote_Average, &movie.Vote_Count, &movie.Production_Countries, &movie.Added, &movie.Modified, &movie.Last_Watched, &movie.All_Watched, &movie.Count_Watched, &movie.Score, &movie.Director, &movie.Writer, &movie.Actors, &movie.Awards, &movie.Imdb_Rating, &movie.Imdb_Votes)
items = append(items, &movie)
count++
}
rows.Close()
tx.Commit()
mlog.Info("Listed %d movies", count)
mlog.Info("Representing %d movies", self.count)
msg.Reply <- &message.MoviesDTO{Count: self.count, Movies: items}
}
作者:sh4
项目:mediabas
func (self *Server) importMovies(c *gin.Context) {
mlog.Info("importMovies: you know .. i got here")
msg := message.Status{Reply: make(chan *message.Context)}
self.Bus.ImportMovies <- &msg
reply := <-msg.Reply
c.JSON(200, &reply)
}
作者:sh4
项目:mediabas
func (self *Dal) Start() {
mlog.Info("starting dal service ...")
self.dbase = filepath.Join(".", "db", "mediabase.db")
self.db, self.err = sql.Open("sqlite3", self.dbase)
if self.err != nil {
mlog.Fatalf("open database: %s (%s)", self.err, self.dbase)
}
stmtExist := self.prepare(`select name from sqlite_master where type='table' and name='movie'`)
defer stmtExist.Close()
var name string
err := stmtExist.QueryRow().Scan(&name)
if err != nil {
mlog.Fatalf("unable to check for existence of movie database: %s (%s)", self.err, self.dbase)
}
if name != "movie" {
mlog.Info("Initializing database schema ...")
self.initSchema()
}
self.count = 0
self.searchCount = 0
self.searchArgs = ""
self.countRows = self.prepare("select count(*) from movie;")
self.listMovies = self.prepare("select rowid, title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched, score, director, writer, actors, awards, imdb_rating, imdb_votes from movie order by ? desc limit ? offset ?")
self.listByRuntime = self.prepare("select rowid, title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched, score, director, writer, actors, awards, imdb_rating, imdb_votes from movie order by runtime")
self.listMoviesToFix = self.prepare("select rowid, title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched, score, director, writer, actors, awards, imdb_rating, imdb_votes from movie where original_title = 'FIXMOV23'")
var abs string
if abs, err = filepath.Abs(self.dbase); err != nil {
mlog.Info("unable to get absolute path: %s, ", err)
return
}
mlog.Info("connected to database (%s)", abs)
// self.initSchema()
go self.react()
}
作者:sh4
项目:mediabas
func (self *Dal) Stop() {
self.listMoviesToFix.Close()
self.listByRuntime.Close()
self.listMovies.Close()
// self.searchMovies.Close()
// self.storeMovie.Close()
self.db.Close()
mlog.Info("dal service stopped")
}
作者:sh4
项目:mediabas
func (self *Cache) requestWork(media *message.Media) {
mlog.Info("CACHE MEDIA REQUESTED [%s]", media.Movie.Title)
gig := &CacheGig{
media,
self.Config.DataDir,
}
self.workpool.PostWork("cachegig", gig)
}
作者:sh4
项目:mediabas
func (self *Dal) doWatchedMovie(msg *message.SingleMovie) {
mlog.Info("STARTED UPDATING WATCHED MOVIE %s (%s)", msg.Movie.Title, msg.Movie.Last_Watched)
tx, err := self.db.Begin()
if err != nil {
mlog.Fatalf("at begin: %s", err)
}
stmt, err := tx.Prepare("update movie set last_watched = ?, all_watched = ?, count_watched = ?, score = ?, modified = ? where rowid = ?")
if err != nil {
tx.Rollback()
mlog.Fatalf("at prepare: %s", err)
}
defer stmt.Close()
now := time.Now().UTC().Format(time.RFC3339)
var all_watched string
count_watched := msg.Movie.Count_Watched
if !strings.Contains(msg.Movie.All_Watched, msg.Movie.Last_Watched) {
count_watched++
if msg.Movie.All_Watched == "" {
all_watched = msg.Movie.Last_Watched
} else {
all_watched += "|" + msg.Movie.Last_Watched
}
}
_, err = stmt.Exec(msg.Movie.Last_Watched, all_watched, count_watched, msg.Movie.Score, now, msg.Movie.Id)
if err != nil {
tx.Rollback()
mlog.Fatalf("at exec: %s", err)
}
tx.Commit()
mlog.Info("FINISHED UPDATING WATCHED MOVIE %s", msg.Movie.Title)
msg.Movie.All_Watched = all_watched
msg.Movie.Count_Watched = count_watched
msg.Movie.Modified = now
msg.Reply <- msg.Movie
}
作者:sh4
项目:mediabas
func (self *Dal) doStoreMovie(movie *message.Movie) {
self.count = 0
mlog.Info("STARTED SAVING %s [%d]", movie.Title)
tx, err := self.db.Begin()
if err != nil {
mlog.Fatalf("at begin: %s", err)
}
// stmt, err := tx.Prepare("insert into movie(title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, director, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
stmt, err := tx.Prepare("insert into movie(title, original_title, file_title, year, runtime, tmdb_id, imdb_id, overview, tagline, resolution, filetype, location, cover, backdrop, genres, vote_average, vote_count, countries, added, modified, last_watched, all_watched, count_watched, score, director, writer, actors, awards, imdb_rating, imdb_votes) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
tx.Rollback()
mlog.Fatalf("at prepare: %s", err)
}
defer stmt.Close()
_, err = stmt.Exec(movie.Title, movie.Original_Title, movie.File_Title, movie.Year, movie.Runtime, movie.Tmdb_Id, movie.Imdb_Id, movie.Overview, movie.Tagline, movie.Resolution, movie.FileType, movie.Location, movie.Cover, movie.Backdrop,
movie.Genres, movie.Vote_Average, movie.Vote_Count, movie.Production_Countries, movie.Added, movie.Modified, movie.Last_Watched, movie.All_Watched, movie.Count_Watched, movie.Score, movie.Director, movie.Writer, movie.Actors, movie.Awards,
movie.Imdb_Rating, movie.Imdb_Votes)
if err != nil {
tx.Rollback()
mlog.Fatalf("at exec: %s", err)
}
// mlog.Info("Movie is %v", movie)
// _, self.err = self.storeMovie.Exec(movie.Title, movie.Year, movie.Resolution, movie.FileType, movie.Location)
// if self.err != nil {
// mlog.Fatalf("at storemovie: %s", self.err)
// }
tx.Commit()
mlog.Info("FINISHED SAVING %s", movie.Title)
// _, self.err = self.storeMovie.Exec(movie.Name, movie.Year, movie.Resolution, movie.Type, movie.Path, movie.Picture)
// if self.err != nil {
// mlog.Fatalf(self.err)
// }
}