Skip to content

Commit

Permalink
Refactor comment area
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Jan 3, 2025
1 parent 014ef13 commit 5f71d08
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
41 changes: 35 additions & 6 deletions violet/lib/component/eh/eh_headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:violet/component/eh/eh_parser.dart';
import 'package:violet/network/wrapper.dart' as http;

class EHSession {
static EHSession? tryLogin(String id, String pass) {
return null;
}

static Future<String> requestString(String url) async {
static Future<String?> cookie() async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
return prefs.getString('eh_cookies');
}

static Future<String> requestString(String url) async {
final cookie = await EHSession.cookie();
return (await http.get(url, headers: {'Cookie': cookie ?? ''})).body;
}

static Future<String?> requestRedirect(String url) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final cookie = await EHSession.cookie();
Request req = Request('Get', Uri.parse(url))..followRedirects = false;
req.headers['Cookie'] = cookie ?? '';
Client baseClient = Client();
Expand All @@ -27,11 +31,36 @@ class EHSession {
}

static Future<String> postComment(String url, String content) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final cookie = await EHSession.cookie();
return (await http.post(url,
headers: {'Cookie': cookie ?? ''},
body: 'commenttext_new=${Uri.encodeFull(content)}'))
.body;
}

static Future<EHArticle?> fetchArticle(int id, String ehash) async {
final cookie = await EHSession.cookie();

// 1. 설정된 쿠키가 있다면 exhentai에서 먼저 시도
if (cookie != null) {
try {
final html = await EHSession.requestString(
'https://exhentai.org/g/$id/$ehash/?p=0&inline_set=ts_l');
return EHParser.parseArticleData(html);
} catch (_) {}
}

// 2. 설정된 쿠키가 없거나 exh 요청이 실패하면 eh에서 시도
try {
final html = (await http
.get('https://e-hentai.org/g/$id/$ehash/?p=0&inline_set=ts_l'))
.body;
if (!EHParser.validHtml(html)) {
return null;
}
return EHParser.parseArticleData(html);
} catch (_) {}

return null;
}
}
41 changes: 11 additions & 30 deletions violet/lib/pages/article_info/article_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:uuid/uuid.dart';
import 'package:violet/component/eh/eh_headers.dart';
import 'package:violet/component/eh/eh_parser.dart';
import 'package:violet/component/hitomi/related.dart';
import 'package:violet/component/hitomi/tag_translate.dart';
import 'package:violet/database/query.dart';
Expand All @@ -28,7 +27,6 @@ import 'package:violet/database/user/record.dart';
import 'package:violet/locale/locale.dart';
import 'package:violet/model/article_info.dart';
import 'package:violet/model/article_list_item.dart';
import 'package:violet/network/wrapper.dart' as http;
import 'package:violet/other/dialogs.dart';
import 'package:violet/pages/article_info/preview_area.dart';
import 'package:violet/pages/article_info/simple_info.dart';
Expand Down Expand Up @@ -521,34 +519,17 @@ class __CommentAreaState extends State<_CommentArea> {
super.initState();

Future.delayed(const Duration(milliseconds: 100)).then((value) async {
if (widget.queryResult.ehash() != null) {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
if (cookie != null) {
try {
final html = await EHSession.requestString(
'https://exhentai.org/g/${widget.queryResult.id()}/${widget.queryResult.ehash()}/?p=0&inline_set=ts_l');
final article = EHParser.parseArticleData(html);
setState(() {
comments.addAll(article.comment ?? []);
comments.sort((x, y) => x.$1.compareTo(y.$1));
});
return;
} catch (_) {}
}
try {
final html = (await http.get(
'https://e-hentai.org/g/${widget.queryResult.id()}/${widget.queryResult.ehash()}/?p=0&inline_set=ts_l'))
.body;
if (!EHParser.validHtml(html)) {
return;
}
final article = EHParser.parseArticleData(html);
setState(() {
comments.addAll(article.comment ?? []);
comments.sort((x, y) => x.$1.compareTo(y.$1));
});
} catch (_) {}
if (widget.queryResult.ehash() == null) {
return;
}

final article = await EHSession.fetchArticle(
widget.queryResult.id(), widget.queryResult.ehash());
if (article != null) {
setState(() {
comments.addAll(article.comment ?? []);
comments.sort((x, y) => x.$1.compareTo(y.$1));
});
}
});
}
Expand Down

0 comments on commit 5f71d08

Please sign in to comment.