public void FindCast()     //filmin castını bulan method
        {
            WebClient webClient = new WebClient();
            string    result    = webClient.DownloadString("https://www.imdb.com/" + this.MovieId);

            Boolean control = false;                 //ayn isim var mı kontrolunde kullanılıyor
            string  roleResult, castUrl;
            string  name, role = "";

            int startsWith = result.IndexOf("credit_summary_item");  //herbir castın başında bu item olduğundan başlangıç noktası bu olarak alınır

            result = result.Substring(startsWith);
            int endsWith = result.IndexOf("<script>");

            result = result.Substring(0, endsWith);

            while (result.IndexOf("credit_summary_item") != -1)  //"credit_summary_item" bulamadığında döngü sonlanır
            {
                startsWith = result.IndexOf("credit_summary_item");
                result     = result.Substring(startsWith);
                endsWith   = result.IndexOf("/div");
                roleResult = result.Substring(0, endsWith);
                result     = result.Substring(endsWith);

                if (roleResult.IndexOf("Director") != -1)
                {
                    role = "Director";
                }
                else if (roleResult.IndexOf("Writer") != -1)
                {
                    role = "Writer";
                }
                else if (roleResult.IndexOf("Star") != -1)
                {
                    role = "Star";
                }

                roleResult = roleResult.Substring(roleResult.IndexOf(role));

                do                     //aynı role içinde birden fazla kişi varsa diye ilki ismi yazıp , varsa devam eder
                {
                    endsWith   = roleResult.IndexOf(">");
                    roleResult = roleResult.Substring(endsWith + 1);

                    startsWith = roleResult.IndexOf('"');
                    roleResult = roleResult.Substring(startsWith + 1);
                    endsWith   = roleResult.IndexOf('?');
                    castUrl    = roleResult.Substring(0, endsWith); // castın url i

                    //iki kez >(tag kapama) geçtikten sonra castın ismi vardır

                    startsWith = roleResult.IndexOf(">");
                    roleResult = roleResult.Substring(startsWith + 1);
                    endsWith   = roleResult.IndexOf("<");

                    name = roleResult.Substring(0, endsWith);            //tag kapama ile tag açma arasındaki string cast ismidir
                    name = name.Replace("ö", "ö");

                    roleResult = roleResult.Substring(endsWith + 1);

                    Role role1 = Role.none;
                    if (role == "Director" || role == "Creator")
                    {
                        role1 = Role.director;
                    }
                    else if (role == "Writer")
                    {
                        role1 = Role.writer;
                    }
                    else if (role == "Star")
                    {
                        role1 = Role.star;
                    }

                    for (int i = 0; i < Casts.Count; i++)           //aynı kişi varsa sadece rolüne ekleme yapar
                    {
                        if (Casts[i].CastId == castUrl)
                        {
                            Casts[i].Roles.Add(role1);
                            control = true;                    //aynı kisi varsa kontrolunu yapar
                            break;
                        }
                    }
                    if (control == false)
                    {
                        string first = name;
                        string last  = "";
                        if (name.Contains(" "))
                        {
                            first = name.Substring(0, name.IndexOf(" "));
                            last  = name.Substring(name.IndexOf(" ") + 1);
                        }

                        Cast cast = new Cast(first, last, castUrl);
                        cast.GetInfo();
                        cast.Roles.Add(role1);

                        Casts.Add(cast);
                    }
                }while (roleResult.IndexOf(",") != -1);

                control = false;
            }
        }
