Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import 'package:openlib/state/state.dart'
autoRankInstancesProvider,
userAgentProvider,
cookieProvider,
donationKeyProvider,
selectedTypeState,
selectedSortState,
selectedFileTypeState,
Expand Down Expand Up @@ -111,6 +112,11 @@ void main(List<String> args) async {
.catchError((e) => 'All') as String? ??
'All';

String savedDonationKey = await dataBase
.getPreference('donationKey')
.catchError((e) => '') as String? ??
'';

// Check onboarding status
bool onboardingCompleted = await dataBase
.getPreference('onboardingCompleted')
Expand All @@ -136,6 +142,7 @@ void main(List<String> args) async {
.overrideWith((ref) => openEpubwithExternalapp),
showManualDownloadButtonProvider
.overrideWith((ref) => showManualDownloadButton),
donationKeyProvider.overrideWith((ref) => savedDonationKey),
userAgentProvider.overrideWith((ref) => browserUserAgent),
cookieProvider.overrideWith((ref) => browserCookie),
selectedTypeState.overrideWith((ref) => savedType),
Expand Down
32 changes: 23 additions & 9 deletions lib/services/annas_archieve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,31 @@ class AnnasArchieve {
// _bookInfoParser FUNCTION (Detail Page - Fixed 'unable to get data' error)
// --------------------------------------------------------------------
Future<BookInfoData?> _bookInfoParser(
resData, url, String currentBaseUrl) async {
resData, url, String currentBaseUrl, String? donationKey) async {
var document = parse(resData.toString());
final main = document.querySelector('div.main-inner');
if (main == null) return null;

// --- Mirror Link Extraction ---
String? mirror;
final slowDownloadLinks =
main.querySelectorAll('ul.list-inside a[href*="/slow_download/"]');
if (slowDownloadLinks.isNotEmpty &&
slowDownloadLinks.first.attributes['href'] != null) {
mirror = currentBaseUrl + slowDownloadLinks.first.attributes['href']!;

if (donationKey != null && donationKey.isNotEmpty) {
final fastDownloadLinks =
main.querySelectorAll('ul.list-inside a[href*="/fast_download/"]');
if (fastDownloadLinks.isNotEmpty &&
fastDownloadLinks.first.attributes['href'] != null) {
mirror =
"$currentBaseUrl${fastDownloadLinks.first.attributes['href']!}?key=$donationKey";
}
}

if (mirror == null) {
final slowDownloadLinks =
main.querySelectorAll('ul.list-inside a[href*="/slow_download/"]');
if (slowDownloadLinks.isNotEmpty &&
slowDownloadLinks.first.attributes['href'] != null) {
mirror = currentBaseUrl + slowDownloadLinks.first.attributes['href']!;
}
}
// --------------------------------

Expand Down Expand Up @@ -487,7 +500,8 @@ class AnnasArchieve {
}
}

Future<BookInfoData> bookInfo({required String url}) async {
Future<BookInfoData> bookInfo(
{required String url, String? donationKey}) async {
_logger.info('Fetching book info',
tag: 'AnnasArchive', metadata: {'url': url});

Expand Down Expand Up @@ -524,8 +538,8 @@ class AnnasArchieve {
);
}

BookInfoData? data =
await _bookInfoParser(response.data, adjustedUrl, currentBaseUrl);
BookInfoData? data = await _bookInfoParser(
response.data, adjustedUrl, currentBaseUrl, donationKey);
if (data != null) {
return data;
} else {
Expand Down
5 changes: 4 additions & 1 deletion lib/state/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ final searchQueryProvider = StateProvider<String>((ref) => "");
final enableFiltersState = StateProvider<bool>((ref) => true);

// Web/Download States
final donationKeyProvider = StateProvider<String>((ref) => "");
final cookieProvider = StateProvider<String>((ref) => "");
final userAgentProvider = StateProvider<String>((ref) => "");
final webViewLoadingState = StateProvider.autoDispose<bool>((ref) => true);
Expand Down Expand Up @@ -346,7 +347,9 @@ final searchProvider = FutureProvider.family
final bookInfoProvider =
FutureProvider.family<BookInfoData, String>((ref, url) async {
final AnnasArchieve annasArchieve = AnnasArchieve();
BookInfoData data = await annasArchieve.bookInfo(url: url);
final donationKey = ref.watch(donationKeyProvider);
BookInfoData data =
await annasArchieve.bookInfo(url: url, donationKey: donationKey);
return data;
});

Expand Down
56 changes: 56 additions & 0 deletions lib/ui/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:openlib/state/state.dart'
instanceManagerProvider,
currentInstanceProvider,
archiveInstancesProvider,
donationKeyProvider,
myLibraryProvider;

// Scans a directory for book files (epub, pdf) and imports them to the library database
Expand Down Expand Up @@ -252,6 +253,61 @@ class SettingsPage extends ConsumerWidget {
dataBase.savePreference('showManualDownloadButton', val);
},
),
_buildSettingTile(
context,
title: "Anna's Archive Donation Key",
subtitle: "Enter key for faster downloads",
icon: Icons.key,
onTap: () {
final currentKey = ref.read(donationKeyProvider);
final controller = TextEditingController(text: currentKey);
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Donation Key"),
content: TextField(
controller: controller,
decoration: const InputDecoration(
hintText: "Enter your key",
helperText:
"Used for faster downloads on Anna's Archive",
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(
"Cancel",
style: TextStyle(
color:
Theme.of(context).brightness == Brightness.light
? Colors.black
: Colors.white,
),
),
),
TextButton(
onPressed: () {
final newKey = controller.text.trim();
ref.read(donationKeyProvider.notifier).state = newKey;
dataBase.savePreference('donationKey', newKey);
Navigator.pop(context);
},
child: Text(
"Save",
style: TextStyle(
color:
Theme.of(context).brightness == Brightness.light
? Colors.black
: Colors.white,
),
),
),
],
),
);
},
),
const SizedBox(height: 20),
_buildSectionHeader(context, "About"),
_buildSettingTile(
Expand Down