public async Task <PyObject> PythonResponseToBytearray(HttpResponseMessage resp) { //Debug.WriteLine("PythonResponseToBytearray"); byte[] content = await resp.Content.ReadAsByteArrayAsync(); using (Py.GIL()) { PyObject bytearray = PyScope.Eval("bytearray(" + content.Length + ")"); PythonCompat.WriteToBuffer(bytearray, content); return(bytearray); } }
public static object PythonObjectToManaged(dynamic pythonObj) { switch ((string)pythonObj.__class__.__name__) { case "str": return((string)pythonObj); case "int": long num = (long)pythonObj; if (num >= int.MaxValue || num <= int.MinValue) { return(num); } else { return((int)num); } case "float": return((float)pythonObj); case "bool": return((bool)pythonObj); case "dict": Dictionary <string, object> infoDict = new Dictionary <string, object>(); foreach (string key in pythonObj) { infoDict.Add(key, PythonCompat.PythonObjectToManaged(pythonObj[key])); } return(infoDict); case "list": List <object> infoDict2 = new List <object>(); foreach (dynamic val in pythonObj) { infoDict2.Add(PythonCompat.PythonObjectToManaged(val)); } return(infoDict2); case "generator": PyObject obj = (PyObject)pythonObj; dynamic list = new PyList(Runtime.PySequence_List(obj.Handle)); return(PythonCompat.PythonObjectToManaged(list)); case "NoneType": return(null); default: Debug.WriteLine("Unsupported type: " + (string)pythonObj.__class__.__name__); throw new NotSupportedException("Unsupported type: " + (string)pythonObj.__class__.__name__); } }
public PyObject CacheLoad(string section, string key, PyObject def = null) { //Debug.WriteLine("CacheLoad"); if (ytdl.Options.CacheDir == null) { return(def); } string cachepath = Path.Combine(ytdl.Options.CacheDir, "cache.json"); if (!File.Exists(cachepath)) { return(def); } using (var stream = File.OpenRead(cachepath)) { using (var reader = JsonDocument.Parse(stream)) { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); var xreader = new Utf8JsonReader(bytes); if (reader.RootElement.TryGetProperty(section, out var sectElem)) { if (sectElem.TryGetProperty(key, out var keyElem)) { var type = Type.GetType(keyElem.GetProperty("type").GetString()); var value = keyElem.GetProperty("value").GetRawText(); object managed = JsonSerializer.Deserialize(value, type); return(PythonCompat.ManagedObjectToPython(managed, type)); } } //Debug.WriteLine("def"); return(def); } } }
public async Task <HttpResponseMessage> PythonUrlOpen(PyObject pythonre) { //Debug.WriteLine("PythonUrlOpen"); string fullUrl; dynamic data; dynamic pheaders; string method; string datatype = null; using (Py.GIL()) { dynamic pythonreq = (dynamic)pythonre; dynamic fullUrl_x = pythonreq.__dict__.get("_full_url", null); fullUrl = (string)fullUrl_x; //string origin_req_host = (string)pythonreq.__dict__.get("origin_req_host", null); data = pythonreq.__dict__.get("_data", null); if (data != null) { datatype = (string)data.__class__.__name__; } pheaders = pythonreq.headers; //bool unverifiable = (bool)pythonreq.__dict__.get("unverifiable", false); dynamic method_x = pythonreq.get_method(); method = (string)method_x; } HttpRequestMessage req; switch (method) { default: case "GET": req = new HttpRequestMessage(HttpMethod.Get, fullUrl); break; case "POST": req = new HttpRequestMessage(HttpMethod.Post, fullUrl); break; case "HEAD": req = new HttpRequestMessage(HttpMethod.Head, fullUrl); break; case "PUT": req = new HttpRequestMessage(HttpMethod.Put, fullUrl); break; } Dictionary <string, string> reqHeaders = new Dictionary <string, string>(); using (Py.GIL()) { if (data != null) { if (datatype == "str") { Dictionary <string, string> formdata = new Dictionary <string, string>(); foreach (string kv in (data as string).Split('&')) { var s = kv.Split('='); formdata.Add(s[0], s[1]); } req.Content = new FormUrlEncodedContent(formdata); } else if (datatype == "bytearray") { req.Content = new ByteArrayContent((byte[])data); } } if (pheaders != null) { Dictionary <string, object> headers = PythonCompat.PythonObjectToManaged(pheaders); foreach (var header in headers) { reqHeaders.Add(header.Key, (string)header.Value); req.Headers.Add(header.Key, (string)header.Value); } } } req.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"); var resp = await ytdl.HttpClient.SendAsync(req).ConfigureAwait(false); // follow redirects (https => http redirects are ignored by .net core) while (resp.StatusCode == HttpStatusCode.Redirect) { HttpRequestMessage redirectReq = new HttpRequestMessage(); redirectReq.RequestUri = resp.Headers.Location; redirectReq.Content = req.Content; redirectReq.Method = req.Method; foreach (var h in reqHeaders) { redirectReq.Headers.Add(h.Key, h.Value); } resp = await ytdl.HttpClient.SendAsync(redirectReq).ConfigureAwait(false); } return(resp); }
/* * public void SetupPython() * { * using (Py.GIL()) * { * PyScope = Py.CreateScope(); * } * } * * public PyList GetAllPythonExtractors() * { * using (Py.GIL()) * { * PyScope.NewScope(); * PyScope.Exec("from .extractor import gen_extractor_classes"); * PyScope.Exec("extractors = gen_extractor_classes()"); * return (PyList)PyScope.Get("extractors"); * } * } * * public PyObject GetPythonInfoExtractor(string name) * { * using (Py.GIL()) * { * PyScope.NewScope(); * PyScope.Exec("from .extractor import get_info_extractor"); * PyScope.Exec("extractor = get_info_extractor(" + name + ")"); * return PyScope.Get("extractor"); * } * }*/ public void CacheStore(string section, string key, PyObject data) { //Debug.WriteLine("CacheStore"); if (ytdl.Options.CacheDir == null) { return; } string cachepath = Path.Combine(ytdl.Options.CacheDir, "cache.json"); dynamic dynData = data; object obj = (List <object>)PythonCompat.PythonObjectToManaged(dynData); string str = JsonSerializer.Serialize(obj, obj.GetType()); Debug.WriteLine(str); string readstream = string.Empty; if (File.Exists(cachepath)) { using (var sr = new StreamReader(cachepath)) { readstream = sr.ReadToEnd(); } } using (var writestream = File.Open(cachepath, FileMode.Create, FileAccess.Write)) { using (var writer = new Utf8JsonWriter(writestream, new JsonWriterOptions { Indented = true })) { writer.WriteStartObject(); bool sectionWritten = false; try { using (var reader = JsonDocument.Parse(readstream)) { foreach (var elem in reader.RootElement.EnumerateObject()) { if (elem.Name == section) { writer.WriteStartObject(section); foreach (var kElem in elem.Value.EnumerateObject()) { if (kElem.Name != key) { kElem.WriteTo(writer); } } writer.WriteStartObject(key); writer.WriteString("type", obj.GetType().FullName); writer.WritePropertyName("value"); JsonSerializer.Serialize(writer, obj, obj.GetType()); writer.WriteEndObject(); writer.WriteEndObject(); sectionWritten = true; } else { elem.WriteTo(writer); } } } } catch { } if (!sectionWritten) { writer.WriteStartObject(section); writer.WriteStartObject(key); writer.WriteString("type", obj.GetType().FullName); writer.WritePropertyName("value"); JsonSerializer.Serialize(writer, obj, obj.GetType()); writer.WriteEndObject(); writer.WriteEndObject(); sectionWritten = true; } writer.WriteEndObject(); } } }