Skip to content

Commit

Permalink
Added a quality option
Browse files Browse the repository at this point in the history
  • Loading branch information
creeperkafasi committed Sep 28, 2023
1 parent f27c833 commit 48cacf0
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 114 deletions.
10 changes: 6 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _AnimeAppState extends State<AnimeApp> {
theme: ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.red,
brightness: brightness ?? Brightness.light,
brightness: brightness,
),
useMaterial3: true,
),
Expand All @@ -63,9 +63,11 @@ class _AnimeAppState extends State<AnimeApp> {
// Shitty global settings logic: themes, etc.
_loadSettings() async {
final prefs = await SharedPreferences.getInstance();
brightness = prefs.getBool("brightness") ?? true
? Brightness.light
: Brightness.dark;
setState(() {
brightness = prefs.getBool("brightness") ?? true
? Brightness.light
: Brightness.dark;
});
}

refresh() {
Expand Down
167 changes: 109 additions & 58 deletions lib/show.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const allowedSourceNames = [
"Default",
"Sak",
"Kir",
"Default B",
"Ac",
// "Default B",
// "Ac",
"S-mp4",
"Uv-mp4",
// "Uv-mp4",
"Luf-mp4",
];

Expand Down Expand Up @@ -260,12 +260,63 @@ class _ShowOverviewState extends State<ShowOverview> {
.reversed
.map(
(episode) => TextButton.icon(
onPressed: () => showSelectServerDialog(
context,
showInfo["_id"],
tl,
episode,
),
// onPressed: () => showSelectServerDialog(
// context,
// showInfo["_id"],
// tl,
// episode,
// ),
onPressed: () async {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => const Dialog(
child: CircularProgressIndicator(),
),
);

final sources =
((await AllanimeAPI.episodeInfo(
showInfo["_id"],
episode,
tl,
))["data"]["episode"]["sourceUrls"]
as List)
.where(
(element) =>
allowedSourceNames.contains(
element["sourceName"],
),
);

List<Quality> qualities = [];

for (var source in sources) {
try {
qualities.addAll(
await AllanimeAPI
.getQualitiesFromSource(
source,
),
);
} catch (e) {
print(source);
}
}
// ignore: use_build_context_synchronously
Navigator.pop(context);
// ignore: use_build_context_synchronously
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WatchPage(
source: sources.first,
qualities: qualities,
showData: widget.showData,
),
),
);
},
icon: Icon({
"sub": Icons.subtitles_outlined,
"dub": Icons.record_voice_over_outlined,
Expand All @@ -292,53 +343,53 @@ class _ShowOverviewState extends State<ShowOverview> {
);
}

Future<dynamic> showSelectServerDialog(
BuildContext context,
String showId,
String translationType,
String episodeString,
) {
return showDialog(
context: context,
builder: (contex) =>
SimpleDialog(title: const Text("Select Server:"), children: [
FutureBuilder(
future: AllanimeAPI.episodeInfo(
showId,
episodeString,
translationType,
),
builder: (context, snapshot) {
if (snapshot.hasData) {
final sources =
snapshot.data!["data"]["episode"]["sourceUrls"] as List;
sources.removeWhere((source) =>
!allowedSourceNames.contains(source["sourceName"]));
return Column(
children: sources
.map(
(source) => TextButton(
onPressed: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WatchPage(
source: source,
showData: widget.showData,
),
));
},
child: Text(source["sourceName"]),
),
)
.toList(),
);
}
return const Center(child: CircularProgressIndicator());
},
),
]),
);
}
// Future<dynamic> showSelectServerDialog(
// BuildContext context,
// String showId,
// String translationType,
// String episodeString,
// ) {
// return showDialog(
// context: context,
// builder: (contex) =>
// SimpleDialog(title: const Text("Select Server:"), children: [
// FutureBuilder(
// future: AllanimeAPI.episodeInfo(
// showId,
// episodeString,
// translationType,
// ),
// builder: (context, snapshot) {
// if (snapshot.hasData) {
// final sources =
// snapshot.data!["data"]["episode"]["sourceUrls"] as List;
// sources.removeWhere((source) =>
// !allowedSourceNames.contains(source["sourceName"]));
// return Column(
// children: sources
// .map(
// (source) => TextButton(
// onPressed: () {
// Navigator.pop(context);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => WatchPage(
// source: source,
// showData: widget.showData,
// ),
// ));
// },
// child: Text(source["sourceName"]),
// ),
// )
// .toList(),
// );
// }
// return const Center(child: CircularProgressIndicator());
// },
// ),
// ]),
// );
// }
}
60 changes: 60 additions & 0 deletions lib/utils/allanime.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:m3u_nullsafe/m3u_nullsafe.dart';

class AllanimeAPI {
static const String allanimeBase = "https://allanime.to";
Expand Down Expand Up @@ -172,4 +173,63 @@ class AllanimeAPI {
}
return bytes;
}

static Future<List<Quality>> getQualitiesFromSource(
dynamic src,
) async {
final decryptedClockUrl = decryptAllAnime(
"1234567890123456789",
src["sourceUrl"].toString().split("--")[1],
);
final clockUrl = Uri.parse(
"https://embed.ssbcontent.site${decryptedClockUrl.replaceFirst("clock", "clock.json")}",
);
final source = await http.get(
clockUrl,
headers: {"Referer": "https://allanime.to"},
);

List<Quality> qualities = [];
for (var link in jsonDecode(source.body)["links"] as List) {
if (link["hls"] != null) {
if (link["link"].toString().contains("workfields")) continue;
final m3u8Content = (await http.get(Uri.parse(link["link"]))).body;

for (var q in (await M3uParser.parse(
m3u8Content,
))) {
var baseUrl = Uri.parse(link["link"]);
List<String> newPathSegments = [];
newPathSegments.addAll(baseUrl.pathSegments);
newPathSegments.removeLast();
newPathSegments.add(q.link);

qualities.add(
Quality(
url: baseUrl.replace(pathSegments: newPathSegments),
priority: 0,
name: "${link['resolutionStr']} - "
"${RegExp(r"\d+p").firstMatch(q.title)?.group(0).toString() ?? q.title}",
),
);
}
} else {
qualities.add(Quality(
url: Uri.parse(link["link"]),
priority: link["priority"] ?? 0,
name: "${src["sourceName"]} ${link["resolutionStr"]}",
));
}
}

return qualities;
}
}

class Quality {
final Uri url;
final int priority;
final String name;

Quality({required this.url, required this.priority, required this.name});
}
Loading

0 comments on commit 48cacf0

Please sign in to comment.