Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Improved raw size handling
Browse files Browse the repository at this point in the history
- Allows to specify which hosts to check for a _raw image version. The
failback now iterates through all sizes of the original found host until
a valid size was found that can be downloaded.
- The tumblr hosts can be set in the settings.json in the  TumblrHosts
variable. The first entry is check first, if there was no _raw image
found, the next entry is checked, until the list is exhausted. Then the
_1280 size of the original host is tried, the _500, _400 until the
request was successfully.
  • Loading branch information
johanneszab committed Jul 8, 2017
1 parent fa497d8 commit 70def36
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/TumblThree/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

[assembly: ComVisible(false)]
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
[assembly: AssemblyVersion("1.0.6.8")]
[assembly: AssemblyFileVersion("1.0.6.8")]
[assembly: AssemblyVersion("1.0.6.9")]
[assembly: AssemblyFileVersion("1.0.6.9")]
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@ private async Task DownloadPhotoAsync(IProgress<DataModels.DownloadProgress> pro
TumblrPost downloadItem, CancellationToken ct)
{
string blogDownloadLocation = blog.DownloadLocation();
string fileName = FileName(downloadItem);
string url = Url(downloadItem);
var fileDownloader = new FileDownloader();
url = await fileDownloader.TestImageRawUrl(url, shellService.Settings);
string fileName = url.Split('/').Last();
string fileLocation = FileLocation(blogDownloadLocation, fileName);
string fileLocationUrlList = FileLocationLocalized(blogDownloadLocation, Resources.FileNamePhotos);
DateTime postDate = PostDate(downloadItem);
Expand Down
64 changes: 52 additions & 12 deletions src/TumblThree/TumblThree.Applications/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,65 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
}
else
{
// TODO: Ugly hack: Many "_raw" requests seem to fail with '403 -- access denied' whereas usually
// the file just contained a lower resolution if it didn't had the specified size.
// We just replace the "_raw" with "_1280" and try again since "_1280" images are around for longer.
// Else, we finally give up.
if (url.Contains("_raw"))
{
url = url.Replace("_raw", "_1280");
}
else
{
throw;
}
throw;
}
}
}
return true;
}
}

public async Task<string> TestImageRawUrl(string url, AppSettings settings)
{
if (settings.ImageSize == "raw")
{
return await TestRawUrl(url, settings);
}
return url;
}

public async Task<string> TestRawUrl(string url, AppSettings settings)
{
if (!url.Contains("_raw"))
return url;
string path = new Uri(url).LocalPath.TrimStart('/');

foreach (string host in settings.TumblrHosts)
{
string rawUrl = "https://" + host + "/" + path;
if (await UrlExists(rawUrl, settings))
return rawUrl;
}

foreach (string size in settings.ImageSizes)
{
string rawUrl = url.Replace(settings.ImageSize, size);
if (await UrlExists(rawUrl, settings))
return rawUrl;
}

return url;
}

private async Task<bool> UrlExists(string url, AppSettings settings)
{
HttpWebRequest request = CreateWebReqeust(url, settings);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Method = "HEAD";

try
{
using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
return false;
}
}

public static async Task<bool> SaveStreamToDisk(Stream input, string destinationFileName, CancellationToken ct)
{
using (var stream = new FileStream(destinationFileName, FileMode.OpenOrCreate, FileAccess.Write))
Expand Down
11 changes: 11 additions & 0 deletions src/TumblThree/TumblThree.Applications/Properties/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public sealed class AppSettings : IExtensibleDataObject
"1080", "480"
};

private static readonly string[] tumblrHosts =
new string[]
{
"media.tumblr.com", "68.media.tumblr.com", "66.media.tumblr.com"
};

public AppSettings()
{
Initialize();
Expand Down Expand Up @@ -230,6 +236,11 @@ public ObservableCollection<string> BlogTypes
get { return new ObservableCollection<string>(blogTypes); }
}

public ObservableCollection<string> TumblrHosts
{
get { return new ObservableCollection<string>(tumblrHosts); }
}

ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }

private void Initialize()
Expand Down

0 comments on commit 70def36

Please sign in to comment.