Beispiel #1
0
        private void button8_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(() =>
            {
                String token = textBox4.Text;
                String url   = getURL(textBox2.Text, textBox4.Text + "/photos?limit=100");
                richTextBox1.Invoke(new MethodInvoker(() =>
                {
                    richTextBox1.AppendText("Đang lấy danh sách ID");
                }));
                List <String> list2 = new List <string>();
                getListIDAllbum(token, url, list2);
                richTextBox1.Invoke(new MethodInvoker(() =>
                {
                    richTextBox1.AppendText("Đã lấy xong danh sách id, Đang lấy danh sách image");
                }));
                List <ImageID.Image> list      = new List <ImageID.Image>();
                UpdataToPhotos.Rootobject root = new UpdataToPhotos.Rootobject();
                List <UpdataToPhotos.Newmediaitem> listItem = new List <UpdataToPhotos.Newmediaitem>();
                int i = 0;
                int j = 0;
                foreach (String id in list2)
                {
                    ImageID.Image[] image = getUrlFromID(id);
                    String tokenImg       = "";
                    try
                    {
                        tokenImg = uploadImage(image[0].source, textBox3.Text);
                    }
                    catch (Exception ex)
                    {
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.SelectionFont  = new Font(richTextBox1.Font, FontStyle.Bold);
                            richTextBox1.SelectionColor = Color.Red;
                            richTextBox1.AppendText("\nLỗi ảnh: " + id);
                            richTextBox1.SelectionColor = Color.Black;
                        }));
                        File.AppendAllText("log.lg", String.Format("Error:  Upload image to Google ID: {0}\tTime: {1} {2}\n", id, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        break;
                    }

                    File.AppendAllText("log.lg", String.Format("Status:  Upload image to Google ID: {0}\tTime: {1} {2}\n", id, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                    UpdataToPhotos.Newmediaitem newItem = new UpdataToPhotos.Newmediaitem();
                    newItem.simpleMediaItem             = new UpdataToPhotos.Simplemediaitem();
                    newItem.simpleMediaItem.fileName    = id + ".jpg";
                    newItem.simpleMediaItem.uploadToken = tokenImg;
                    newItem.description = "Đây là ảnh của Allbum ID: " + id;
                    listItem.Add(newItem);
                    richTextBox1.Invoke(new MethodInvoker(() =>
                    {
                        richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
                        richTextBox1.AppendText("\nXong ảnh: " + id);
                    }));
                    i++;
                    j++;
                    if (j >= 20)
                    {
                        root.newMediaItems = listItem.ToArray();
                        HttpRequest http2  = getRequest("", "");
                        http2.AddHeader("Authorization", "Bearer " + textBox3.Text);
                        String post     = JsonConvert.SerializeObject(root);
                        String dataOut2 = http2.Post("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", post, "application/json").ToString();
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.AppendText("\nKẾT THÚC 100 ẢNH. TIẾP TỤC 100 ẢNH");
                        }));
                        File.AppendAllText("log.lg", String.Format("Status:  END Part\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        root = new UpdataToPhotos.Rootobject();
                        j    = 0;
                    }
                }
                root.newMediaItems = listItem.ToArray();
                File.AppendAllText("log.lg", String.Format("Status:  Push to Google Photos\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                HttpRequest http = getRequest("", "");
                http.AddHeader("Authorization", "Bearer " + token);
                try
                {
                    String dataOut = http.Post("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", JsonConvert.SerializeObject(root), "application/json").ToString();
                }
                catch (Exception ex)
                {
                    richTextBox1.Invoke(new MethodInvoker(() =>
                    {
                        richTextBox1.AppendText("\nKẾT THÚC: Error");
                    }));
                    File.AppendAllText("log.lg", String.Format("Error:  END ALL\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                    return;
                }

                richTextBox1.Invoke(new MethodInvoker(() =>
                {
                    richTextBox1.AppendText("\nKẾT THÚC");
                }));
                File.AppendAllText("log.lg", String.Format("Status:  END ALL\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
            });

            t.IsBackground = true;
            t.Start();
        }
Beispiel #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(() =>
            {
                try
                {
                    String[] idLi = textBox1.Text.Split('|');
                    foreach (String idItem in idLi)
                    {
                        String token = textBox3.Text;
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.AppendText("\nBắt đầu lấy thông tin bài viết: " + idItem);
                        }));
                        File.AppendAllText("log.lg", String.Format("Status: Start get ID image of facebook post: ID {0}\tTime: {1} {2}\n", idItem, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        List <String> idL              = getListIDImage(idItem);
                        List <ImageID.Image> list      = new List <ImageID.Image>();
                        UpdataToPhotos.Rootobject root = new UpdataToPhotos.Rootobject();
                        root.newMediaItems             = new UpdataToPhotos.Newmediaitem[100];
                        int i = 0;
                        File.AppendAllText("log.lg", String.Format("Status: End get ID image of facebook post\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        File.AppendAllText("log.lg", String.Format("Status: Start upload image to Google\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.AppendText("\nBắt đầu tải ảnh lên Google");
                        }));
                        foreach (String id in idL)
                        {
                            ImageID.Image[] image = getUrlFromID(id);
                            String tokenImg       = uploadImage(image[0].source, token);
                            UpdataToPhotos.Newmediaitem newItem = new UpdataToPhotos.Newmediaitem();
                            newItem.simpleMediaItem             = new UpdataToPhotos.Simplemediaitem();
                            newItem.simpleMediaItem.fileName    = id + ".jpg";
                            newItem.simpleMediaItem.uploadToken = tokenImg;
                            newItem.description   = "Đây là ảnh của bài việt ID: " + id;
                            root.newMediaItems[i] = newItem;
                            richTextBox1.Invoke(new MethodInvoker(() =>
                            {
                                richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
                                richTextBox1.AppendText("\nXong ảnh: " + id);
                            }));
                            i++;
                        }
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
                            richTextBox1.AppendText("\nKết thúc tải ảnh lên Google");
                        }));
                        File.AppendAllText("log.lg", String.Format("Status: End upload image to Google\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.AppendText("\nTạo ảnh trên Google Photos");
                        }));
                        File.AppendAllText("log.lg", String.Format("Status: Push image to Google Photos\tTime: {0} {1}\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        HttpRequest http = getRequest("", "");
                        http.AddHeader("Authorization", "Bearer " + token);
                        String dataOut = http.Post("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", JsonConvert.SerializeObject(root), "application/json").ToString();
                        richTextBox1.Invoke(new MethodInvoker(() =>
                        {
                            richTextBox1.AppendText("\nXong: " + idItem);
                        }));
                        File.AppendAllText("log.lg", String.Format("Status: End: {0}\tTime: {1} {2}\n", idItem, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                        Thread.Sleep(new Random().Next(3000, 5000));
                    }
                    richTextBox1.Invoke(new MethodInvoker(() =>
                    {
                        richTextBox1.AppendText("\nKẾT THÚC");
                    }));
                }
                catch (Exception ex)
                {
                    richTextBox1.Invoke(new MethodInvoker(() =>
                    {
                        richTextBox1.AppendText("\nLỗi: " + ex.Message);
                    }));
                    File.AppendAllText("log.lg", String.Format("Status: End: Error: {0}\tTime: {1} {2}\n", ex.Message, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
                }
            });

            th.IsBackground = true;
            th.Start();
        }