|
@@ -16,15 +16,16 @@
|
|
|
*/
|
|
|
module cmds.daily;
|
|
|
|
|
|
-import std.datetime.systime;
|
|
|
-
|
|
|
import configuration;
|
|
|
import pixiv;
|
|
|
import pixiv_downloader;
|
|
|
+import std.datetime.systime;
|
|
|
import std.experimental.logger;
|
|
|
import term;
|
|
|
import util;
|
|
|
|
|
|
+import std.typecons: nullable, Nullable;
|
|
|
+
|
|
|
public int dailyHandle(string[] args, in Config config)
|
|
|
{
|
|
|
import core.time : TimeException;
|
|
@@ -43,12 +44,11 @@ public int dailyHandle(string[] args, in Config config)
|
|
|
try {
|
|
|
getopt(args,
|
|
|
"begin|b", &rawBeginDate,
|
|
|
- GetOptOption.required,
|
|
|
"end|e", &rawEndDate,
|
|
|
"force|f", &force,
|
|
|
"sfw-only|s", &restrict);
|
|
|
} catch (GetOptException e) {
|
|
|
- stderr.writefln("pixiv_donw daily: %s", e.msg);
|
|
|
+ stderr.writefln("pixiv_down daily: %s", e.msg);
|
|
|
stderr.writefln("Run 'pixiv_down help daily' for more information");
|
|
|
return 1;
|
|
|
}
|
|
@@ -69,12 +69,28 @@ public int dailyHandle(string[] args, in Config config)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- endDate = SysTime.fromISOExtString(rawEndDate ~ "T00:00:00+00:00");
|
|
|
- } catch (TimeException te) {
|
|
|
- stderr.writefln("pixiv_down daily: Failed to parse the end DATE: %s", rawEndDate);
|
|
|
- stderr.writeln("Make sure it's in the format YYYY-MM-DD, for example: 2023-06-20");
|
|
|
+ /* Automatically determine the end date, based off of the last run
|
|
|
+ * of this command, but allow people to specify a custom end date. */
|
|
|
+ if (string.init != rawEndDate) {
|
|
|
+ try {
|
|
|
+ endDate = SysTime.fromISOExtString(rawEndDate ~ "T00:00:00+00:00");
|
|
|
+ } catch (TimeException te) {
|
|
|
+ stderr.writefln("pixiv_down daily: Failed to parse the end DATE: %s", rawEndDate);
|
|
|
+ stderr.writeln("Make sure it's in the format YYYY-MM-DD, for example: 2023-06-20");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ } else if (string.init != rawBeginDate) {
|
|
|
+ stderr.writeln("pixiv_down daily: Cannot determine END date when BEGIN date has been provided.");
|
|
|
+ stderr.writeln(" Please manually provide and END date with the -e option.");
|
|
|
return 1;
|
|
|
+ } else {
|
|
|
+ auto lastRunTime = fetchLastStartTime();
|
|
|
+ if (lastRunTime.isNull()) {
|
|
|
+ stderr.writeln("pixiv_down daily: No end date could be determined, please provide date.");
|
|
|
+ stderr.writeln(" For example `pixiv_down daily -e 2023-06-20`");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ endDate = lastRunTime.get();
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -86,10 +102,17 @@ public int dailyHandle(string[] args, in Config config)
|
|
|
beginDate.minute = 59;
|
|
|
beginDate.second = 59;
|
|
|
}
|
|
|
- stderr.writeln("Begin = ", beginDate);
|
|
|
- stderr.writeln("End = ", endDate);
|
|
|
+
|
|
|
+ if (endDate.stdTime > beginDate.stdTime) {
|
|
|
+ stderr.writeln("pixiv_down daily: Cannot download. Provided END date is after BEGIN date.");
|
|
|
+ stderr.writeln(" pixiv_down downloads from newer date to older date.");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ stderr.writeln("Begin = ", (cast(Date)beginDate).toISOExtString());
|
|
|
+ stderr.writeln("End = ", (cast(Date)endDate).toISOExtString());
|
|
|
|
|
|
downloadDaily(DailyOptions(beginDate, endDate, restrict, force), config);
|
|
|
+ saveLastStartTime(beginDate);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -100,13 +123,16 @@ void displayDailyHelp()
|
|
|
|
|
|
stderr.writefln(
|
|
|
"pixiv_down daily - Download new content from followed artists.\n" ~
|
|
|
- "\nUsage:\tpixiv_down daily --end DATE [options]\n" ~
|
|
|
+ "\nUsage:\tpixiv_down daily [options]\n" ~
|
|
|
"\nThis command will, by default, download all the recently uploaded\n" ~
|
|
|
"from the people you follow up to (and including) the end date.\n" ~
|
|
|
- "This will also download both the \"safe\" works and \"r18\" works.\n" ~
|
|
|
- "Use the RESTRICT option to change this.\n" ~
|
|
|
- "\nThe only required option is the END date. Both the BEGIN and END\n" ~
|
|
|
- "dates are in the format of YYYY-MM-DD, for example: 2023-06-20.\n" ~
|
|
|
+ "This will also download both the \"safe\" and \"r18\" works. Use\n" ~
|
|
|
+ "the --sfw-only flag to change this.\n" ~
|
|
|
+ "\nBoth the BEGIN and END dates must follow the format YYYY-MM-DD,\n" ~
|
|
|
+ "for example: 2023-06-20. The END date is only required for the\n" ~
|
|
|
+ "first run of the daily command. Afterwards, the end date will be\n" ~
|
|
|
+ "determined automatically based on the last run's BEGIN date,\n" ~
|
|
|
+ "regardless of whether a BEGIN date was provided or not.\n" ~
|
|
|
"\nOptions:\n" ~
|
|
|
" -b, --begin BEGIN \tThe date the begin downloading from.\n" ~
|
|
|
" -e, --end END \tThe date to finish downloading at.\n" ~
|
|
@@ -121,8 +147,8 @@ void displayDailyHelp()
|
|
|
"\nI wouldn't recommend downloading all content from a specific day\n" ~
|
|
|
"which is over a month or two ago, since this command isn't really\n" ~
|
|
|
"designed for that. It'll require a lot of requests to the pixiv\n" ~
|
|
|
- "server and will take a long while. You'd be better using either the\n" ~
|
|
|
- "'artist' or 'following' command.");
|
|
|
+ "server and will take a long while. You'd be better using either\n" ~
|
|
|
+ "the 'artist' or 'following' command.");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -232,3 +258,37 @@ void downloadLatestNovels(in DailyOptions options, in Config config)
|
|
|
}
|
|
|
} while (!pastEndDate);
|
|
|
}
|
|
|
+
|
|
|
+Nullable!SysTime fetchLastStartTime() nothrow
|
|
|
+{
|
|
|
+ import mlib.directories: getProjectDirectories;
|
|
|
+ import std.datetime.date: Date;
|
|
|
+ import std.path: buildPath;
|
|
|
+ import std.stdio: File;
|
|
|
+
|
|
|
+ Nullable!SysTime sysTime;
|
|
|
+ auto dirs = getProjectDirectories(null, "YumeNeru Software", "pixiv_down");
|
|
|
+
|
|
|
+ try {
|
|
|
+ auto file = File(buildPath(dirs.stateDir, "last_daily_run"), "r");
|
|
|
+ auto date = Date.fromISOExtString(file.readln());
|
|
|
+ sysTime = SysTime(date);
|
|
|
+ } catch (Exception) {
|
|
|
+ return sysTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ return sysTime;
|
|
|
+}
|
|
|
+
|
|
|
+void saveLastStartTime(const ref SysTime startDate)
|
|
|
+{
|
|
|
+ import mlib.directories: getProjectDirectories;
|
|
|
+ import std.datetime.date: Date;
|
|
|
+ import std.path: buildPath;
|
|
|
+ import std.stdio: File;
|
|
|
+
|
|
|
+ auto dirs = getProjectDirectories(null, "YumeNeru Software", "pixiv_down");
|
|
|
+
|
|
|
+ auto file = File(buildPath(dirs.stateDir, "last_daily_run"), "w+");
|
|
|
+ file.writeln((cast(Date)startDate).toISOExtString());
|
|
|
+}
|