Ejemplo n.º 1
0
        public VideoInfo ParseOtherHandrake(string fileFullName, VideoItemFile videoItemFile)
        {
            VideoInfo videoInfo = new VideoInfo();

            videoInfo.Initialize();

            string contents = MyFile.ReadAllText(fileFullName);

            if (String.IsNullOrEmpty(contents))
            {
                return(videoInfo);
            }

            Regex regexSize = new Regex(@"\+ Crop and Scale \(([0-9]{3,4}):([0-9]{3,4}):", RegexOptions.IgnoreCase);
            Match matchSize = regexSize.Match(contents);

            if (matchSize.Success && matchSize.Groups.Count == 3)
            {
                int width = 0;
                int.TryParse(matchSize.Groups[1].Value, out width);
                videoInfo.videoItem.encoding.width = width;
                int height = 0;
                int.TryParse(matchSize.Groups[2].Value, out height);
                videoInfo.videoItem.encoding.height = height;
            }

            if (contents.IndexOf("x264", StringComparison.OrdinalIgnoreCase) != -1)
            {
                videoInfo.videoItem.encoding.codec = "x264";
            }

            // hmm .. not alawys valid ..
            // Regex regexBitrate = new Regex(@"\+ bitrate ([0-9]{3,5}) kbps", RegexOptions.IgnoreCase);
            // so try summing all track instead
            int             bitrateVideoAudio = 0;
            Regex           regexBitrate      = new Regex(@"bytes, ([0-9]{3,5})\.[0-9]{2} kbps, fifo", RegexOptions.IgnoreCase);
            MatchCollection matchBitrates     = regexBitrate.Matches(contents);

            foreach (Match matchBitrate in matchBitrates)
            {
                if (matchBitrate.Success && matchBitrate.Groups.Count == 2)
                {
                    for (int index = matchBitrate.Groups.Count - 1; index >= 1; index--)
                    {
                        int bitrate = 0;
                        int.TryParse(matchBitrate.Groups[index].Value, out bitrate);
                        bitrateVideoAudio += bitrate;
                    }
                }
            }
            // MyLog.Add(videoItemFile.FullName + " bitrate : " + bitrateVideoAudio.ToString());
            if (bitrateVideoAudio > 100)
            {
                videoInfo.videoItem.encoding.bitrate = bitrateVideoAudio * 1024;
            }


            return(videoInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// update the XBMC .nfo file
        /// </summary>
        /// <param name="videoItemFile"></param>
        /// <param name="videoItem"></param>
        /// <returns></returns>
        public bool UpdateXBMC(VideoInfo videoInfo)
        {
            // MyLog.Add("UpdateXBMC");

            string videoFullName = null;
            string fileContents  = null;

            if (videoInfo.files == null || videoInfo.files.xbmc == null)
            {
                if (Config.settings.createXBMC)
                {
                    videoFullName = videoInfo.videoDirectory + @"\movie.nfo";
                    fileContents  = GetDefaultXBMCxml();
                }
                else
                {
                    return(false);
                }
            }
            else if (!Config.settings.updateXBMC)
            {
                return(false);
            }
            else
            {
                videoFullName = videoInfo.GetFullName(videoInfo.files.xbmc);
                fileContents  = MyFile.ReadAllText(videoFullName);
            }
            if (fileContents == null || videoFullName == null)
            {
                return(false);
            }

            VideoItem videoItem = videoInfo.videoItem;


            XmlDocument doc = MyXMLDoc.LoadXml(fileContents);

            if (doc == null)
            {
                return(false);
            }

            MyXMLDoc.SetSingleNodeString(doc, "/movie/title", videoItem.title);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/plot", videoItem.plot);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/set", videoItem.movieset);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/tagline", videoItem.tagline);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/id", videoItem.imdbId);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/tmdbId", videoItem.tmdbId);
            MyXMLDoc.SetSingleNodeString(doc, "/movie/mpaa", videoItem.mpaa);

            MyXMLDoc.SetSingleNodeDecimal(doc, "/movie/rating", videoItem.imdbRating);

            MyXMLDoc.SetSingleNodeInt(doc, "/movie/year", videoItem.year);
            MyXMLDoc.SetSingleNodeInt(doc, "/movie/runtime", videoItem.runtime);
            MyXMLDoc.SetSingleNodeInt(doc, "/movie/playcount", videoItem.playCount);
            int watched = VideoFileEnums.watched.GetValueByKey(videoItem.watched);

            // either watched or not
            if (watched != 0 && watched != 1)
            {
                watched = 0;
            }
            MyXMLDoc.SetSingleNodeInt(doc, "/movie/watched", watched);

            bool ret = MyXMLDoc.SaveXML(doc, videoFullName);

            if (ret)
            {
                MyLog.Add("wrote " + videoFullName);
            }
            else
            {
                MyLog.Add("error writing to " + videoFullName);
            }
            return(ret);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// update the MB .xml file
        /// </summary>
        /// <param name="videoItemFile"></param>
        /// <param name="videoItem"></param>
        /// <returns></returns>
        public bool UpdateMB(VideoInfo videoInfo)
        {
            string videoFullName = null;
            string fileContents  = null;

            if (videoInfo.files == null || videoInfo.files.mb == null)
            {
                if (Config.settings.createMB)
                {
                    videoFullName = videoInfo.videoDirectory + @"\movie.xml";
                    fileContents  = GetDefaultMBxml();
                }
                else
                {
                    return(false);
                }
            }
            else if (!Config.settings.updateMB)
            {
                return(false);
            }
            else
            {
                videoFullName = videoInfo.GetFullName(videoInfo.files.mb);
                fileContents  = MyFile.ReadAllText(videoFullName);
            }
            if (fileContents == null || videoFullName == null)
            {
                return(false);
            }
            VideoItem videoItem = videoInfo.videoItem;

            XmlDocument doc = MyXMLDoc.LoadXml(fileContents);

            if (doc == null)
            {
                return(false);
            }

            MyXMLDoc.SetSingleNodeString(doc, "/Title/LocalTitle", videoItem.title);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/Overview", videoItem.plot);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/set", videoItem.movieset);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/tagline", videoItem.tagline);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/IMDB", videoItem.imdbId);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/TMDbId", videoItem.tmdbId);
            MyXMLDoc.SetSingleNodeString(doc, "/Title/ContentRating", videoItem.mpaa);

            MyXMLDoc.SetSingleNodeDecimal(doc, "/Title/Rating", videoItem.imdbRating);

            MyXMLDoc.SetSingleNodeInt(doc, "/Title/ProductionYear", videoItem.year);
            MyXMLDoc.SetSingleNodeInt(doc, "/Title/RunningTime", videoItem.runtime);

            bool ret = MyXMLDoc.SaveXML(doc, videoFullName);

            if (ret)
            {
                MyLog.Add("wrote " + videoFullName);
            }
            else
            {
                MyLog.Add("error writing to " + videoFullName);
            }
            return(ret);
        }
