This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYiffDownloader.cs
123 lines (105 loc) · 4.35 KB
/
YiffDownloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Net;
namespace YiffVirus
{
class YiffDownloader
{
private readonly string FUNNY_API = "http://e621.net/posts.json?rating:e&limit=251";
private Random rand = new Random();
private int lastPostIndex = 0;
private dynamic jsonResponse;
public string downloadYiff(string folderPath, bool convertToIcon)
{
try
{
// Download all posts once
if(jsonResponse == null) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FUNNY_API);
request.UserAgent = "Yiff Virus (by Imf44#6363)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using StreamReader reader = new StreamReader(response.GetResponseStream());
jsonResponse = JsonConvert.DeserializeObject(reader.ReadToEnd());
}
for (int postIndex = lastPostIndex + 1; postIndex < jsonResponse["posts"].Count; postIndex++)
{
if (!jsonResponse["posts"][postIndex]["rating"].ToString().Equals("e"))
{
// Skipping SFW posts bc we only want the **VERY HALAL** ONES
continue;
}
string sampleUrl = jsonResponse["posts"][lastPostIndex = postIndex]["sample"]["url"].ToString();
int postId = jsonResponse["posts"][postIndex]["id"];
string yiffFilePath = (!convertToIcon && folderPath != null) ? folderPath + @"\" + postId + ".jpg" : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + postId + ".jpg";
using WebClient client = new WebClient();
client.DownloadFile(new Uri(sampleUrl), yiffFilePath);
if (convertToIcon)
{
IcoFromFile(yiffFilePath, folderPath, postId);
return $"owo_{postId}";
}
else
{
return sampleUrl;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return "owo";
}
public void IcoFromFile(string filePath, string folderPath, int postId)
{
using Image imageSex = Image.FromFile(filePath);
using Bitmap bitmap = new Bitmap(imageSex, new Size(256, 256));
SaveAsIcon(bitmap, folderPath + $"\\owo_{postId}.ico");
}
// Taken From: https://stackoverflow.com/a/11448060/368354
// & Fix from: https://stackoverflow.com/a/14157197
public static void SaveAsIcon(Bitmap SourceBitmap, string FilePath)
{
using FileStream FS = new FileStream(FilePath, FileMode.Create);
// ICO header
FS.WriteByte(0); FS.WriteByte(0);
FS.WriteByte(1); FS.WriteByte(0);
FS.WriteByte(1); FS.WriteByte(0);
// Image size
// Set to 0 for 256 px width/height
FS.WriteByte(0);
FS.WriteByte(0);
// Palette
FS.WriteByte(0);
// Reserved
FS.WriteByte(0);
// Number of color planes
FS.WriteByte(1); FS.WriteByte(0);
// Bits per pixel
FS.WriteByte(32); FS.WriteByte(0);
// Data size, will be written after the data
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
// Offset to image data, fixed at 22
FS.WriteByte(22);
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
// Writing actual data
SourceBitmap.Save(FS, System.Drawing.Imaging.ImageFormat.Png);
// Getting data length (file length minus header)
long Len = FS.Length - 22;
// Write it in the correct place
FS.Seek(14, SeekOrigin.Begin);
FS.WriteByte((byte)Len);
FS.WriteByte((byte)(Len >> 8));
FS.WriteByte((byte)(Len >> 16));
FS.WriteByte((byte)(Len >> 24));
FS.Close();
}
}
}