Skip to content

Commit

Permalink
[feature/ISSUE-28] Use Cache in app.d
Browse files Browse the repository at this point in the history
  • Loading branch information
azihassan committed Oct 15, 2023
1 parent 965948e commit 6ff138c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 4 additions & 3 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import std.range : iota;
import std.logger;
import std.getopt;

import parsers;
import downloaders;
import helpers;
import parsers : YoutubeFormat, YoutubeVideoURLExtractor;
import cache : Cache;

void main(string[] args)
{
Expand Down Expand Up @@ -74,10 +75,10 @@ void main(string[] args)

void handleURL(string url, int itag, StdoutLogger logger, bool displayFormats, bool outputURL, bool parallel, bool noProgress)
{
auto cache = Cache(logger);
logger.display(formatTitle("Handling " ~ url));
string html = url.get().idup;
logger.displayVerbose("Downloaded video HTML");
YoutubeVideoURLExtractor parser = makeParser(html, logger);
YoutubeVideoURLExtractor parser = cache.makeParser(url, itag);

if(displayFormats)
{
Expand Down
18 changes: 11 additions & 7 deletions source/cache.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Cache
this.downloadAsString = downloadAsString;
}

YoutubeVideoURLExtractor makeParser(string url, int itag, StdoutLogger logger)
YoutubeVideoURLExtractor makeParser(string url, int itag)
{
string html = getHTML(url, itag);
if(html.indexOf("signatureCipher") == -1)
Expand Down Expand Up @@ -59,15 +59,19 @@ struct Cache

private void updateCache(string url, string htmlCachePath, string baseJSCachePath, int itag)
{
string cachedHTML = htmlCachePath.readText();
bool shouldRedownload = !htmlCachePath.exists() || isStale(cachedHTML, itag);
bool shouldRedownload = !htmlCachePath.exists() || isStale(htmlCachePath.readText(), itag);
if(shouldRedownload)
{
logger.display("Cache miss, downloading HTML...");
string html = this.downloadAsString(url);
htmlCachePath.write(html);
string baseJS = html.parseBaseJSURL();
baseJSCachePath.write(html);
}
else
{
logger.display("Cache hit, skipping HTML download...");
}
}

private bool isStale(string html, int itag)
Expand Down Expand Up @@ -102,7 +106,7 @@ unittest
auto cache = Cache(new StdoutLogger(), downloadAsString);
cache.cacheDirectory = getcwd();

auto parser = cache.makeParser("https://youtu.be/zoz", 18, new StdoutLogger());
auto parser = cache.makeParser("https://youtu.be/zoz", 18);
assert(downloadAttempted);
}

Expand All @@ -120,7 +124,7 @@ unittest

"zoz-fresh.html".write("zoz.html".readText().dup.replace("expire=1638935038", "expire=" ~ tomorrow.toUnixTime().to!string));

auto parser = cache.makeParser("https://youtu.be/zoz-fresh", 18, new StdoutLogger());
auto parser = cache.makeParser("https://youtu.be/zoz-fresh", 18);
assert(!downloadAttempted);
}

Expand All @@ -135,7 +139,7 @@ unittest
auto cache = Cache(new StdoutLogger(), downloadAsString);
cache.cacheDirectory = getcwd();

auto parser = cache.makeParser("https://youtu.be/dQw4w9WgXcQ", 18, new StdoutLogger());
auto parser = cache.makeParser("https://youtu.be/dQw4w9WgXcQ", 18);
assert(downloadAttempted);
}

Expand All @@ -158,6 +162,6 @@ unittest
);


auto parser = cache.makeParser("https://youtu.be/dQw4w9WgXcQ-fresh", 18, new StdoutLogger());
auto parser = cache.makeParser("https://youtu.be/dQw4w9WgXcQ-fresh", 18);
assert(!downloadAttempted);
}

0 comments on commit 6ff138c

Please sign in to comment.