-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature/ISSUE-28] Cache initial HTML page retrieval
- Loading branch information
Showing
2 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import std.base64 : Base64URL; | ||
import std.conv : to; | ||
import std.datetime : SysTime, Clock; | ||
import std.file : exists, getcwd, readText, tempDir, write; | ||
import std.net.curl : get; | ||
import std.path : buildPath; | ||
import std.string : indexOf; | ||
|
||
import helpers : StdoutLogger, parseID, parseQueryString; | ||
import parsers : makeParser, YoutubeVideoURLExtractor; | ||
|
||
struct Cache | ||
{ | ||
private StdoutLogger logger; | ||
private string delegate(string url) downloadAsString; | ||
string cacheDirectory; | ||
|
||
this(StdoutLogger logger) | ||
{ | ||
this.logger = logger; | ||
downloadAsString = (string url) => url.get().idup; | ||
cacheDirectory = tempDir(); | ||
} | ||
|
||
this(StdoutLogger logger, string delegate(string url) downloadAsString) | ||
{ | ||
this(logger); | ||
this.downloadAsString = downloadAsString; | ||
} | ||
|
||
string getHTML(string url, int itag) | ||
{ | ||
string cacheKey = url.parseID(); | ||
if(cacheKey == "") | ||
{ | ||
cacheKey = Base64URL.encode(cast(ubyte[]) url); | ||
} | ||
|
||
string cachePath = buildPath(cacheDirectory, cacheKey) ~ ".html"; | ||
updateCache(url, cachePath, itag); | ||
return cachePath.readText(); | ||
} | ||
|
||
private void updateCache(string url, string cachePath, int itag) | ||
{ | ||
string cachedHTML = cachePath.readText(); | ||
if(!cachePath.exists() || isStale(cachedHTML, itag)) | ||
{ | ||
string html = this.downloadAsString(url); | ||
cachePath.write(html); | ||
} | ||
} | ||
|
||
private bool isStale(string html, int itag) | ||
{ | ||
YoutubeVideoURLExtractor parser = makeParser(html, logger); | ||
string videoURL = parser.getURL(itag); | ||
int expire = videoURL.parseQueryString()["expire"].to!int; | ||
return Clock.currTime() < SysTime.fromUnixTime(expire); | ||
} | ||
} | ||
|
||
unittest | ||
{ | ||
import std.stdio : writeln; | ||
writeln("When cache is stale, should redownload HTML"); | ||
bool downloadAttempted; | ||
auto downloadAsString = delegate string(string url) { | ||
downloadAttempted = true; | ||
return ""; | ||
}; | ||
auto cache = Cache(new StdoutLogger(), downloadAsString); | ||
cache.cacheDirectory = getcwd(); | ||
|
||
string html = cache.getHTML("https://youtu.be/dQw4w9WgXcQ", 18); | ||
assert(downloadAttempted); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters