-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructures.cs
More file actions
157 lines (130 loc) · 4.89 KB
/
Copy pathStructures.cs
File metadata and controls
157 lines (130 loc) · 4.89 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace ArchiveMe
{
public class OPPost : Post {
public string sub { get; set; }
public int images { get; set; }
public int replies { get; set; }
public OPPost(JToken OPpost, string thread_id, string board)
{
dynamic post = JsonConvert.DeserializeObject(OPpost.ToString());
sub = TrySet(post, "sub");
images = post.images;
replies = post.replies;
SetPostData(OPpost, thread_id, board);
}
public OPPost(string sub,int images,int replies)
{
this.sub = sub;
this.images = images;
this.replies = replies;
}
}
public class Post
{
private string thread_id {get;set;}
private string board { get; set; }
public UInt64 no { get; set; }
public Int64 time { get; set; }
public string name { get; set; }
public string id { get; set; }
public string country { get; set; } //only one of this will be set
public string troll_country { get;set; }
public string country_name { get; set; }
public long tim { get; set; } //filename
public string ext { get; set; }
public string com { get; set; }
public string image { get; set; }
public string thumbnail { get; set; }
public Post()
{
}
public Post(UInt64 no, Int64 time, string name, string id,string country, string troll_country, string country_name, string com, string image, string thumbnail)
{
this.no = no;
this.time = time;
this.name = name;
this.id = (id != "*") ? " ID: "+id : null;
this.country = (country != "*") ? country : null;
this.troll_country = (troll_country != "*") ? troll_country : null;
this.country_name = (country_name != "*") ? country_name : null;
this.com = (com != "*") ? com : null;
this.thumbnail = (thumbnail != "*") ? thumbnail : null;
this.image = (image != "*") ? image : null;
}
public UInt64 SetPostData(JToken raw_post, string thread_id, string board)
{
this.thread_id = thread_id;
this.board = board;
dynamic post = JsonConvert.DeserializeObject(raw_post.ToString());
string text = post.ext;
no = post.no;
time = post.time;
name = post.name;
id = TrySet(post, "id");
country = TrySet(post,"country");
troll_country = TrySet(post, "troll_country");
country_name = TrySet(post, "country_name");
ext = TrySet(post, "ext");
com = TrySet(post, "com");
if((TrySet(post, "tim") != null)){
tim = (TrySet(post, "tim"));
}
else{
tim = 0;
}
if (ext != null)
{
if (DowloadImages(thread_id,board,tim.ToString(),ext))
{
image = "images\\" + board + "\\" + thread_id + "\\" + tim + ext;
thumbnail = "images\\" + board + "\\" + thread_id + "\\" + tim + "s.jpg";
}
}
return no;
}
private bool DowloadImages(string thread_id,string board, string tim, string ext)
{
string image_save_path = System.AppDomain.CurrentDomain.BaseDirectory + "images\\" + board + "\\" + thread_id + "\\" + tim + ext;
string thumbnail_save_path = System.AppDomain.CurrentDomain.BaseDirectory +"images\\" + board + "\\" + thread_id + "\\" + tim + "s.jpg";
string image_url = "https://i.4cdn.org/"+board+"/"+tim+ext;
string thumbnail_url = "https://i.4cdn.org/" + board + "/" + tim + "s.jpg";
using (var client = new System.Net.WebClient())
{
try
{
System.IO.Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory+"images\\" + board + "\\" + thread_id + "\\");
client.DownloadFile(image_url,image_save_path);
client.DownloadFile(thumbnail_url, thumbnail_save_path);
}
catch (Exception)
{
return false;
throw;
}
}
return true;
}
public static dynamic TrySet(dynamic obj,string post_propery)
{
dynamic property;
try
{
property = obj[post_propery].Value;
}
catch
{
property = null;
}
return property;
}
public UInt64 GetNo()
{
return no;
}
}
}