%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /data/down/pc_is/Desktop/vintedDown/
Upload File :
Create Path :
Current File : //data/down/pc_is/Desktop/vintedDown/User.cs

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace vintedDown
{
	public class User
	{
#if DEBUG
		private const string _BASE_FOLDER = "c:\\tmp\\vinted";
#else
		private const string _BASE_FOLDER = "/www/varak.cloud/img.varak.cloud/img/vinted_down3";
#endif
		private string existingItemsFile;
		private string csrf;
		private string anonId;

		private List<ItemPhoto> photos;
		private CookieContainer cookieContainer;

		private List<long> existingItemIds;

		public User(string baseAddress, string id) 
		{
			BaseAddress = baseAddress;
			Id = id;
			UserId = id;

			photos = new List<ItemPhoto>();
			if (!Directory.Exists($"{_BASE_FOLDER}{BaseAddress}/{Id}"))
			{
				Directory.CreateDirectory($"{_BASE_FOLDER}/{BaseAddress}/{Id}");
			}
			existingItemsFile = $"{_BASE_FOLDER}/{BaseAddress}/{UserId}/items.json";
			LoadDone();
		}

		public User(string baseAddress, string id, bool isOneItem)
		{
			BaseAddress = baseAddress;
			Id = id;

			IsOneItem = isOneItem;
		}

		public string BaseAddress { get; set; }

		public string Id { get; set; }

		public string UserId { get; set; }

		public bool IsOneItem { get; set; } = false;

		public async Task LoadItems()
		{
			await GetAuthCookies();

			if (IsOneItem)
			{
				await LoadSingleItem();
				return;
			}

			//var baseAddr = "https://" + BaseAddress + "/api/v2/users/" + Id + "/items?page={0}&per_page=20&order=relevance";
			string baseAddr = "https://www.vinted.fr/api/v2/wardrobe/" + Id + "/items?page={0}&per_page=96&order=relevance";
			var hasPage = true;
			for(var i = 1; hasPage; i++)
			{
				var jsonString = await GetAsync(string.Format(baseAddr, i));
				var data = JsonConvert.DeserializeObject<ApiUser.Root>(jsonString, new JsonSerializerSettings() { Error = (sender, error) => error.ErrorContext.Handled = true });
				foreach(var item in data.items)
				{
					if (existingItemIds.Any(x => x == item.id))
						continue;

					foreach(var photo in item.photos)
					{
						photos.Add(new ItemPhoto { ItemId = item.id, PhotoUrl = photo.full_size_url });
					}
				}
				if(i == data.pagination.total_pages)
				{
					hasPage = false;
				}
			}
		}

		public async Task SaveItems(string path = "")
		{
			foreach (var photo in photos)
			{
				await GetAsyncSave(photo);
				existingItemIds.Add(photo.ItemId);
			}

			await SaveDone();
		}

		private async Task LoadSingleItem()
		{
			var baseAddr = "https://" + BaseAddress + "/api/v2/items/" + Id + "?localize=false";
			var jsonString = await GetAsync(baseAddr);
			var data = JsonConvert.DeserializeObject<ApiItemNew.Root>(jsonString, new JsonSerializerSettings() { Error = (sender, error) => error.ErrorContext.Handled = true });
			if (data == null) return;

			photos = new List<ItemPhoto>();
			UserId = data.item.user_id.ToString();
			if (!Directory.Exists($"{_BASE_FOLDER}/{BaseAddress}/{UserId}"))
			{
				Directory.CreateDirectory($"{_BASE_FOLDER}/{BaseAddress}/{UserId}");
			}
			existingItemsFile = $"{_BASE_FOLDER}/{BaseAddress}/{UserId}/items.json";
			LoadDone();

			foreach (var photo in data.item.photos)
			{
				photos.Add(new ItemPhoto {ItemId = data.item.id, PhotoUrl = photo.full_size_url});
			}
		}

		private void LoadDone()
		{
			if (File.Exists(existingItemsFile))
			{
				var data = File.ReadAllText(existingItemsFile);
				existingItemIds = JsonConvert.DeserializeObject<List<long>>(data,
					                  new JsonSerializerSettings()
						                  { Error = (sender, error) => error.ErrorContext.Handled = true }) ??
				                  new List<long>();
			}
			else
			{
				existingItemIds = new List<long>();
			}
		}

		private Task SaveDone()
		{
			var json = JsonConvert.SerializeObject(existingItemIds, Formatting.Indented);
			return File.WriteAllTextAsync(existingItemsFile, json);
		}

		private async Task GetAuthCookies()
		{
			cookieContainer = new CookieContainer();
			var uri = $"https://{BaseAddress}/member/{Id}";
			Console.WriteLine("[{0}] GET {1}", DateTime.Now.ToString("u"), uri);
			using var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
			var _client = new HttpClient(handler);
			_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
			using HttpResponseMessage response = await _client.GetAsync(uri);
			var txt = await response.Content.ReadAsStringAsync();
			int tokenIdx = txt.IndexOf("CSRF_TOKEN");
			csrf = txt.Substring(tokenIdx + 15, 36);
			tokenIdx = txt.IndexOf("\\\"anon_id\\\"");
			anonId = txt.Substring(tokenIdx + 14, 36);
		}

		private async Task<string> GetAsync(string uri)
		{
			Console.WriteLine("[{0}] GET {1}", DateTime.Now.ToString("u"), uri);
			using var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
			var _client = new HttpClient(handler);
			_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
			_client.DefaultRequestHeaders.Add("X-CSRF-Token", csrf);
			_client.DefaultRequestHeaders.Add("X-Money-Object", "true");
			_client.DefaultRequestHeaders.Referrer = new Uri($"https://{BaseAddress}/member/{Id}");
			_client.DefaultRequestHeaders.Add("x-Anon-Id", anonId);
			using HttpResponseMessage response = await _client.GetAsync(uri);

			return await response.Content.ReadAsStringAsync();
		}

		private async Task GetAsyncSave(ItemPhoto item)
		{
			Console.WriteLine("[{0}] GET {1}", DateTime.Now.ToString("u"), item.PhotoUrl);
			using var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
			var _client = new HttpClient(handler);
			_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
			using HttpResponseMessage response = await _client.GetAsync(item.PhotoUrl);

			var data = await response.Content.ReadAsByteArrayAsync();
			var bits = item.PhotoUrl.Split("/", StringSplitOptions.RemoveEmptyEntries);
			var filename = item.ItemId.ToString("x16") + "_" + GetFileNameFromUrl(item.PhotoUrl).Split(".")[0] + "_" + bits[3] + ".jpg";

			File.WriteAllBytes($"{_BASE_FOLDER}/{BaseAddress}/{UserId}/" + filename, data);
		}

		private string GetFileNameFromUrl(string url)
		{
			Uri SomeBaseUri = new Uri("http://canbeanything");
			Uri uri;
			if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
				uri = new Uri(SomeBaseUri, url);

			return Path.GetFileName(uri.LocalPath);
		}

	}
}

Zerion Mini Shell 1.0