Пример #1
0
		public bool Post(Advertisement ad)
		{
			AdvertService ads = new AdvertService(auth, captcha);

			return ads.Post(ad);
		}
Пример #2
0
		public Task<bool> PostAsync(Advertisement ad)
		{
			throw new NotImplementedException();
		}
Пример #3
0
		public bool Delete(Advertisement ad)
		{
			return Delete(ad.Id);
		}
Пример #4
0
		public bool Close(Advertisement ad)
		{
			return Close(ad.Id);
		}
Пример #5
0
        public Advertisement Get(string url)
        {
            string tmpurl = url;

            url = editUrl + url.Substring(url.IndexOf(".ru/") + 4).Replace('/', '_');

            AvitoWebClient web = new AvitoWebClient(auth.CredsCont);
            web.Encoding = Encoding.UTF8;
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(web.DownloadString(url));
            HtmlNode node = doc.DocumentNode.SelectSingleNode("//form[@id='f_item']");
            HtmlNode tmp;

            Advertisement ad = new Advertisement();
            ad.User = new UserInfo.User();
            ad.Location = new UserLocation();
            ad.Parameters = new Dictionary<int, int>();
            ad.Images = new List<Image>();

            // Id
            ad.Id = Int32.Parse(url.Substring(url.LastIndexOf('_') + 1));

            // URL
            ad.Url = tmpurl;

            // Title
            ad.Title = "";

            // Body
            ad.Body = node.SelectSingleNode("//*[@name='description']").InnerText;

            // Name
            //ad.User.Name = "������� ����";

            // Manager
            ad.User.Manager = node.SelectSingleNode("//*[@name='manager']").GetAttributeValue("value", "");

            // Phone
            ad.User.Phone = node.SelectSingleNode("//*[@name='phone']").GetAttributeValue("value", "");

            // Allow Mails
            ad.User.DenyEmails = node.SelectSingleNode("//*[@name='allow_mails']").GetAttributeValue("checked", "xxx") == "xxx";

            // Email
            //ad.User.Email = "*****@*****.**";

            // Location
            // Region

            ad.Location.Region = new Region
            {
                Id = Int32.Parse(node.SelectSingleNode("//*[@id='region']").GetAttributeValue("value", "0"))
            };

            // City
            ad.Location.City = new City
            {
                Id = Int32.Parse(node.SelectSingleNode("//*[@name='location_id']").GetAttributeValue("value", "0"))
            };

            // Metro
            tmp = node.SelectSingleNode("//*[@name='metro_id']//*[@selected]");
            if (tmp != null)
                ad.Location.Metro = new Metro
                                        {
                                            Id = Int32.Parse(tmp.GetAttributeValue("value", "0"))
                                        };

            // Road
            tmp = node.SelectSingleNode("//*[@name='road_id']//*[@selected]");
            if (tmp != null)
                ad.Location.Road = new Road
                {
                    Id = Int32.Parse(tmp.GetAttributeValue("value", "0"))
                };

            // District
            tmp = node.SelectSingleNode("//*[@name='district_id']//*[@selected]");
            if (tmp != null)
                ad.Location.District = new District
                {
                    Id = Int32.Parse(tmp.GetAttributeValue("value", "0"))
                };

            // End Location

            // Category
            ad.CategoryId = Int32.Parse(node.SelectSingleNode("//*[@name='category_id']").GetAttributeValue("value", ""));

            // Parameters
            HtmlNodeCollection par = node.SelectNodes("//*[contains(@name,'params')]");
            foreach (HtmlNode x in par)
            {
                string id = x.GetAttributeValue("name", "").Substring(7);
                id = id.Substring(0, id.IndexOf(']'));
                int value = Int32.Parse(x.SelectSingleNode(".//*[@selected]").GetAttributeValue("value", ""));
                ad.Parameters.Add(Int32.Parse(id), value);
            }

            // Geo

            tmp = node.SelectSingleNode("//*[@name='coords[lat]']");
            if (tmp != null)
            {
                ad.Coordinates = new GeoCoords
                {
                    Latitude = tmp.GetAttributeValue("value", ""),
                    Longitude = node.SelectSingleNode("//*[@name='coords[lng]']").GetAttributeValue("value", ""),
                    Zoom = node.SelectSingleNode("//*[@name='coords[zoom]']").GetAttributeValue("value", "")
                };
            }

            // Price
            ad.Price = Int32.Parse(node.SelectSingleNode("//*[@name='price']").GetAttributeValue("value", ""));

            // Images
            par = node.SelectSingleNode("//*[@name='images[]']").ParentNode.SelectNodes(".//img");
            foreach (HtmlNode x in par)
            {
                string[] arr = x.GetAttributeValue("src", "").Split('/');
                arr[3] = "640x480";
                byte[] data;
                try
                {
                    data = web.DownloadData(String.Join("/", arr));
                }
                catch (Exception e)
                {
                    return null;
                }
                MemoryStream ms = new MemoryStream();
                ms.Write(data, 0, data.Length);
                Image im = Image.FromStream(ms);
                ad.Images.Add(im);
            }

            // End

            return ad;
        }
