Example #1
0
        /// <summary>
        /// Event for when data is received over websockets.
        /// </summary>
        static void WsMessageReceived(object sender, WebSocketEventArgs args)
        {
            string msg = args.Message;

            if (msg.Contains("getFiles"))
            {
                try
                {
                    scanDirs.Abort();
                }
                catch
                {
                }
                scanDirs = new Thread(new ThreadStart(UpdateFileDic));
                scanDirs.Start();
            }
            else if (msg.Contains("GetYoutubeTrailer:"))
            {
                string search = msg.Replace("GetYoutubeTrailer:", "").Trim();

                try
                {
                    using (WebClient client = new WebClient())
                    {
                        Console.WriteLine("Requested trailer for: " + search);
                        string data = client.DownloadString("https://www.youtube.com/results?search_query=" + search.Replace(' ', '+') + "+trailer&spf=navigate");
                        string json = GetTrailersFromYoutubeData(data);
                        ws.SendGlobalMessage("TRAILERS: " + json);
                    }
                }
                catch
                {
                    ws.SendGlobalMessage("ERROR WHILE PARSING URL");
                }
            }
            else if (msg.Contains("SetDir:"))
            {
                try
                {
                    string dirToSet = msg.Split(new string[] { "SetDir:" }, StringSplitOptions.None)[1];



                    if (Directory.Exists(dirToSet))
                    {
                        if (dirToSet.Contains('/'))
                        {
                            if (dirToSet[dirToSet.Length - 1] != '/')
                            {
                                dirToSet = dirToSet + '/';
                            }
                        }
                        else if (dirToSet.Contains('\\'))
                        {
                            if (dirToSet[dirToSet.Length - 1] != '\\')
                            {
                                dirToSet = dirToSet + '\\';
                            }
                        }

                        dirToScan = dirToSet;
                        http.SetFileDir(dirToScan);

                        if (File.Exists("config.ini"))
                        {
                            string[] lines     = File.ReadAllLines("config.ini");
                            string   newConfig = "";

                            foreach (string config in lines)
                            {
                                Console.WriteLine("ConfigData: " + config);
                                if (config.Contains("customDir = "))
                                {
                                    newConfig = newConfig + Environment.NewLine + "customDir = " + dirToScan;
                                }
                                else
                                {
                                    newConfig = newConfig + Environment.NewLine + config;
                                }
                            }

                            using (StreamWriter file = new StreamWriter("config.ini"))
                            {
                                file.Write(newConfig);
                            }
                        }

                        List <string> DirsInPath = new List <string>();
                        if (dirToScan.Contains("/"))
                        {
                            DirsInPath.AddRange(dirToScan.Split('/'));
                        }
                        else
                        {
                            DirsInPath.AddRange(dirToScan.Split('\\'));
                        }
                        if (File.Exists("exclude.txt"))
                        {
                            string[]      excluded  = File.ReadAllLines("exclude.txt");
                            List <string> toExclude = new List <string>(excluded);
                            toExclude.AddRange(DirsInPath);
                            parser = new IMDBFileNameParser(DirsInPath[DirsInPath.Count - 1], toExclude);
                            Console.WriteLine("If a directory or filename contains one of these values, it will be ignored:");
                            foreach (string toBeExcluded in toExclude)
                            {
                                Console.WriteLine(toBeExcluded);
                            }
                        }
                        else
                        {
                            parser = new IMDBFileNameParser(DirsInPath[DirsInPath.Count - 1], DirsInPath);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Directory does not exists :(: " + dirToSet);
                        ws.SendGlobalMessage("ERROR (DIRECTORY DOES NOT EXISTS) WHILE SETTING DIRECTORY: " + dirToSet);
                    }
                } catch
                {
                    Console.WriteLine("Trying to set a dir, but you probably made an error sending that message :X");
                    ws.SendGlobalMessage("ERROR (SYNTAX) WHILE SETTING DIRECTORY");
                }
            }
            else if (msg.Contains("GetDir"))
            {
                ws.SendGlobalMessage("DIR:" + dirToScan);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome To VixionServer!");
            Console.WriteLine("Please do read the following:");
            Console.WriteLine();
            Console.WriteLine("This server indexes all your files starting from the directory where I am launched. This takes a while!");
            Console.WriteLine("It tries to automatically detect which serie or movie the file belongs to... but be aware that this is never 100% accurate!");
            Console.WriteLine("This is a very early build of the server! Don't be scared by the random text flying by!");
            Console.WriteLine("To open the interface, go to: www.vixion.ga (temporary).");
            Console.WriteLine("If you launch for the first time, this server will show up. Click on it!");
            Console.WriteLine("To be sure that you select the correct server, click on the button that says: " + GetLocalIPAddress());
            Console.WriteLine("Thank you for using Vixion, I hope you enjoy using it :D");



            if (File.Exists("config.ini"))
            {
                string[] configData = File.ReadAllLines("config.ini");

                foreach (string config in configData)
                {
                    Console.WriteLine("ConfigData: " + config);
                    if (config.Contains("customDir = "))
                    {
                        string customDir = config.Split(new string[] { "customDir = " }, StringSplitOptions.None)[1];

                        if (Directory.Exists(customDir))
                        {
                            if (customDir.Contains('/'))
                            {
                                if (customDir[customDir.Length - 1] != '/')
                                {
                                    customDir = customDir + '/';
                                }
                            }
                            else if (customDir.Contains('\\'))
                            {
                                if (customDir[customDir.Length - 1] != '\\')
                                {
                                    customDir = customDir + '\\';
                                }
                            }
                            dirToScan = customDir;
                        }
                        else
                        {
                            dirToScan = Directory.GetCurrentDirectory();
                        }
                    }
                    else
                    {
                        dirToScan = Directory.GetCurrentDirectory();
                        using (StreamWriter file = new StreamWriter("config.ini"))
                        {
                            file.WriteLine("customDir = " + dirToScan + Environment.NewLine);
                        }
                    }
                }
            }
            else
            {
                dirToScan = Directory.GetCurrentDirectory();
                using (StreamWriter file = new StreamWriter("config.ini"))
                {
                    file.Write("customDir = " + dirToScan + Environment.NewLine);
                }
            }

            Console.WriteLine("Directory that will be scanned: " + dirToScan);

            List <string> DirsInPath = new List <string>();

            if (dirToScan.Contains("/"))
            {
                DirsInPath.AddRange(dirToScan.Split('/'));
            }
            else
            {
                DirsInPath.AddRange(dirToScan.Split('\\'));
            }

            if (File.Exists("exclude.txt"))
            {
                string[]      excluded  = File.ReadAllLines("exclude.txt");
                List <string> toExclude = new List <string>(excluded);
                parser = new IMDBFileNameParser(DirsInPath[DirsInPath.Count - 1], toExclude);
                Console.WriteLine("If a directory or filename contains one of these values, it will be ignored:");
                foreach (string toBeExcluded in toExclude)
                {
                    Console.WriteLine(toBeExcluded);
                }
            }
            else
            {
                parser = new IMDBFileNameParser(DirsInPath[DirsInPath.Count - 1], DirsInPath);
            }

            http = new HttpServer(port);
            http.SetWebHomeDir(Directory.GetCurrentDirectory() + @"\GUI");
            http.SetFileDir(dirToScan);
            http.Start();

            ws = new WebSocketServer(4655);
            ws.MessageReceived += new EventHandler <WebSocketEventArgs>(WsMessageReceived);
            ws.DebugMessage    += new EventHandler <WebSocketEventArgs>(WsDebugReceived);
            ws.Start();

            dicWithFiles = new ConcurrentDictionary <string, DataContainer>();
            Console.ReadLine();
        }