Ejemplo n.º 4
0
        public bool SyncUpVideoSource(FileInfo sourceFileInfo)
        {
            // clean up old uploads
            IEnumerable <string> files = MyFile.EnumerateFiles(@"sync", "*.gz");

            foreach (string file in files)
            {
                MyFile.DeleteFile(file);
            }

            // compress file, video source
            string compressedFile = @"sync\" + MyFile.SafeFileName(sourceFileInfo.Name);

            if (!MyFile.Compress(sourceFileInfo.FullName, compressedFile))
            {
                return(false);
            }

            // rename file so has 'rand' key/iv
            Random random     = new Random();
            int    rand       = random.Next(0, MyEncrypt.sharedKeys.Length - 1);
            string uploadFile = compressedFile.Replace("." + Config.settings.exportExt, "-" + rand + "." + Config.settings.exportExt);

            File.Move(compressedFile + ".gz", uploadFile + ".gz");



            string key       = MyEncrypt.sharedKeys[rand];
            string iv        = MyEncrypt.GenerateIV();
            string delimiter = "#";

            // now encrypt compressed file contents
            string fileContents = MyFile.ReadAllBinaryToString(uploadFile + ".gz");

            string contentsHeader = "{";

            contentsHeader += "\"apiKey\":\"" + this.apiKey + "\", ";
            contentsHeader += "\"iv\":\"" + iv + "\", ";
            contentsHeader += "\"sync\":" + MySerialize.ToJSON(Sync.syncSettings);
            contentsHeader += "}";

            contentsHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentsHeader));



            fileContents = MyEncrypt.EncryptRJ256(key, iv, fileContents);
            if (fileContents == null)
            {
                return(false);
            }

            contentsHeader = MyEncrypt.EncryptRJ256(key, iv, contentsHeader);

            string contentsToEncode = contentsHeader + delimiter + fileContents;

            // write base64 encoded file
            File.WriteAllText(uploadFile + ".enc", contentsToEncode);


            // log it
            FileInfo encodedFileInfo = MyFile.FileInfo(uploadFile + ".enc");

            if (encodedFileInfo == null)
            {
                return(false);
            }
            MyLog.Add(String.Format("Encrypted {0} to {1}", encodedFileInfo.Name, MyFile.FormatSize(encodedFileInfo.Length)));


            // test decrypt

            fileContents = MyFile.ReadAllText(uploadFile + ".enc");

            string[] fileParts = fileContents.Split(new string[] { delimiter }, StringSplitOptions.None);

            fileContents = MyEncrypt.DecryptRJ256(key, iv, fileParts[1]);



            MyFile.DeleteFile(uploadFile + ".gz");
            MyFile.WriteAllBinaryFromString(uploadFile + ".gz", fileContents);



            // post encoded file to website

            string url = apiURL;
            List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> > {
            };

            headers.Add(new KeyValuePair <string, string>("api-key", apiKey));
            headers.Add(new KeyValuePair <string, string>("access-token", iv));
            Upload(url, uploadFile + ".enc", headers);



            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// read the video directory and try to parse video info
        /// such as XBMC, MB
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public VideoInfo ReadDirectory(string directory)
        {
            bool      parsedFile = false;
            VideoInfo videoInfo  = new VideoInfo();

            videoInfo.Initialize();
            videoInfo.videoDirectory = directory;
            VideoInfo parsedVideoInfo = new VideoInfo();

            parsedVideoInfo.Initialize();
            parsedVideoInfo.videoDirectory = directory;

            IEnumerable <string> files = MyFile.EnumerateFiles(directory);

            if (files.Count() == 0)
            {
                // empty folder .. maybe a category folder, or movie placeholder
                return(videoInfo);
            }


            // MyLog.Add("ReadDirectory: " + files.Count() + " files");
            foreach (string file in files)
            {
                FileInfo fileInfo = MyFile.FileInfo(file);

                if (fileInfo == null)
                {
                    continue;
                }
                VideoItemFile videoItemFile = new VideoItemFile();
                videoItemFile.Set(directory, fileInfo);

                // parse nfo/xml/file to get file info

                videoInfo.files.qty += 1;

                string fileExt = fileInfo.Extension.TrimStart('.');
                switch (fileExt)
                {
                case "avi":
                case "m4v":
                case "mov":
                case "mpg":
                case "mkv":
                case "mp4":
                case "mpeg":
                    videoInfo.files.video = videoItemFile;

                    //if (videoInfo.videoItem.encoding == null)
                    //{
                    string encoding = RunFFprobe(videoInfo.GetFullName(videoInfo.files.video));
                    if (encoding != null)
                    {
                        videoInfo.videoItem.encoding = ParseFFprobe(encoding);
                    }
                    //}
                    break;

                case "jpg":
                case "jpeg":
                case "png":
                    if (fileInfo.Name == "poster.jpg")
                    {
                        videoInfo.files.poster = videoItemFile;
                    }
                    else if (fileInfo.Name == "fanart.jpg")
                    {
                        videoInfo.files.fanart = videoItemFile;
                    }
                    videoInfo.files.images.Add(videoItemFile);
                    break;

                case "mve":
                case "nfo":
                case "xml":
                    // TODO btr way to ensure which type parsing

                    string fileContents = MyFile.ReadAllText(fileInfo.FullName);
                    fileContents = fileContents.Trim();
                    if (String.IsNullOrEmpty(fileContents))
                    {
                        continue;
                    }
                    if (fileContents.StartsWith("<?xml"))
                    {
                        XmlDocument doc = MyXMLDoc.LoadXml(fileContents);
                        if (doc == null)
                        {
                            continue;
                        }



                        if (fileContents.Contains("<VideoInfo>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseMVE(fileInfo.FullName);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                // videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.mve = videoItemFile;

                            parsedFile = true;
                        }
                        if (fileContents.Contains("<Title>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseMB(doc);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                //videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.mb = videoItemFile;

                            parsedFile = true;
                        }
                        if (fileContents.Contains("<movie>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseXBMC(doc);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                //videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.xbmc = videoItemFile;

                            parsedFile = true;
                        }
                    }
                    else if (fileContents.StartsWith("{"))
                    {
                        //if (parsedFile == false)
                        {
                            parsedVideoInfo = ParseMVE(fileInfo.FullName);
                            videoInfo.MergeVideoItemWith(parsedVideoInfo);
                            // videoInfo.videoItem = parsedVideoInfo.videoItem;
                        }
                        videoInfo.files.mve = videoItemFile;

                        parsedFile = true;
                    }

                    break;

                case "txt":
                    if (videoItemFile.Name.StartsWith("handbrake"))
                    {
                        parsedVideoInfo = ParseOtherHandrake(videoInfo.GetFullName(videoItemFile), videoItemFile);
                        videoInfo.MergeVideoItemWith(parsedVideoInfo);
                    }
                    else
                    {
                        parsedVideoInfo = ParseOtherFileName(videoInfo.GetFullName(videoItemFile), videoItemFile);
                        videoInfo.MergeVideoItemWith(parsedVideoInfo);
                    }

                    videoInfo.files.others.Add(videoItemFile);
                    break;

                default:
                    videoInfo.files.others.Add(videoItemFile);
                    break;
                } // end switch ext
            }     // end foreach file


            // couldnt parse nfo/xml and have video file in dir, so use video file for some basic info
            if (!parsedFile && videoInfo.files != null && videoInfo.files.video != null)
            {
                parsedVideoInfo = ParseVideoFile(videoInfo.files.video);
                videoInfo.MergeVideoItemWith(parsedVideoInfo);

                parsedFile = true;
            }

            /* .. no video file .. so skip
             * // couldnt parse nfo/xml and have files in dir, so use dir for some basic info
             * else if (!parsedFile && files.Count() > 0)
             * {
             *  DirectoryInfo directoryInfo = new DirectoryInfo(directory);
             *  parsedVideoInfo = ParseDirectory(directoryInfo);
             *  videoInfo.MergeVideoItemWith(parsedVideoInfo);
             *
             *  parsedFile = true;
             * }
             */

            if (videoInfo.videoItem.title == null || videoInfo.files == null || videoInfo.files.video == null)
            {
                videoInfo = null;
            }
            else
            {
                // sanitize some data

                // remove (year) from title .. bad prior parse mb, xbmc
                videoInfo.videoItem.title = Regex.Replace(videoInfo.videoItem.title, @"\([0-9]{4}\)$", "");
            }

            return(videoInfo);
        }