D wrapper around (some) of the pixiv web API
Révision | fff4a231002b0048fdae574183f4343848f0bfeb (tree) |
---|---|
l'heure | 2023-08-13 10:11:17 |
Auteur | supercell <stigma@disr...> |
Commiter | supercell |
Add signals for download progress and completion
This makes use of std.signals. This will emit the DownloadProgressEvent
when downloading individual pages or the ZIP (in the case of Ugoira).
The DownloadCompleteEvent is emitted after the file being written to has
been closed.
@@ -6,6 +6,7 @@ import std.array : appender; | ||
6 | 6 | import std.format : format; |
7 | 7 | import std.json; |
8 | 8 | import std.net.curl : HTTP; |
9 | +import std.signals; | |
9 | 10 | |
10 | 11 | import graphicsmagick_c; |
11 | 12 | import graphicsmagick_c.magick.api; |
@@ -17,6 +18,16 @@ import pixivd.types; | ||
17 | 18 | public enum PixivDVersion = 0.8; |
18 | 19 | public enum PixivDVersionString = "0.8"; |
19 | 20 | |
21 | +struct DownloadProgressEvent | |
22 | +{ | |
23 | + size_t total; | |
24 | + size_t current; | |
25 | +} | |
26 | + | |
27 | +struct DownloadCompleteEvent | |
28 | +{ | |
29 | +} | |
30 | + | |
20 | 31 | /** |
21 | 32 | * |
22 | 33 | * The main client for performing pixiv API requests. |
@@ -85,6 +96,10 @@ public: | ||
85 | 96 | } |
86 | 97 | } |
87 | 98 | |
99 | + /* Signals */ | |
100 | + mixin Signal!(DownloadProgressEvent); | |
101 | + mixin Signal!(DownloadCompleteEvent); | |
102 | + | |
88 | 103 | /** |
89 | 104 | * Fetch an illustration |
90 | 105 | * |
@@ -713,9 +728,14 @@ private: | ||
713 | 728 | imageFile.rawWrite(data); |
714 | 729 | return data.length; |
715 | 730 | }; |
731 | + m_client.onProgress = (size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) { | |
732 | + emit(DownloadProgressEvent(dlTotal, dlNow)); | |
733 | + return 0; | |
734 | + }; | |
716 | 735 | |
717 | 736 | m_client.perform(); |
718 | 737 | imageFile.close(); |
738 | + emit(DownloadCompleteEvent()); | |
719 | 739 | |
720 | 740 | SysTime createDate = SysTime.fromISOExtString(illust.createDate); |
721 | 741 |
@@ -769,9 +789,15 @@ private: | ||
769 | 789 | img.rawWrite(data); |
770 | 790 | return data.length; |
771 | 791 | }; |
792 | + m_client.onProgress = (size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) { | |
793 | + emit(DownloadProgressEvent(dlTotal, dlNow)); | |
794 | + return 0; | |
795 | + }; | |
796 | + | |
772 | 797 | |
773 | 798 | m_client.perform(); |
774 | 799 | img.close(); |
800 | + emit(DownloadCompleteEvent()); | |
775 | 801 | } |
776 | 802 | } |
777 | 803 |
@@ -823,8 +849,13 @@ private: | ||
823 | 849 | outFile.rawWrite(data); |
824 | 850 | return data.length; |
825 | 851 | }; |
852 | + m_client.onProgress = (size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) { | |
853 | + emit(DownloadProgressEvent(dlTotal, dlNow)); | |
854 | + return 0; | |
855 | + }; | |
826 | 856 | m_client.perform(); |
827 | 857 | outFile.close(); |
858 | + emit(DownloadCompleteEvent()); | |
828 | 859 | |
829 | 860 | SysTime createDate = SysTime.fromISOExtString(illust.createDate); |
830 | 861 |
@@ -887,6 +918,10 @@ private: | ||
887 | 918 | zipFile.rawWrite(data); |
888 | 919 | return data.length; |
889 | 920 | }; |
921 | + m_client.onProgress = (size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) { | |
922 | + emit(DownloadProgressEvent(dlTotal, dlNow)); | |
923 | + return 0; | |
924 | + }; | |
890 | 925 | |
891 | 926 | m_client.perform(); |
892 | 927 | zipFile.close(); |
@@ -977,5 +1012,6 @@ private: | ||
977 | 1012 | } |
978 | 1013 | |
979 | 1014 | DestroyExceptionInfo(&exception); |
1015 | + emit(DownloadCompleteEvent()); | |
980 | 1016 | } |
981 | 1017 | } |