コード例 #1
0
        // Private Methods (2) 

        private static bool processNewMovieFile(ref long userImdbId, MovieFileInfo fileInfo, ref TryUploadResult res, Exception ex)
        {
            if (ex.Message.Contains("response contains boolean value where array expected")) // what did you expect from PHP developers?!
            {
                if (userImdbId == 0)
                {
                    userImdbId = Imdb.GetImdbId(Path.GetFileNameWithoutExtension(fileInfo.MovieFileName));
                    if (userImdbId == 0)
                    {
                        throw new NotSupportedException("It's a new movie file. Please find/fill its IMDB Id first.");
                    }
                    LogWindow.AddMessage(LogType.Info, string.Format("ImdbId: {0}", userImdbId));
                }

                //it's a new movie file and site's db has no info (IDMovieImdb val) about it.
                res = new TryUploadResult {
                    data = null, status = "200 OK", alreadyindb = 0
                };
                return(true);
            }
            return(false);
        }
コード例 #2
0
        public string UploadSubtitle(long userImdbId, string subLanguageId, string subFileNamePath, Action <int> progress)
        {
            string finalUrl;

            subFileNamePath = new ChangeEncoding().TryReduceRtlLargeFileContent(subFileNamePath);
            var fileInfo = new MovieFileInfo(_movieFileName, subFileNamePath);

            //login
            if (progress != null)
            {
                progress(10);
            }

            LogWindow.AddMessage(LogType.Info, "TryLogin ...");
            tryLogin();

            if (progress != null)
            {
                progress(25);
            }

            LogWindow.AddMessage(LogType.Info, "TryUploadSubtitle ...");

            TryUploadResult res = null;

            try
            {
                res = _client.TryUploadSubtitles(_loginToken,
                                                 new[]
                {
                    new TryUploadInfo
                    {
                        subhash       = fileInfo.SubtitleHash,
                        subfilename   = fileInfo.SubFileName,
                        moviehash     = fileInfo.MovieHash,
                        moviebytesize = fileInfo.MovieFileLength,
                        moviefilename = fileInfo.MovieFileName
                    }
                }
                                                 );
            }
            catch (Exception ex)
            {
                if (!processNewMovieFile(ref userImdbId, fileInfo, ref res, ex))
                {
                    throw;
                }
            }

            if (res == null)
            {
                throw new InvalidOperationException("Bad response ...");
            }

            if (progress != null)
            {
                progress(50);
            }

            if (res.status != "200 OK")
            {
                throw new Exception("Bad response ...");
            }

            if (res.alreadyindb == 0)
            {
                if ((userImdbId == 0) && (res.data == null || res.data.Length == 0))
                {
                    throw new Exception("Bad format ...");
                }

                LogWindow.AddMessage(LogType.Info, string.Format("CheckSubHash({0})", fileInfo.SubtitleHash));
                var checkSubHashRes = _client.CheckSubHash(_loginToken, new[] { fileInfo.SubtitleHash });

                if (progress != null)
                {
                    progress(75);
                }

                var idSubtitleFile = int.Parse(checkSubHashRes.data[fileInfo.SubtitleHash].ToString());
                if (idSubtitleFile > 0)
                {
                    throw new Exception("Duplicate subHash, alreadyindb.");
                }

                LogWindow.AddMessage(LogType.Info, "PostData ...");
                //xml-rpc.net dll does not work here so, ...
                var post = PostXml.PostData(
                    ConfigSetGet.GetConfigData("OpensubtitlesOrgApiUri"),
                    UploadData.CreateUploadXml(_loginToken,
                                               new UploadBaseinfo
                {
                    idmovieimdb      = res.data != null ? res.data[0]["IDMovieImdb"].ToString() : userImdbId.ToString(),
                    sublanguageid    = subLanguageId,
                    movieaka         = string.Empty,
                    moviereleasename = fileInfo.MovieReleaseName,
                    subauthorcomment = string.Empty
                },
                                               new UploadCDsInfo
                {
                    moviebytesize = fileInfo.MovieFileLength.ToString(),
                    moviefilename = fileInfo.MovieFileName,
                    moviehash     = fileInfo.MovieHash,
                    subfilename   = fileInfo.SubFileName,
                    subhash       = fileInfo.SubtitleHash,
                    subcontent    = fileInfo.SubContentToUpload,
                    moviefps      = string.Empty,
                    movieframes   = string.Empty,
                    movietimems   = string.Empty
                }));

                LogWindow.AddMessage(LogType.Info, "Done!");
                finalUrl = RegexHelper.GetUploadUrl(post);
                LogWindow.AddMessage(LogType.Announcement, string.Format("Url: {0}", finalUrl));
            }
            else
            {
                throw new Exception("Duplicate file, alreadyindb");
            }

            if (progress != null)
            {
                progress(100);
            }
            return(finalUrl.Trim());
        }