public string GetImdbIdByMovieHash(string movieFilename) { string _result = null; if (FileManager.DisableOpenSubtitles) { return(null); } if (!string.IsNullOrEmpty(movieFilename) && File.Exists(movieFilename) && m_osdbProxy != null && m_Token != null) { subInfo[] subs = new subInfo[1]; subs[0] = new subInfo(); subs[0].imdbid = string.Empty; subs[0].sublanguageid = ""; subs[0].moviehash = GetMovieHash(movieFilename); subs[0].moviebytesize = new FileInfo(movieFilename).Length.ToString(); subs[0].query = string.Empty; subrt subrt = null; try { subrt = m_osdbProxy.SearchSubtitles(m_Token, subs); } catch { } if (subrt != null && subrt.data.Length != 0) { _result = "tt" + subrt.data[0].IDMovieImdb.PadLeft(7, '0'); } } return(_result); }
private static void Go(string FolderArg, string outputPath, string LangArg, bool overwrite, bool rename, bool newOnly, bool nfo, bool folders, bool imdb, bool covers, bool nosubfolders) { Console.WriteLine("Searching for movie files"); #region Get Movie Files if (File.Exists(FolderArg)) { if (outputPath == "") { outputPath = Path.GetDirectoryName(FolderArg); } MovieFile theFile = new MovieFile(); theFile.filename = FolderArg; theFile.getOldSubtitle(subtitleFormats, theLangMap, FolderArg); myFiles.Add(theFile); } else if (Directory.Exists(FolderArg)) { if (outputPath == "") { outputPath = FolderArg; } foreach (string extension in movieFormats) { foreach (string filename in Utils.GetFilesByExtensions(FolderArg, "." + extension, SearchOption.AllDirectories)) { MovieFile theFile = new MovieFile(); theFile.filename = filename; theFile.getOldSubtitle(subtitleFormats, theLangMap, FolderArg); myFiles.Add(theFile); } } Console.WriteLine("Found " + myFiles.Count.ToString() + " Movie Files"); } else { throw new Exception("The folder or file given does not exist"); } #endregion Console.WriteLine("Creating subtitle request from movie files"); #region Create subtitle request from movie files langs = Get3CodeStr(LangArg).Split(','); List<string> l3 = new List<string>(langs); List<string> l2 = new List<string>(LangArg.Split(',')); subInfo[] si = new subInfo[myFiles.Count * langs.Length]; int i = 0; foreach (MovieFile theFile in myFiles) { if ((newOnly && (theFile.oldSubtitle == "")) || (!newOnly)) { foreach (string lang in langs) { try { si[i] = new subInfo(); si[i].sublanguageid = lang; theFile.hash = Utils.ToHexadecimal(Utils.ComputeMovieHash(theFile.filename)); si[i].moviehash = theFile.hash; si[i].moviebytesize = new FileInfo(theFile.filename).Length.ToString(); i++; } catch (Exception ex) { Console.WriteLine("Error with file: " + theFile.filename + "\n" + ex.Message); } } } } #endregion Console.WriteLine("Connecting..."); #region Connect and login osdbProxy = XmlRpcProxyGen.Create<IOSDb>(); osdbProxy.Url = "http://www.opensubtitles.org/xml-rpc"; osdbProxy.KeepAlive = false; if (myFiles.Count == 0) { Console.WriteLine("No movies found"); } XmlRpcStruct Login = osdbProxy.LogIn("", "", "en", "vroksub"); theToken = Login["token"].ToString(); #endregion Console.WriteLine("Searching for subtitles..."); #region Search for subtitles subrt SubResults = null; try { SubResults = osdbProxy.SearchSubtitles(theToken, si); } catch (Exception e) { } #endregion Console.WriteLine("Found subtitles:"); #region Choose best subtitle if (SubResults != null) { BindingList<subRes> g = new BindingList<subRes>(SubResults.data); dlCount = 0; foreach (MovieFile mf in myFiles) { try { if ((newOnly && (mf.oldSubtitle == "")) || (!newOnly)) { if (mf.SelectBestSubtitle(g, l3)) { Console.WriteLine(mf.subRes.MovieName + " - " + mf.subRes.LanguageName); dlCount++; } } } catch (Exception ex) { Console.WriteLine("Could not choose subtitle for: " + mf.filename + "\n" + ex.Message); } } #endregion Console.WriteLine("Downloading subtitles..."); #region Download Subtitles string[] ids = new string[dlCount]; int k = 0; foreach (MovieFile myf in myFiles) { if (myf.subtitleId != null) { ids[k] = myf.subtitleId; k++; } } subdata files = osdbProxy.DownloadSubtitles(theToken, ids); #endregion if (imdb) { Console.WriteLine("Fetching imdb details..."); #region Fetch imdb details foreach (MovieFile myf in myFiles) { if (myf.subRes != null) { try { myf.imdbinfo = osdbProxy.GetIMDBMovieDetails(theToken, "0" + myf.subRes.IDMovieImdb).data; } catch (Exception ex) { Console.WriteLine("Error fetching imdb data for: " + myf.filename + "\n" + ex.Message); } } } #endregion } Console.WriteLine("Saving subtitles..."); #region Process (rename, create folders, savenfo and save) subtitles foreach (subtitle s in files.data) { foreach (MovieFile m in myFiles) { try { if (m.subtitleId == s.idsubtitlefile) { m.subtitle = Utils.DecodeAndDecompress(s.data); if (outputPath != FolderArg) { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } if (!File.Exists(outputPath + "\\" + Path.GetFileName(m.filename))) { try { File.Move(m.filename, outputPath + "\\" + Path.GetFileName(m.filename)); m.originalfilename = Path.GetFileName(m.filename); m.filename = outputPath + "\\" + Path.GetFileName(m.filename); } catch (Exception ex) { Console.WriteLine("Error moving movie: " + m.filename + "\n" + ex.Message); } } } if (folders) { m.newFolder(outputPath, FolderFormat); } if (rename) { m.rename(FileFormat, CDFormat); } if (nfo) { m.saveNfo(); } m.saveSubtitle(overwrite); continue; } } catch (Exception ex) { Console.WriteLine("Error saving subtitle for: " + m.filename + "\n" + ex.Message); } } } } #endregion if (covers) { Console.WriteLine("Downloading covers..."); #region Download covers System.Net.WebClient Client = new WebClient(); foreach (MovieFile myf in myFiles) { if (myf.imdbinfo != null) { if ((myf.imdbinfo.cover != null) && (myf.imdbinfo.cover != "")) { try { Stream strm = Client.OpenRead(myf.imdbinfo.cover); FileStream writecover = new FileStream(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", FileMode.Create); int a; do { a = strm.ReadByte(); writecover.WriteByte((byte)a); } while (a != -1); writecover.Position = 0; File.Copy(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileName(myf.filename) + ".jpg"); if (folders) { File.Copy(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", Path.GetDirectoryName(myf.filename) + "\\folder.jpg"); } strm.Close(); writecover.Close(); } catch (Exception ex) { Console.WriteLine("Error saving cover for: " + myf.filename + "\n" + ex.Message); } } } #endregion } } Console.WriteLine("Disconnecting..."); #region Disconnect osdbProxy.LogOut(theToken); #endregion }
private void Search(string FolderArg, string outputPath, string LangArg, bool overwrite, bool rename, bool newOnly, bool nfo, bool folders, bool imdb) { subtitleFound = false; try { infoBox.Items.Add("Searching for movie files..."); if (File.Exists(FolderArg)) { if (outputPath == "") { outputPath = Path.GetDirectoryName(FolderArg); } MovieFile theFile = new MovieFile(); theFile.filename = FolderArg; theFile.getOldSubtitle(subtitleFormats, theLangMap, LangArg); myFiles.Add(theFile); } else if (Directory.Exists(FolderArg)) { if (outputPath == "") { outputPath = FolderArg; } foreach (string extension in movieFormats) { foreach (string filename in Utilities.GetFilesByExtensions(FolderArg, "." + extension, SearchOption.AllDirectories)) { MovieFile theFile = new MovieFile(); theFile.filename = filename; theFile.getOldSubtitle(subtitleFormats, theLangMap, LangArg); if ((newOnly && (theFile.oldSubtitle == "")) || (!newOnly)) { myFiles.Add(theFile); } } } infoBox.Items.Add("Found " + myFiles.Count.ToString() + " movies"); } else { infoBox.Items.Add("The folder or file given does not exist"); } if (myFiles.Count != 0) { infoBox.Items.Add("Creating subtitle requests from movies..."); langs = Get3CodeStr(LangArg).Split(','); List <string> l3 = new List <string>(langs); List <string> l2 = new List <string>(LangArg.Split(',')); int i = 0; List <List <subInfo> > allSis = new List <List <subInfo> >(); List <subInfo> sis = new List <subInfo>(); foreach (MovieFile theFile in myFiles) { if ((newOnly && (theFile.oldSubtitle == "")) || (!newOnly)) { foreach (string lang in langs) { try { if (sis.Count >= 40) { allSis.Add(sis); sis = new List <subInfo>(); } subInfo si = new subInfo(); si.sublanguageid = lang; theFile.hash = Utilities.ToHexadecimal(Utilities.ComputeMovieHash(theFile.filename)); si.moviehash = theFile.hash; si.moviebytesize = new FileInfo(theFile.filename).Length.ToString(); sis.Add(si); i++; } catch (Exception ex) { infoBox.Items.Add("Error with file: " + theFile.filename); infoBox.Items.Add(ex.Message); } } } } if (sis.Count > 0) { allSis.Add(sis); } infoBox.Items.Add("Connecting..."); osdbProxy = XmlRpcProxyGen.Create <IOSDb>(); osdbProxy.Url = "http://api.opensubtitles.org/xml-rpc"; osdbProxy.KeepAlive = false; if (myFiles.Count == 0) { infoBox.Items.Add("No movies found"); } XmlRpcStruct Login = osdbProxy.LogIn("", "", "en", "vroksub"); theToken = Login["token"].ToString(); infoBox.Items.Add("Searching for subtitles..."); subrt SubResults = new subrt(); List <subRes> lstSubResults = new List <subRes>(); try { foreach (List <subInfo> theSis in allSis) { subrt tempSubResults = osdbProxy.SearchSubtitles(theToken, theSis.ToArray()); lstSubResults.AddRange(tempSubResults.data); SubResults.seconds += tempSubResults.seconds; } SubResults.data = lstSubResults.ToArray(); } catch (Exception e) { } if (SubResults != null) { BindingList <subRes> g = new BindingList <subRes>(SubResults.data); dlCount = 0; foreach (MovieFile mf in myFiles) { try { if ((newOnly && (mf.oldSubtitle == "")) || (!newOnly)) { if (mf.SelectBestSubtitle(g, l3)) { subtitleFound = true; infoBox.Items.Add("Subtitles found"); infoBox.Items.Add(mf.subRes.MovieName + " - " + mf.subRes.LanguageName); dlCount++; } } } catch (Exception ex) { infoBox.Items.Add("Couldn't choose subtitle for: " + mf.filename); infoBox.Items.Add(ex.Message); } } if (subtitleFound) { infoBox.Items.Add("Downloading subtitles..."); } else { infoBox.Items.Add("No subtitles found"); } string[] ids = new string[dlCount]; List <string> lstids = new List <string>(); List <List <string> > allids = new List <List <string> >(); int k = 0; foreach (MovieFile myf in myFiles) { if (lstids.Count >= 40) { allids.Add(lstids); lstids = new List <string>(); } if (myf.subtitleId != null) { lstids.Add(myf.subtitleId); k++; } } if (lstids.Count > 0) { allids.Add(lstids); } subdata files = new subdata(); List <subtitle> thesubs = new List <subtitle>(); foreach (List <string> theList in allids) { subdata tempfiles = osdbProxy.DownloadSubtitles(theToken, lstids.ToArray()); thesubs.AddRange(tempfiles.data); files.seconds += tempfiles.seconds; } files.data = thesubs.ToArray(); if (imdb) { infoBox.Items.Add("Fetching IMDb details..."); foreach (MovieFile myf in myFiles) { if (myf.subRes != null) { try { myf.imdbinfo = osdbProxy.GetIMDBMovieDetails(theToken, "0" + myf.subRes.IDMovieImdb).data; } catch //(Exception ex) { //infoBox.Items.Add("Error fetching IMDb data for: " + myf.filename + " ERROR: " + ex.Message); } } } infoBox.Items.Add("Downloading covers..."); WebClient Client = new WebClient(); Client.Encoding = Encoding.UTF8; foreach (MovieFile myf in myFiles) { if (myf.imdbinfo != null) { if ((myf.imdbinfo.cover != null) && (myf.imdbinfo.cover != "")) { try { Stream strm = Client.OpenRead(myf.imdbinfo.cover); FileStream writecover = new FileStream(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", FileMode.Create); int a; do { a = strm.ReadByte(); writecover.WriteByte((byte)a); }while (a != -1); writecover.Position = 0; File.Copy(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileName(myf.filename) + ".jpg"); if (folders) { File.Copy(Path.GetDirectoryName(myf.filename) + "\\" + Path.GetFileNameWithoutExtension(myf.filename) + ".jpg", Path.GetDirectoryName(myf.filename) + "\\folder.jpg"); } strm.Close(); writecover.Close(); } catch (Exception ex) { infoBox.Items.Add("Error saving cover for: " + myf.filename); infoBox.Items.Add(ex.Message); } } } } } if (subtitleFound) { infoBox.Items.Add("Saving subtitles..."); } foreach (subtitle s in files.data) { foreach (MovieFile m in myFiles) { try { if (m.subtitleId == s.idsubtitlefile) { m.subtitle = Utilities.DecodeAndDecompress(s.data); if (outputPath != FolderArg) { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } if (!File.Exists(outputPath + "\\" + Path.GetFileName(m.filename))) { try { File.Move(m.filename, outputPath + "\\" + Path.GetFileName(m.filename)); m.originalfilename = Path.GetFileName(m.filename); m.filename = outputPath + "\\" + Path.GetFileName(m.filename); } catch //(Exception ex) { //infoBox.Items.Add("Error moving movie: " + m.filename + " ERROR: " + ex.Message); } } } if (folders) { m.newFolder(outputPath, FolderFormat); } if (rename) { m.rename(FileFormat, CDFormat); } if (nfo) { m.saveNfo(); } m.saveSubtitle(overwrite); continue; } } catch (Exception ex) { infoBox.Items.Add("Error saving subtitle for: " + m.filename); infoBox.Items.Add(ex.Message); } } } } infoBox.Items.Add("Disconnecting..."); osdbProxy.LogOut(theToken); } } catch (Exception ex) { infoBox.Items.Add("Generic error:"); infoBox.Items.Add(ex.Message); } }