Ejemplo n.º 2
0
        private void FormMovie_Load(object sender, EventArgs e)
        {
            pbxPoster.SizeMode = PictureBoxSizeMode.StretchImage;
            this.Text          = _Movie.Name;

            rchbxRate.Text        = _Movie.Rate.ToString();
            rchbxDescription.Text = _Movie.Description;
            rchbxDate.Text        = _Movie.Date.ToString();

            if (_Movie.Poster != null)
            {
                pbxPoster.Load(_Movie.Poster);
            }
            else
            {
                pbxPoster.Load("https://us.123rf.com/450wm/pavelstasevich/pavelstasevich1811/pavelstasevich181101065/112815953-stock-vector-no-image-available-icon-flat-vector.jpg?ver=6");
            }

            SqlConnection cnn = new SqlConnection(@"server=.\MSSQLServer01;database=Imdb;trusted_connection=true");

            SqlDataAdapter da = new SqlDataAdapter("select * from MovieCastMapping", cnn);

            DataSet dtMovie = new DataSet();

            da.Fill(dtMovie);

            SqlDataAdapter da1 = new SqlDataAdapter("select * from Person", cnn);

            DataSet ds = new DataSet();

            da1.Fill(ds);

            bool hasMap = false;

            foreach (DataRow row in dtMovie.Tables[0].Rows)
            {
                if (row[dtMovie.Tables[0].Columns.IndexOf("MovieID")].ToString() == _Movie.ImdbId)
                {
                    hasMap = true;

                    foreach (DataRow rowCast in ds.Tables[0].Rows)
                    {
                        if (row[dtMovie.Tables[0].Columns.IndexOf("PersonID")].ToString() == rowCast[ds.Tables[0].Columns.IndexOf("PersonID")].ToString())
                        {
                            Cast cast = new Cast();
                            cast.FirstName   = rowCast[ds.Tables[0].Columns.IndexOf("FirstName")].ToString();
                            cast.LastName    = rowCast[ds.Tables[0].Columns.IndexOf("LastName")].ToString();
                            cast.CastId      = rowCast[ds.Tables[0].Columns.IndexOf("PersonID")].ToString();
                            cast.Description = rowCast[ds.Tables[0].Columns.IndexOf("Description")].ToString();
                            cast.PosterLink  = rowCast[ds.Tables[0].Columns.IndexOf("PosterLink")].ToString();
                            cast.BirthDate   = Convert.ToDateTime(rowCast[ds.Tables[0].Columns.IndexOf("BirthDate")]);
                            cast.City        = rowCast[ds.Tables[0].Columns.IndexOf("City")].ToString();
                            cast.State       = rowCast[ds.Tables[0].Columns.IndexOf("State")].ToString();
                            cast.Country     = rowCast[ds.Tables[0].Columns.IndexOf("Country")].ToString();

                            SqlDataAdapter dar = new SqlDataAdapter("select * from Role", cnn);

                            DataTable dtMovieRole = new DataTable();
                            dar.Fill(dtMovieRole);

                            foreach (DataRow rowRole in dtMovieRole.Rows)
                            {
                                if (row[dtMovie.Tables[0].Columns.IndexOf("RoleID")].ToString() == rowRole[dtMovieRole.Columns.IndexOf("RoleID")].ToString())
                                {
                                    if (rowRole[dtMovieRole.Columns.IndexOf("RoleName")].ToString().Replace(" ", "").ToLower() == "director")
                                    {
                                        lstbxDirector.Items.Add(cast);
                                    }
                                    else if (rowRole[dtMovieRole.Columns.IndexOf("RoleName")].ToString().Replace(" ", "").ToLower() == "writer")
                                    {
                                        lstbxWriter.Items.Add(cast);
                                    }
                                    else if (rowRole[dtMovieRole.Columns.IndexOf("RoleName")].ToString().Replace(" ", "").ToLower() == "star")
                                    {
                                        lstbxStar.Items.Add(cast);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (hasMap == false)
            {
                _Movie.FindCast();

                foreach (Cast cast in _Movie.Casts)
                {
                    bool isthere = false;
                    foreach (DataRow row in dtMovie.Tables[0].Rows)
                    {
                        if (row[dtMovie.Tables[0].Columns.IndexOf("PersonID")].ToString() == cast.CastId)
                        {
                            isthere = true;
                        }
                    }
                    if (isthere == false)
                    {
                        cast.GetInfo();
                        DataRow rowPerson = ds.Tables[0].NewRow();
                        rowPerson[ds.Tables[0].Columns.IndexOf("FirstName")]   = cast.FirstName;
                        rowPerson[ds.Tables[0].Columns.IndexOf("LastName")]    = cast.LastName;
                        rowPerson[ds.Tables[0].Columns.IndexOf("PersonID")]    = cast.CastId;
                        rowPerson[ds.Tables[0].Columns.IndexOf("Description")] = cast.Description;
                        rowPerson[ds.Tables[0].Columns.IndexOf("PosterLink")]  = cast.PosterLink;
                        rowPerson[ds.Tables[0].Columns.IndexOf("BirthDate")]   = cast.BirthDate;
                        rowPerson[ds.Tables[0].Columns.IndexOf("City")]        = cast.City;
                        rowPerson[ds.Tables[0].Columns.IndexOf("State")]       = cast.State;
                        rowPerson[ds.Tables[0].Columns.IndexOf("Country")]     = cast.Country;

                        ds.Tables[0].Rows.Add(rowPerson);
                    }

                    //if (!dtMovie.Tables[0].Rows.Contains(_Movie.ImdbId))
                    //{

                    foreach (Role role in cast.Roles)
                    {
                        DataRow row = dtMovie.Tables[0].NewRow();
                        row[dtMovie.Tables[0].Columns.IndexOf("PersonID")] = cast.CastId;
                        row[dtMovie.Tables[0].Columns.IndexOf("MovieID")]  = _Movie.ImdbId;

                        if (role.ToString() == "director")
                        {
                            lstbxDirector.Items.Add(cast);
                            row[dtMovie.Tables[0].Columns.IndexOf("RoleID")] = 1;
                        }
                        else if (role.ToString() == "writer")
                        {
                            lstbxWriter.Items.Add(cast);
                            row[dtMovie.Tables[0].Columns.IndexOf("RoleID")] = 2;
                        }
                        else if (role.ToString() == "star")
                        {
                            lstbxStar.Items.Add(cast);
                            row[dtMovie.Tables[0].Columns.IndexOf("RoleID")] = 3;
                        }
                        else
                        {
                            row[dtMovie.Tables[0].Columns.IndexOf("RoleID")] = 4;
                        }


                        dtMovie.Tables[0].Rows.Add(row);
                    }
                    new SqlCommandBuilder(da1);
                    da1.Update(ds);
                    new SqlCommandBuilder(da);
                    da.Update(dtMovie);
                    //   }
                }
            }
            cnn.Close();
        }