-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathsource_generator.dart
105 lines (94 loc) · 3.76 KB
/
source_generator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart/anime/anime_source_list.dart';
import 'dart/manga/manga_source_list.dart';
import 'dart/novel/novel_source_list.dart';
import 'model/source.dart';
void main() {
final jsSources = _searchJsSources(Directory("javascript"));
genManga(
jsSources.where((element) => element.itemType!.name == "manga").toList());
genAnime(
jsSources.where((element) => element.itemType!.name == "anime").toList());
genNovel(
jsSources.where((element) => element.itemType!.name == "novel").toList());
}
void genManga(List<Source> jsMangasourceList) {
List<Source> mangaSources = [];
mangaSources.addAll(dartMangasourceList);
mangaSources.addAll(jsMangasourceList);
final List<Map<String, dynamic>> jsonList =
mangaSources.map((source) => source.toJson()).toList();
final jsonString = jsonEncode(jsonList);
final file = File('index.json');
file.writeAsStringSync(jsonString);
log('JSON file created: ${file.path}');
}
void genAnime(List<Source> jsAnimesourceList) {
List<Source> animeSources = [];
animeSources.addAll(dartAnimesourceList);
animeSources.addAll(jsAnimesourceList);
final List<Map<String, dynamic>> jsonList =
animeSources.map((source) => source.toJson()).toList();
final jsonString = jsonEncode(jsonList);
final file = File('anime_index.json');
file.writeAsStringSync(jsonString);
log('JSON file created: ${file.path}');
}
void genNovel(List<Source> jsNovelSourceList) {
List<Source> novelSources = [];
novelSources.addAll(dartNovelSourceList);
novelSources.addAll(jsNovelSourceList);
final List<Map<String, dynamic>> jsonList =
novelSources.map((source) => source.toJson()).toList();
final jsonString = jsonEncode(jsonList);
final file = File('novel_index.json');
file.writeAsStringSync(jsonString);
log('JSON file created: ${file.path}');
}
List<Source> _searchJsSources(Directory dir) {
List<Source> sourceList = [];
List<FileSystemEntity> entities = dir.listSync();
for (FileSystemEntity entity in entities) {
if (entity is Directory) {
List<FileSystemEntity> entities = entity.listSync();
for (FileSystemEntity entity in entities) {
if (entity is Directory) {
sourceList.addAll(_searchJsSources(entity));
} else if (entity is File && entity.path.endsWith('.js')) {
final regex = RegExp(r'const\s+mangayomiSources\s*=\s*(\[.*?\]);',
dotAll: true);
final defaultSource = Source();
final match = regex.firstMatch(entity.readAsStringSync());
if (match != null) {
for (var sourceJson in jsonDecode(match.group(1)!) as List) {
final langs = sourceJson["langs"] as List?;
Source source = Source.fromJson(sourceJson)
..sourceCodeLanguage = 1
..appMinVerReq =
sourceJson["appMinVerReq"] ?? defaultSource.appMinVerReq
..sourceCodeUrl =
"https://raw.githubusercontent.com/kodjodevf/mangayomi-extensions/$branchName/javascript/${sourceJson["pkgPath"] ?? sourceJson["pkgName"]}";
if (sourceJson["id"] != null) {
source = source..id = int.tryParse("${sourceJson["id"]}");
}
if (langs?.isNotEmpty ?? false) {
for (var lang in langs!) {
final id = sourceJson["ids"]?[lang] as int?;
sourceList.add(Source.fromJson(source.toJson())
..lang = lang
..id =
id ?? 'mangayomi-js-"$lang"."${source.name}"'.hashCode);
}
} else {
sourceList.add(source);
}
}
}
}
}
}
}
return sourceList;
}