Skip to content

Commit 9f0475f

Browse files
authored
Merge pull request #71 from warreth/donator-key
Donator key support
2 parents 7bb3e49 + 055b4bf commit 9f0475f

4 files changed

Lines changed: 90 additions & 10 deletions

File tree

lib/main.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import 'package:openlib/state/state.dart'
3838
autoRankInstancesProvider,
3939
userAgentProvider,
4040
cookieProvider,
41+
donationKeyProvider,
4142
selectedTypeState,
4243
selectedSortState,
4344
selectedFileTypeState,
@@ -111,6 +112,11 @@ void main(List<String> args) async {
111112
.catchError((e) => 'All') as String? ??
112113
'All';
113114

115+
String savedDonationKey = await dataBase
116+
.getPreference('donationKey')
117+
.catchError((e) => '') as String? ??
118+
'';
119+
114120
// Check onboarding status
115121
bool onboardingCompleted = await dataBase
116122
.getPreference('onboardingCompleted')
@@ -136,6 +142,7 @@ void main(List<String> args) async {
136142
.overrideWith((ref) => openEpubwithExternalapp),
137143
showManualDownloadButtonProvider
138144
.overrideWith((ref) => showManualDownloadButton),
145+
donationKeyProvider.overrideWith((ref) => savedDonationKey),
139146
userAgentProvider.overrideWith((ref) => browserUserAgent),
140147
cookieProvider.overrideWith((ref) => browserCookie),
141148
selectedTypeState.overrideWith((ref) => savedType),

lib/services/annas_archieve.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,31 @@ class AnnasArchieve {
285285
// _bookInfoParser FUNCTION (Detail Page - Fixed 'unable to get data' error)
286286
// --------------------------------------------------------------------
287287
Future<BookInfoData?> _bookInfoParser(
288-
resData, url, String currentBaseUrl) async {
288+
resData, url, String currentBaseUrl, String? donationKey) async {
289289
var document = parse(resData.toString());
290290
final main = document.querySelector('div.main-inner');
291291
if (main == null) return null;
292292

293293
// --- Mirror Link Extraction ---
294294
String? mirror;
295-
final slowDownloadLinks =
296-
main.querySelectorAll('ul.list-inside a[href*="/slow_download/"]');
297-
if (slowDownloadLinks.isNotEmpty &&
298-
slowDownloadLinks.first.attributes['href'] != null) {
299-
mirror = currentBaseUrl + slowDownloadLinks.first.attributes['href']!;
295+
296+
if (donationKey != null && donationKey.isNotEmpty) {
297+
final fastDownloadLinks =
298+
main.querySelectorAll('ul.list-inside a[href*="/fast_download/"]');
299+
if (fastDownloadLinks.isNotEmpty &&
300+
fastDownloadLinks.first.attributes['href'] != null) {
301+
mirror =
302+
"$currentBaseUrl${fastDownloadLinks.first.attributes['href']!}?key=$donationKey";
303+
}
304+
}
305+
306+
if (mirror == null) {
307+
final slowDownloadLinks =
308+
main.querySelectorAll('ul.list-inside a[href*="/slow_download/"]');
309+
if (slowDownloadLinks.isNotEmpty &&
310+
slowDownloadLinks.first.attributes['href'] != null) {
311+
mirror = currentBaseUrl + slowDownloadLinks.first.attributes['href']!;
312+
}
300313
}
301314
// --------------------------------
302315

@@ -487,7 +500,8 @@ class AnnasArchieve {
487500
}
488501
}
489502

490-
Future<BookInfoData> bookInfo({required String url}) async {
503+
Future<BookInfoData> bookInfo(
504+
{required String url, String? donationKey}) async {
491505
_logger.info('Fetching book info',
492506
tag: 'AnnasArchive', metadata: {'url': url});
493507

@@ -524,8 +538,8 @@ class AnnasArchieve {
524538
);
525539
}
526540

527-
BookInfoData? data =
528-
await _bookInfoParser(response.data, adjustedUrl, currentBaseUrl);
541+
BookInfoData? data = await _bookInfoParser(
542+
response.data, adjustedUrl, currentBaseUrl, donationKey);
529543
if (data != null) {
530544
return data;
531545
} else {

lib/state/state.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ final searchQueryProvider = StateProvider<String>((ref) => "");
173173
final enableFiltersState = StateProvider<bool>((ref) => true);
174174

175175
// Web/Download States
176+
final donationKeyProvider = StateProvider<String>((ref) => "");
176177
final cookieProvider = StateProvider<String>((ref) => "");
177178
final userAgentProvider = StateProvider<String>((ref) => "");
178179
final webViewLoadingState = StateProvider.autoDispose<bool>((ref) => true);
@@ -346,7 +347,9 @@ final searchProvider = FutureProvider.family
346347
final bookInfoProvider =
347348
FutureProvider.family<BookInfoData, String>((ref, url) async {
348349
final AnnasArchieve annasArchieve = AnnasArchieve();
349-
BookInfoData data = await annasArchieve.bookInfo(url: url);
350+
final donationKey = ref.watch(donationKeyProvider);
351+
BookInfoData data =
352+
await annasArchieve.bookInfo(url: url, donationKey: donationKey);
350353
return data;
351354
});
352355

lib/ui/settings_page.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import 'package:openlib/state/state.dart'
3131
instanceManagerProvider,
3232
currentInstanceProvider,
3333
archiveInstancesProvider,
34+
donationKeyProvider,
3435
myLibraryProvider;
3536

3637
// Scans a directory for book files (epub, pdf) and imports them to the library database
@@ -253,6 +254,61 @@ class SettingsPage extends ConsumerWidget {
253254
dataBase.savePreference('showManualDownloadButton', val);
254255
},
255256
),
257+
_buildSettingTile(
258+
context,
259+
title: "Anna's Archive Donation Key",
260+
subtitle: "Enter key for faster downloads",
261+
icon: Icons.key,
262+
onTap: () {
263+
final currentKey = ref.read(donationKeyProvider);
264+
final controller = TextEditingController(text: currentKey);
265+
showDialog(
266+
context: context,
267+
builder: (context) => AlertDialog(
268+
title: const Text("Donation Key"),
269+
content: TextField(
270+
controller: controller,
271+
decoration: const InputDecoration(
272+
hintText: "Enter your key",
273+
helperText:
274+
"Used for faster downloads on Anna's Archive",
275+
),
276+
),
277+
actions: [
278+
TextButton(
279+
onPressed: () => Navigator.pop(context),
280+
child: Text(
281+
"Cancel",
282+
style: TextStyle(
283+
color:
284+
Theme.of(context).brightness == Brightness.light
285+
? Colors.black
286+
: Colors.white,
287+
),
288+
),
289+
),
290+
TextButton(
291+
onPressed: () {
292+
final newKey = controller.text.trim();
293+
ref.read(donationKeyProvider.notifier).state = newKey;
294+
dataBase.savePreference('donationKey', newKey);
295+
Navigator.pop(context);
296+
},
297+
child: Text(
298+
"Save",
299+
style: TextStyle(
300+
color:
301+
Theme.of(context).brightness == Brightness.light
302+
? Colors.black
303+
: Colors.white,
304+
),
305+
),
306+
),
307+
],
308+
),
309+
);
310+
},
311+
),
256312
const SizedBox(height: 20),
257313
_buildSectionHeader(context, "About"),
258314
_buildSettingTile(

0 commit comments

Comments
 (0)