Skip to content

Commit 5781cf9

Browse files
committed
Alter 4chan thread downloading code to further alter anhor element href attributes to work locally with file:// protocol
1 parent 9a54c2a commit 5781cf9

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

GChan/Helpers/Utils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static string GetFilenameFromUrl(string hrefLink)
127127
/// </summary>
128128
/// <param name="directory">Directory path, exlcluding filename.</param>
129129
/// <returns>True if file was downloaded or already existed, false for error occured.</returns>
130-
public static bool DownloadFile(string url, string directory)
130+
public static bool DownloadFileIfDoesntExist(string url, string directory)
131131
{
132132
if (!Directory.Exists(directory))
133133
{

GChan/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3232
// übernehmen, indem Sie "*" eingeben:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("5.4.0")]
35-
[assembly: AssemblyFileVersion("5.4.0")]
34+
[assembly: AssemblyVersion("5.5.0")]
35+
[assembly: AssemblyFileVersion("5.5.0")]

GChan/Trackers/Sites/Thread_4Chan.cs

+42-41
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,17 @@ protected override ImageLink[] GetImageLinksImpl(bool includeAlreadySaved = fals
7272

7373
public override void DownloadHtmlImpl()
7474
{
75-
var thumbs = new List<string>();
76-
var htmlPage = "";
77-
var baseUrl = "//i.4cdn.org/" + BoardCode + "/";
78-
var jsonUrl = "http://a.4cdn.org/" + BoardCode + "/thread/" + ID + ".json";
79-
75+
var thumbUrls = new List<string>();
76+
var baseUrl = $"//i.4cdn.org/{BoardCode}/";
77+
var jsonUrl = $"http://a.4cdn.org/{BoardCode}/thread/{ID}.json";
78+
var htmlPage = string.Empty;
8079
JObject jObject;
8180

8281
using (var web = Utils.CreateWebClient())
8382
{
8483
htmlPage = web.DownloadString(Url);
85-
86-
//Prevent the html from being destroyed by the anti adblock script
8784
htmlPage = htmlPage.Replace("f=\"to\"", "f=\"penis\"");
88-
85+
8986
var json = web.DownloadString(jsonUrl);
9087
jObject = JObject.Parse(json);
9188
}
@@ -94,64 +91,68 @@ public override void DownloadHtmlImpl()
9491
.SelectTokens("posts[*]")
9592
.Where(x => x["ext"] != null)
9693
.ToList();
97-
94+
9895
foreach (var post in posts)
9996
{
100-
var old = baseUrl + post["tim"] + post["ext"];
101-
var replacement = post["tim"] + (string) post["ext"];
102-
htmlPage = htmlPage.Replace(old, replacement);
103-
104-
//get the actual filename saved
105-
var filename = Path
106-
.GetFileNameWithoutExtension(
107-
new ImageLink(post["tim"].Value<long>(),
108-
old,
109-
post["filename"].ToString(),
110-
post["no"].Value<long>(),
111-
this
112-
)
113-
.GenerateFilename((ImageFileNameFormat)Settings.Default.ImageFilenameFormat));
114-
115-
//Save thumbs for files that need it
116-
if (replacement.Split('.')[1] == "webm")
97+
var tim = post["tim"].ToString();
98+
var ext = post["ext"].ToString();
99+
var oldUrl = baseUrl + tim + ext;
100+
var newFilename = Path.GetFileNameWithoutExtension(
101+
new ImageLink(
102+
post["tim"].Value<long>(),
103+
oldUrl,
104+
post["filename"].ToString(),
105+
post["no"].Value<long>(),
106+
this
107+
).GenerateFilename((ImageFileNameFormat)Settings.Default.ImageFilenameFormat)
108+
);
109+
110+
htmlPage = htmlPage.Replace(oldUrl, tim + ext);
111+
112+
if (ext == ".webm")
117113
{
118-
old = "//t.4cdn.org/" + BoardCode + "/" + post["tim"] + "s.jpg";
119-
thumbs.Add("http:" + old);
114+
var thumbUrl = $"//t.4cdn.org/{BoardCode}/{tim}s.jpg";
115+
thumbUrls.Add($"http:{thumbUrl}");
120116

121-
htmlPage = htmlPage.Replace(post["tim"].ToString(), filename);
122-
htmlPage = htmlPage.Replace("//i.4cdn.org/" + BoardCode + "/" + filename, "thumb/" + post["tim"]);
117+
htmlPage = htmlPage.Replace(tim, newFilename);
118+
htmlPage = htmlPage.Replace($"{baseUrl}{newFilename}", $"thumb/{tim}");
123119
}
124120
else
125121
{
126-
var thumbName = replacement.Split('.')[0] + "s";
127-
htmlPage = htmlPage.Replace(thumbName + ".jpg", replacement.Split('.')[0] + "." + replacement.Split('.')[1]);
128-
htmlPage = htmlPage.Replace("/" + thumbName, thumbName);
122+
var thumbName = tim + "s";
123+
htmlPage = htmlPage.Replace($"{thumbName}.jpg", tim + ext);
124+
htmlPage = htmlPage.Replace($"/{thumbName}", thumbName);
129125

130-
htmlPage = htmlPage.Replace("//i.4cdn.org/" + BoardCode + "/" + post["tim"], post["tim"].ToString());
131-
htmlPage = htmlPage.Replace(post["tim"].ToString(), filename); //easy fix for images
126+
htmlPage = htmlPage.Replace($"{baseUrl}{tim}", tim);
127+
htmlPage = htmlPage.Replace(tim, newFilename);
132128
}
133129

134-
htmlPage = htmlPage.Replace("//is2.4chan.org/" + BoardCode + "/" + post["tim"], post["tim"].ToString()); //bandaid fix for is2 urls
135-
htmlPage = htmlPage.Replace("/" + replacement, replacement);
130+
htmlPage = htmlPage.Replace($"//is2.4chan.org/{BoardCode}/{tim}", tim);
131+
htmlPage = htmlPage.Replace($"/{tim}{ext}", tim + ext);
136132
}
137133

134+
// 4chan uses double slash urls (copy current protocol), when the user views it locally the protocol will no longer be http, so build it in.
135+
// This is used for javascript references.
138136
htmlPage = htmlPage.Replace("=\"//", "=\"http://");
139137

138+
// Alter all content links like "http://is2.4chan.org/tv/123.jpg" to become local like "123.jpg".
139+
htmlPage = htmlPage.Replace($"http://is2.4chan.org/{BoardCode}/", string.Empty);
140+
140141
if (Settings.Default.SaveThumbnails)
141142
{
142-
//Save thumbs for files that need it
143-
for (int i = 0; i < thumbs.Count; i++)
143+
foreach (var thumb in thumbUrls)
144144
{
145-
Utils.DownloadFile(thumbs[i], SaveTo + "\\thumb");
145+
Utils.DownloadFileIfDoesntExist(thumb, $"{SaveTo}\\thumb");
146146
}
147147
}
148148

149149
if (!string.IsNullOrWhiteSpace(htmlPage))
150150
{
151-
File.WriteAllText(SaveTo + "\\Thread.html", htmlPage);
151+
File.WriteAllText($"{SaveTo}\\Thread.html", htmlPage);
152152
}
153153
}
154154

155+
155156
protected override string GetThreadSubject()
156157
{
157158
string subject = NO_SUBJECT;

GChan/Trackers/Sites/Thread_8Kun.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public override void DownloadHtmlImpl()
148148
{
149149
for (int i = 0; i < thumbs.Count; i++)
150150
{
151-
Utils.DownloadFile(thumbs[i], SaveTo + "\\thumb");
151+
Utils.DownloadFileIfDoesntExist(thumbs[i], SaveTo + "\\thumb");
152152
}
153153
}
154154

0 commit comments

Comments
 (0)