library

package
v0.0.0-...-288be38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2019 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CatalogueLibraryCmd = &cobra.Command{
	Use:   "catalogue",
	Short: "To Show Plex And Library Catalogue",
	Long:  "to show plex and library catalogue.",
	Run: func(cmd *cobra.Command, args []string) {
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svcEc2 := ec2.New(awsSession, awsSession.Config)
		awsSession.Config.Endpoint = aws.String(os.Getenv("DYNAMODB_ENDPOINT"))
		svcDynamo := dynamodb.New(awsSession)
		switch archiveFilter {
		case "all":
			cachedMovies, err := libraryService.GetCachedPlexMovies(svcDynamo)
			ShowError(err)
			var libraryMovies []models.Movie
			uploads, err := libraryService.GetGlacierMovies(svcDynamo)
			ShowError(err)
			for _, upload := range uploads {
				glacierMovie := models.Movie{
					Unwatched: 0,
					Metadata:  upload.Metadata,
				}
				libraryMovies = append(libraryMovies, glacierMovie)
			}
			allMovies := append(cachedMovies, libraryMovies...)
			if notify, _ := strconv.ParseBool(slackNotification); notify {
				slackService.SendCatalogue(allMovies, slackIncomingHookURL)
			}
			json, _ := json.Marshal(allMovies)
			fmt.Println(string(json))
		case "archived":
			var archivedMovies []models.Movie
			uploads, err := libraryService.GetGlacierMovies(svcDynamo)
			ShowError(err)
			for _, upload := range uploads {
				glacierMovie := models.Movie{
					Unwatched: 0,
					Metadata:  upload.Metadata,
				}
				archivedMovies = append(archivedMovies, glacierMovie)
			}
			if notify, _ := strconv.ParseBool(slackNotification); notify {
				slackService.SendCatalogue(archivedMovies, slackIncomingHookURL)
			}
			json, _ := json.Marshal(archivedMovies)
			fmt.Println(string(json))
		case "live":
			instanceID := ec2Service.FetchInstanceID(svcEc2, awsResourceTagNameValue)
			if ec2Status := ec2Service.Status(svcEc2, instanceID); strings.EqualFold(ec2Status, ec2StatusRunning) {
				liveMovies, err := libraryService.GetLivePlexMovies(1)
				ShowError(err)
				if notify, _ := strconv.ParseBool(slackNotification); notify {
					slackService.SendCatalogue(liveMovies, slackIncomingHookURL)
				}
				json, _ := json.Marshal(liveMovies)
				fmt.Println(string(json))
			} else {
				m := make(map[string]string)
				m["plex_status"] = strings.ToLower(ec2StatusStopped)
				jsonString, _ := json.Marshal(m)
				fmt.Println("\n" + string(jsonString))
			}
		case "watched":
			var watchedMovies []models.Movie
			plexMovies, err := libraryService.GetCachedPlexMovies(svcDynamo)
			ShowError(err)
			watchedMovies = filterMovies(plexMovies, func(unwatched int) bool {
				return unwatched == 0
			})
			if notify, _ := strconv.ParseBool(slackNotification); notify {
				slackService.SendCatalogue(watchedMovies, slackIncomingHookURL)
			}
			json, _ := json.Marshal(watchedMovies)
			fmt.Println(string(json))
		case "unwatched":
			var unwatchedMovies []models.Movie
			plexMovies, err := libraryService.GetCachedPlexMovies(svcDynamo)
			ShowError(err)
			unwatchedMovies = filterMovies(plexMovies, func(unwatched int) bool {
				return unwatched == 1
			})
			if notify, _ := strconv.ParseBool(slackNotification); notify {
				slackService.SendCatalogue(unwatchedMovies, slackIncomingHookURL)
			}
			json, _ := json.Marshal(unwatchedMovies)
			fmt.Println(string(json))
		default:
			plexMovies, err := libraryService.GetCachedPlexMovies(svcDynamo)
			ShowError(err)
			if notify, _ := strconv.ParseBool(slackNotification); notify {
				slackService.SendCatalogue(plexMovies, slackIncomingHookURL)
			}
			json, _ := json.Marshal(plexMovies)
			fmt.Println(string(json))
		}
	},
}
View Source
var DeleteArchiveLibraryCmd = &cobra.Command{
	Use:   "delete",
	Short: "To Delete Archives From Library",
	Long:  "to delete a show or a movie from the library.",
	Run: func(cmd *cobra.Command, args []string) {
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svcGlacier := glacier.New(awsSession)
		awsSession.Config.Endpoint = aws.String(os.Getenv("DYNAMODB_ENDPOINT"))
		svcDynamo := dynamodb.New(awsSession)
		deleteArchiveOutput := glacierService.DeleteArchive(archiveID, svcGlacier)
		err := libraryService.DeleteteGlacierInventoryArchive(archiveID, svcDynamo)
		ShowError(err)
		json, _ := json.Marshal(deleteArchiveOutput)
		fmt.Println(string(json))
	},
}
View Source
var DownloadLibraryCmd = &cobra.Command{
	Use:   "download",
	Short: "To Download Movies Or Shows",
	Long:  "to download movies or shows from the library.",
	Run: func(cmd *cobra.Command, args []string) {
		shutdownCh := make(chan struct{})
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svc := glacier.New(awsSession)
		from := glacierService.GetJobOutput(svc, jobID).Body
		defer from.Close()
		to, err := os.OpenFile(targetFile, os.O_RDWR|os.O_CREATE, 0666)
		ShowError(err)
		defer to.Close()
		_, err = io.Copy(to, from)
		ShowError(err)
		glacierService.Unzip(targetFile)
		glacierService.CleanupFiles([]string{targetFile}, "")
		ShowError(err)
		close(shutdownCh)
	},
}
View Source
var InitiateLibraryCmd = &cobra.Command{
	Use:   "initiate",
	Short: "To Initiate Library Jobs",
	Long:  "to initiate library inventory retrieval or archive retrieval.",
	Run: func(cmd *cobra.Command, args []string) {
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svc := glacier.New(awsSession)
		initiateJobOutput := glacierService.InitiateJob(svc, archiveID)
		if notify, _ := strconv.ParseBool(slackNotification); notify {
			slackService.SendInitiatedJobNotification(initiateJobOutput, slackIncomingHookURL)
		}
		json, _ := json.Marshal(initiateJobOutput)
		fmt.Println(string(json))
	},
}
View Source
var InventoryLibraryCmd = &cobra.Command{
	Use:   "inventory",
	Short: "To Show Library Inventory",
	Long:  "to show library inventory and if specified, sync with glacier.",
	Run: func(cmd *cobra.Command, args []string) {
		shutdownCh := make(chan struct{})
		go Indicator(shutdownCh)
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svcGlacier := glacier.New(awsSession)
		awsSession.Config.Endpoint = aws.String(os.Getenv("DYNAMODB_ENDPOINT"))
		svcDynamo := dynamodb.New(awsSession)
		if sync, _ := strconv.ParseBool(enableLibrarySync); sync && jobID != "" {
			err := libraryService.DeleteteGlacierInventoryArchives(svcDynamo)
			ShowError(err)
			jobOutputOutput := glacierService.GetJobOutput(svcGlacier, jobID)
			defer jobOutputOutput.Body.Close()
			response, err := ioutil.ReadAll(jobOutputOutput.Body)
			ShowError(err)
			var inventoryRetrieve = new(InventoryRetrieve)
			err = json.Unmarshal(response, &inventoryRetrieve)
			ShowError(err)
			for _, archive := range inventoryRetrieve.ArchiveList {
				err = libraryService.SaveGlacierInventoryArchive(archive, svcDynamo)
				ShowError(err)
			}
		} else {
			panic("job-id parameter should be provided")
		}
		glacierArchives, err := libraryService.GetGlacierInventoryArchives(svcDynamo)
		ShowError(err)
		jsonString, err := json.Marshal(glacierArchives)
		ShowError(err)
		if notify, _ := strconv.ParseBool(slackNotification); notify {
			slackService.SendInventory(glacierArchives, slackIncomingHookURL)
		}
		fmt.Println(string(jsonString))
		close(shutdownCh)
	},
}
View Source
var JobsLibraryCmd = &cobra.Command{
	Use:   "jobs",
	Short: "To List Library Jobs",
	Long:  "to list library jobs.",
	Run: func(cmd *cobra.Command, args []string) {
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svc := glacier.New(awsSession)
		jobList := glacierService.ListJobs(svc)
		var jobs []*glacier.JobDescription
		switch jobFilter {
		case "all":
			jobs = jobList.JobList
		case "archive":
			jobs = filterJobs(jobList.JobList, func(filter string) bool {
				return filter == "ArchiveRetrieval"
			})
		case "inventory":
			jobs = filterJobs(jobList.JobList, func(filter string) bool {
				return filter == "InventoryRetrieval"
			})
		default:
			jobs = jobList.JobList
		}
		json, _ := json.Marshal(jobs)
		if notify, _ := strconv.ParseBool(slackNotification); notify {
			slackService.SendJobs(jobs, slackIncomingHookURL)
		}
		fmt.Println(string(json))
	},
}
View Source
var (
	RootLibraryCmd = &cobra.Command{
		Use:   "library",
		Short: "To Control Media Library",
	}
)
View Source
var SyncLibraryCmd = &cobra.Command{
	Use:   "sync",
	Short: "To Sync Plex Watched Movies And Shows",
	Long:  "to sync plex watched movies and shows with internal flixctl db.",
	Run: func(cmd *cobra.Command, args []string) {
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		svc := ec2.New(awsSession, awsSession.Config)
		instanceID := ec2Service.FetchInstanceID(svc, awsResourceTagNameValue)
		if ec2Status := ec2Service.Status(svc, instanceID); strings.EqualFold(ec2Status, ec2StatusRunning) {
			SyncMovieLibrary(0, awsSession)
			SyncMovieLibrary(1, awsSession)
		} else {
			m := make(map[string]string)
			m["plex_status"] = strings.ToLower(ec2StatusStopped)
			jsonString, _ := json.Marshal(m)
			fmt.Println("\n" + string(jsonString))
		}
	},
}
View Source
var UploadLibraryCmd = &cobra.Command{
	Use:   "upload",
	Short: "To Upload Movies Or Shows",
	Long:  "to upload movies or shows to the library.",
	Run: func(cmd *cobra.Command, args []string) {
		shutdownCh := make(chan struct{})
		go Indicator(shutdownCh)
		var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
			SharedConfigState: sess.SharedConfigEnable,
		}))
		awsSession.Config.Endpoint = aws.String(os.Getenv("DYNAMODB_ENDPOINT"))
		svc := dynamodb.New(awsSession)
		movies, _ := libraryService.GetCachedPlexMovies(svc)
		var upload models.Upload
		if batchMode, _ := strconv.ParseBool(enableBatchUpload); batchMode {
			if maxUploadItems != "" {
				index, err := strconv.Atoi(maxUploadItems)
				ShowError(err)
				if index <= len(movies) {
					movies = movies[:index]
				}
			}
			for _, movie := range movies {
				if movie.Unwatched == 0 {
					sourceFolder, err := filepath.Abs(filepath.Dir(movie.Metadata.Media[0].Part[0].File))
					ShowError(err)
					archiveCreationOutput := Archive(movie.Metadata.Title, sourceFolder)
					upload = models.Upload{
						ArchiveCreationOutput: *archiveCreationOutput,
						Metadata:              movie.Metadata,
						Title:                 movie.Title,
					}
				}
				err := libraryService.SaveGlacierMovie(upload, svc)
				ShowError(err)
			}
		}
		close(shutdownCh)
	},
}

Functions

func Archive

func Archive(fileName string, sourceFolder string) *glacier.ArchiveCreationOutput

func Indicator

func Indicator(shutdownCh <-chan struct{})

func ShowError

func ShowError(err error)

func SyncMovieLibrary

func SyncMovieLibrary(unwatched int, awsSession *sess.Session)

Types

type InventoryRetrieve

type InventoryRetrieve struct {
	InventoryDate string                    `json:"InventoryDate"`
	VaultARN      string                    `json:"VaultARN"`
	ArchiveList   []models.InventoryArchive `json:"ArchiveList"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL