private static void savePostText(dataDownloader.postData post) { if (!Directory.Exists(downloadFolder)) { Directory.CreateDirectory(downloadFolder); } if (!Directory.Exists($"{downloadFolder}/{post.subreddit}")) { Directory.CreateDirectory($"{downloadFolder}/{post.subreddit}"); } string finalName = post.postTitle; int nextNameNum = 1; while (File.Exists($"{downloadFolder}/{post.subreddit}/{finalName}.txt")) { finalName = post.postTitle + nextNameNum; nextNameNum++; } if (finalName.Length > maxFilenameSize) { finalName = finalName.Remove(maxFilenameSize); } finalName = removeIllegalChars(finalName); File.WriteAllText($"{downloadFolder}/{post.subreddit}/{finalName}.txt", post.postBody); }
private static void downloadMedia(dataDownloader.postData media, bool downloadText) { switch (media.type) { case dataDownloader.postTypes.image: Console.WriteLine($"Downloading file {media.url}"); downloadImage(media); break; case dataDownloader.postTypes.gif: Console.WriteLine($"Downloading file {media.url}"); downloadGfycatVideo(media); break; case dataDownloader.postTypes.imgur: Console.WriteLine($"Downloading file(s) {media.url}"); imgurDownloader.downloadImgurFile(media.url, $"{downloadFolder}/{media.subreddit}", media.postTitle); break; case dataDownloader.postTypes.text: if (downloadText) { Console.WriteLine($"Saving text post {media.postTitle}"); savePostText(media); } break; } }
private static void downloadImage(dataDownloader.postData imageData) { if (imageData.url.EndsWith(".gifv")) { imageData.url = imageData.url.Remove(imageData.url.Length - ".gifv".Length); imageData.url += ".mp4"; } HttpResponseMessage response = _client.GetAsync(imageData.url).Result; if (response.StatusCode != HttpStatusCode.OK) { return; } byte[] data = response.Content.ReadAsByteArrayAsync().Result; if (data.Length == 0) { return; } if (!Directory.Exists($"{downloadFolder}/{imageData.subreddit}")) { Directory.CreateDirectory($"{downloadFolder}/{imageData.subreddit}"); } string finalName = imageData.postTitle; int nextNameNum = 1; while (File.Exists($"{downloadFolder}/{imageData.subreddit}/{finalName}{getFileExtensionFromIRedditUrl(imageData.url)}")) { finalName = imageData.postTitle + nextNameNum; nextNameNum++; } finalName = removeIllegalChars(finalName); if (finalName.Length > maxFilenameSize) { finalName = finalName.Remove(maxFilenameSize); } finalName += getFileExtensionFromIRedditUrl(imageData.url); File.WriteAllBytes($"{downloadFolder}/{imageData.subreddit}/{finalName}", data); }
private static void downloadGfycatVideo(dataDownloader.postData gifData) { string url = gifData.url; url = url.Insert("https://".Length, "giant."); url += ".mp4"; HttpResponseMessage response = _client.GetAsync(url).Result; if (response.StatusCode != HttpStatusCode.OK) { return; } byte[] data = response.Content.ReadAsByteArrayAsync().Result; if (!Directory.Exists($"{downloadFolder}/{gifData.subreddit}")) { Directory.CreateDirectory($"{downloadFolder}/{gifData.subreddit}"); } string finalName = gifData.postTitle; int nextNameNum = 1; while (File.Exists($"{downloadFolder}/{gifData.subreddit}/{finalName}.mp4")) { finalName = gifData.postTitle + nextNameNum; nextNameNum++; } if (finalName.Length > maxFilenameSize) { finalName = finalName.Remove(maxFilenameSize); } finalName = removeIllegalChars(finalName); File.WriteAllBytes($"{downloadFolder}/{gifData.subreddit}/{finalName}.mp4", data); }