|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Net; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Newtonsoft.Json; |
| 8 | + |
| 9 | +namespace Stardrop.Utilities |
| 10 | +{ |
| 11 | + public static class NexusHelper |
| 12 | + { |
| 13 | + // Get Session_cookie and place here. (Click on Slow Download and check network tab in browser) |
| 14 | + private static string session_cookie = ""; |
| 15 | + private static string stardew_game_id = "1303"; |
| 16 | + private static string nexus_download_url = "https://www.nexusmods.com/Core/Libs/Common/Managers/Downloads?GenerateDownloadUrl"; |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + public static string MakeDownloadLink(string refererUrl, string fileId) |
| 21 | + { |
| 22 | + session_cookie = File.ReadAllText("session.txt"); |
| 23 | + string downloadUrl = null; |
| 24 | + HttpClientHandler handler = new HttpClientHandler() |
| 25 | + { |
| 26 | + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate |
| 27 | + }; |
| 28 | + |
| 29 | + var request = (HttpWebRequest)WebRequest.Create(nexus_download_url); |
| 30 | + request.Accept = "*/*"; |
| 31 | + request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"; |
| 32 | + |
| 33 | + request.Headers.Add("Accept-Encoding", "gzip, deflate, br, zstd"); |
| 34 | + request.Headers.Add("Accept-Language", "en,de;q=0.9,de-DE;q=0.8,en-US;q=0.7"); |
| 35 | + request.Headers.Add("Cache-Control", "no-cache"); |
| 36 | + request.Headers.Add("Origin", "https://www.nexusmods.com"); |
| 37 | + request.Referer = refererUrl; |
| 38 | + request.Headers.Add("X-Requested-With", "XMLHttpRequest"); |
| 39 | + request.Headers.Add("Cookie", session_cookie); |
| 40 | + request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
| 41 | + |
| 42 | + var postData = "fid=" + fileId; |
| 43 | + postData += "&game_id=" + stardew_game_id; |
| 44 | + var data = Encoding.ASCII.GetBytes(postData); |
| 45 | + |
| 46 | + request.Method = "POST"; |
| 47 | + request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; |
| 48 | + request.ContentLength = data.Length; |
| 49 | + |
| 50 | + using (var stream = request.GetRequestStream()) |
| 51 | + { |
| 52 | + stream.Write(data, 0, data.Length); |
| 53 | + } |
| 54 | + try |
| 55 | + { |
| 56 | + var response = (HttpWebResponse)request.GetResponse(); |
| 57 | + |
| 58 | + if (response.StatusCode == HttpStatusCode.OK) |
| 59 | + { |
| 60 | + string responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd(); |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + var responseData = JsonConvert.DeserializeObject<dynamic>(responseBody); |
| 65 | + |
| 66 | + downloadUrl = responseData?.url; |
| 67 | + |
| 68 | + if (string.IsNullOrEmpty(downloadUrl)) |
| 69 | + { |
| 70 | + throw new Exception("No download URL found in the response."); |
| 71 | + } |
| 72 | + } |
| 73 | + catch (Newtonsoft.Json.JsonException) |
| 74 | + { |
| 75 | + throw new Exception($"Failed to parse the response as JSON from {refererUrl}"); |
| 76 | + } |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + throw new Exception($"Request failed for {refererUrl} with status code {response.StatusCode}"); |
| 81 | + } |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + throw new Exception($"Error during POST request: {ex.Message}"); |
| 86 | + } |
| 87 | + |
| 88 | + return downloadUrl; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | +} |
0 commit comments