Jump to content

OT - Regex is probably better then this??...


Recommended Posts

Posted (edited)

I know emby does string iterations with Regex for episode naming. Which is probably better then this giant mess of code.

 

Just trying to:

 

1. decide if folder contents are movies or episodes  

2. get the Movie name form the folder name.

3. Make each word have an upper case

 

It works, but the code is huge!

Comments are welcome.

Regex would be better, I think.

 

I am not very good with regular expressions.


 public static string GetNameFromFileName(string inputName)
        {
            // Typical folder string = "assassins.creed.2016.720p.bluray.x264"

            string[] nm1 = {}; // Hold the Folder Name string[] here
            string result = string.Empty;

            // Split up the string before the resolution fragment, we only want any thing before it 
            if(inputName.Contains("1080p"))
            nm1 = inputName.Split(new[] {"1080p"}, StringSplitOptions.None);
            else if(inputName.Contains("720p"))
            nm1 = inputName.Split(new[] {"720p"}, StringSplitOptions.None);


            if (nm1.Any() && // If there is a string to check
                            !IsEpisode(nm1[0])) // If that string doesn't contain any episode identifers
            {
                // Remove the period delimiter and replace it with a space - Sure work with spaces instead of dots
                nm1[0] = nm1[0].Replace(".", " ");

                // First letter to Upper Case eg. "assassins creed" = "Assassins creed"
                nm1[0] = MakeUpperCase(nm1[0]);
                
                //Split the entire Movie name - Hopefully it looks like: "Assassins creed 2017" 
                //Make it: "Assassins", "creed", "2017"
                string[] nm2 = nm1[0].Split(new[] {" "}, StringSplitOptions.None);

                // Get the remaining Title fragments after first Word
                if (nm2.Count() > 2) // Better check that the array count is bigger then [0,1,2]...because... Error... that's why.
                {
                    for (int i = 0; i <= nm2.Count() - 2; i ++)
                    {
                        if ( //If it's not a year or "bluray"
                            !IsYear(nm2[i]) &&
                            nm2[i].ToLower() != "bluray" )
                        {
                            // Give the Title Fragment an upperCase
                            nm2[i] = MakeUpperCase(nm2[i]);

                            // Add to the Result String
                            result += " " + nm2[i];
                        }
                    }
                    
                    MessageBox.Show(result);

                }
            }

            return result;
        }

        private static bool IsEpisode(string input)
        {
            // Split the input name at each period "Marvels.Iron.Fist.S01.E02." = "Marvels", "Iron", "Fist", "s01e02", "" <--Empty Stirng at the end
            string[] nameArray = input.Split(new [] {"."},StringSplitOptions.None);
            // Looking at the second last array item (because of Empty String at the end)
            // Looking for first Character to be "s", and seocnd chartere to be digit: "S01" <--First "s", Second 0
            // if it is true then the media item is an episode - return true
            return nameArray[nameArray.Count() - 2][0].ToString().ToLower() == "s" &&
                   char.IsDigit(nameArray[nameArray.Count() - 2][1]);
        }

        private static bool IsYear(string input)
        {
            // The worst way to figure out if the string is a year... 
            int year;
            return int.TryParse(input, out year) && (year >= 1942 && year <= 2142);
        }

        private static string MakeUpperCase(string input)
        {
            return char.ToUpper(input[0]) + input.Substring(1);
        }
    }





Edited by chef
Posted (edited)

Next bit of code I'll write will get root folders from emby API and decide where to create a movie folder with the name extracted from the code above.

Create it, then move file into it.

 

But first this bit of code I wrote is kinda interesting:

public static string GetAuthToken()
        {
            string response = string.Empty;
            try
            {
                string userName; //= {UserName};
                string password; // = {Password};
                string url; //= eg. "http://192.xxx.x.x:{Port}" {Url with PORT}
                using (var client = new WebClient())
                {
                    client.Headers["Content-Type"] = "text/plain";
                    client.Headers["Authorization"] = "Basic " + Base64Encode(userName + ":" + password);
                    var html = new HtmlDocument();
                    html.LoadHtml(Encoding.UTF8.GetString(
                        client.DownloadData(url + "/gui/token.html")));
                    response = html.DocumentNode.ChildNodes[0].InnerText;
                }
            }catch {}
            return response;
        }

public static string Base64Encode(string plainText)
        {
            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            return Convert.ToBase64String(plainTextBytes);
        }
Edited by chef

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...