Skip to content

Commit

Permalink
last existence of the old (might be efficient) ugoira zip downloading…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
HenrySck075 committed Feb 12, 2024
1 parent 71ec9a7 commit 98ac183
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/json/ajax/illust/Artwork.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 25 additions & 11 deletions lib/pages/view/artworks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ class _UgoiraDisplayerState extends State<UgoiraDisplayer> with TickerProviderSt
)..addListener(() {
var i = widget.frames[_animCtrl.value.round()];
if (_animCtrl.value == widgetFrames.length) {
pxRequestUnprocessed(widget.url,otherHeaders: {"Range":"bytes=${i['offset']}-${i['offset']+i['length']}"}).then((value) {
widgetFrames.add(Image.memory(Uint8List.fromList(arch.ZipFile(arch.InputStream(value.body),arch.ZipFileHeader(arch.InputStream(i["centralDirectory"]))).content)));
pxRequestUnprocessed(widget.url,otherHeaders: {"Range":"bytes=${i['offset']}-${i['offset']+i['length']-1}"}).then((value) {
widgetFrames.add(Image.memory(Uint8List.fromList(arch.ArchiveFile(
i["centralDirectory"].sublist(46,46+10).toString(), // name
i["centralDirectory"].sublist(20,23).fold(0,(p,c)=>p+c), // size
value.body,
i["centralDirectory"].sublist(10,10).fold(0,(p,c)=>p+c), // compression method (usually deflate but i dont trust pixiv)
).content)));
curImg.value = widgetFrames[_animCtrl.value.round()];
scheduleUpdate(i["delay"]);
});
Expand Down Expand Up @@ -187,25 +192,34 @@ class _ArtworkPageState extends State<ArtworkPage> {
/// offset length (look into the source code)
return futureWidget(
future: pxRequestUnprocessed(data["src"],method: "HEAD").then((re) {
var start = int.parse(re.headers['content-length']!)-(int.parse(frames.last['file'].substring(0,6))*56)-22;
var end = int.parse(re.headers['content-length']!);
var content_length = (end-start).toString();
return pxRequestUnprocessed(data["src"],otherHeaders: {"Range":"bytes=$start-$end","content-length":content_length});
var start = int.parse(re.headers['content-length']!)-30000;
var end = int.parse(re.headers['content-length']!)-1;
return pxRequestUnprocessed(data["src"],otherHeaders: {"Range":"bytes=$start-$end","Upgrade-Insecure-Requests":"1"});
}), // the central directory, will be very important
builder: (ctx,snap) {
var cddata = snap.data!.bodyBytes;

int cdOffset = 0;
List<int> frameOffsets = [cdOffset];
int cdOffset = cddata.indexOf(80);
while (true) {
var the = cddata.sublist(cdOffset,cdOffset+4);
if (the[0]==80&&the[1]==75&&the[2]==1&&the[3]==2) break;
cdOffset = cddata.indexOf(80,cdOffset+1);
}
int cdStartOffset = cdOffset;
List<int> frameOffsets = [];
int i = 0;
while (true) {
cdOffset = cddata.indexOf(80);
if (cdOffset!=-1) break;
frameOffsets.insert(0,cddata.sublist(cdOffset+42,cdOffset+45).fold(0, (previousValue, element) => previousValue+element));
if (cdOffset==-1) break;
var the = cddata.sublist(cdOffset,cdOffset+4);
// check if its eocd
if (the[0]==80&&the[1]==75&&the[2]==5&&the[3]==6) break;
frameOffsets.add(cddata.sublist(cdOffset+42,cdOffset+46).fold(0, (previousValue, element) => previousValue+element));
frames[i]["centralDirectory"] = cddata.sublist(cdOffset,cdOffset+56);
cddata = cddata.sublist(cdOffset+57);
i+=1;
cdOffset = cddata.indexOf(80);
}
frameOffsets.add(cdStartOffset);
for (int i = 0; i < frameOffsets.length-1; i++) {
frames[i]["offset"] = frameOffsets[i];
frames[i]["length"] = frameOffsets[i+1]-frameOffsets[i];
Expand Down
19 changes: 10 additions & 9 deletions lib/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,21 @@ var client = http.Client();
Future<void> wait(FutureOr<bool> Function(dynamic) predicate) async => await Future.doWhile(() => Future.delayed(const Duration(milliseconds: 500)).then(predicate));
/// [pxRequest] without postprocess
Future<http.Response> pxRequestUnprocessed(String url, {
Map<String, String> otherHeaders = const {}, String method="GET", Object? body, bool noCache = false,
Map<String, String>? otherHeaders, String method="GET", Object? body, bool noCache = false,
void Function(double percent, int total)? onProgress
}) async {
if (otherHeaders == null) {otherHeaders = {};}
// wait for cookies to not empty (it will never)
if (cooki == "") await wait((_) => cooki=="");

var headers = {
"cookie": cooki.trim(),
"referer": "https://www.pixiv.net/en/",
"x-user-id": "76179633",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
};
print("-----");
print(url);
print(method);
print(otherHeaders);
// print("-----");
// print(url);
// print(method);
// print(otherHeaders);
headers.addAll(otherHeaders);
if (_cachedResponse.containsKey(url) && !noCache && _cachedResponse[url]!.$1["headers"] == headers) {return Future.value(_cachedResponse[url]!.$2);}// we dont really needs to null check but dart sucks so

Expand Down Expand Up @@ -202,9 +201,11 @@ Future<http.Response> pxRequestUnprocessed(String url, {
http.Response(String.fromCharCodes(bytes), resp.statusCode, headers: resp.headers)
)).$2;
}
Future<dynamic> pxRequest(String url, {Map<String, String> otherHeaders = const {}, String method="GET", Object? body, bool noCache = false}) {
Future<dynamic> pxRequest(String url, {Map<String, String>? otherHeaders, String method="GET", Object? body, bool noCache = false}) {
otherHeaders ??= {};
url = '${url+(url.contains("?")?"&":"?")}lang=en&version=$apiVersion';
var d = pxRequestUnprocessed(url,otherHeaders: otherHeaders, method: method, body:body, noCache: noCache).then((v){
// if (cooki == "") await wait((_) => cooki=="");
var d = pxRequestUnprocessed(url,otherHeaders: otherHeaders..addEntries([MapEntry("cookie", cooki.trim())]), method: method, body:body, noCache: noCache).then((v){
return jsonDecode(v.body)["body"];
});
return d;
Expand Down
22 changes: 11 additions & 11 deletions lib/silly.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Widgets & builders
// Widgets & builder

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
Expand Down Expand Up @@ -38,16 +38,16 @@ Wrap artworkGrid(List<Widget> h) => Wrap(
FutureBuilder<T> futureWidget<T>({required Future<T>? future, required AsyncWidgetBuilder<T> builder, Widget placeholder = const Center(child: CircularProgressIndicator())}) {
return FutureBuilder<T>(future: future, builder: (ctx, snap) {
if (snap.data == null) return placeholder;
// if (snap.data == null) {return Scaffold(body:Center(
// child: Column(
// children: [
// const Text("oops, something goes wrong with the request"),
// const Text("try to figure out what's wrong with your implementation henry", style: TextStyle(fontSize: 12),),
// Text(snap.error.toString(),style: const TextStyle(fontSize: 13),),
// Text(snap.stackTrace.toString(),style: const TextStyle(fontSize: 13),),
// ],
// ),
//));}
if (snap.hasError) {return Scaffold(body:Center(
child: Column(
children: [
const Text("oops, something goes wrong with the request"),
const Text("try to figure out what's wrong with your implementation henry", style: TextStyle(fontSize: 12),),
Text(snap.error.toString(),style: const TextStyle(fontSize: 13),),
Text(snap.stackTrace.toString(),style: const TextStyle(fontSize: 13),),
],
),
));}
return builder(ctx, snap);
});
}
Expand Down

0 comments on commit 98ac183

Please sign in to comment.