Пример #6
0
        public bool Post(Advertisement ad)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();

            data.Add("seller_name", ad.User.Name);
            data.Add("manager", (ad.User.Manager == null) ? "" : ad.User.Manager);
            data.Add("phone", ad.User.Phone);
            data.Add("email", ad.User.Email);
            data.Add("allow_mails", (ad.User.DenyEmails) ? "1" : "");

            data.Add("location_id", ad.Location.City.Id.ToString());
            data.Add("metro_id", (ad.Location.Metro == null) ? "" : ad.Location.Metro.Id.ToString());
            data.Add("district_id", (ad.Location.District == null) ? "" : ad.Location.District.Id.ToString());
            data.Add("road_id", (ad.Location.Road == null) ? "" : ad.Location.Road.Id.ToString());

            data.Add("category_id", ad.CategoryId.ToString());
            foreach (KeyValuePair<int, int> x in ad.Parameters)
            {
                data.Add("params[" + x.Key.ToString() + "]", x.Value.ToString());
            }

            data.Add("title", ad.Title);
            data.Add("description", ad.Body);

            if (ad.Coordinates != null)
            {
                data.Add("coords[lat]", ad.Coordinates.Latitude);
                data.Add("coords[lng]", ad.Coordinates.Longitude);
                data.Add("coords[zoom]", ad.Coordinates.Zoom);
            }

            data.Add("price", ad.Price.ToString());

            data.Add("service_code", "free");
            data.Add("main_form_submit", "���������� � ������� �������� �������");

            foreach (Image image in ad.Images)
                data.Add("images[]", PostImage(image));

            AvitoWebClient web = new AvitoWebClient(auth.CredsCont);
            web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            web.Headers.Add("Referer", url1);

            try
            {
                web.UploadString(url1, Stringify(data));
            }
            catch (Exception ex)
            {
                throw new AvitoNetworkException();
            }
            if (!web.ResponseUrl.Contains("confirm"))
                throw new WrongAdvertisementException();

            string date = ((int)((DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds)).ToString();

            HttpWebRequest req = HttpWebRequest.Create(captchaUrl + date) as HttpWebRequest;
            req.Referer = url2;
            req.CookieContainer = auth.CredsCont;
            HttpWebResponse res;
            try
            {
                res = req.GetResponse() as HttpWebResponse;
            }
            catch (Exception ex)
            {
                return false;
            }
            Image im = new Bitmap(res.GetResponseStream());

            string cap;
            try
            {
                cap = captcha.GetCaptcha(im);
            }
            catch (Exception ex)
            {
                return false;
            }

            data.Clear();
            data.Add("captcha", cap);
            data.Add("service", "0");
            data.Add("subscribe-position", "0");

            web.Headers.Add("Referer", url2);
            web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            try
            {
                web.UploadString(url2, Stringify(data));
            }
            catch (Exception ex)
            {
                throw new AvitoNetworkException();
            }
            if (!web.ResponseUrl.Contains("finish"))
                throw new CaptchaException();
            return true;
        }
Пример #7
0
        public List<Advertisement> GetClosed()
        {
            List<Advertisement> list = new List<Advertisement>();

            AvitoWebClient web = new AvitoWebClient(auth.CredsCont);
            web.Encoding = Encoding.UTF8;
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(web.DownloadString(oldItemsUrl));
            HtmlNodeCollection all = doc.DocumentNode.SelectNodes("//div[contains(@class,'item')]");

            if (all != null)
            {
                foreach (HtmlNode node in all)
                {
                    Advertisement ad = new Advertisement();
                    ad.Id = Int32.Parse(node.SelectSingleNode(".//input").GetAttributeValue("value", "0"));
                    ad.Url = baseUrlSecure + node.SelectSingleNode(".//a[starts-with(@name,'item_')]").GetAttributeValue("href", "");
                    ad.Title = node.SelectSingleNode(".//a[starts-with(@name,'item_')]").InnerText;
                    ad = Get(ad.Url);
                    list.Add(ad);
                }
            }

            return list;
        }