From 5d4429bf7c06e967a07984a95818475333d7b605 Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Wed, 4 Sep 2024 15:55:15 +0800 Subject: [PATCH] chore: update watchlist --- pkg/importlist/douban/douban.go | 83 +++++++++++++++++++++++++--- pkg/importlist/douban/douban_test.go | 11 ++++ pkg/importlist/importlist.go | 1 + ui/lib/settings/dialog.dart | 2 +- ui/lib/settings/importlist.dart | 28 +++++++++- 5 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 pkg/importlist/douban/douban_test.go diff --git a/pkg/importlist/douban/douban.go b/pkg/importlist/douban/douban.go index dca1456..3aba7bf 100644 --- a/pkg/importlist/douban/douban.go +++ b/pkg/importlist/douban/douban.go @@ -3,17 +3,28 @@ package douban import ( "fmt" "net/http" + "polaris/log" + "polaris/pkg/importlist" + "strconv" + "strings" "github.com/PuerkitoBio/goquery" ) -type DoulistItem struct { - Name string - ImdbID string -} +const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" -func ParseDoulist(doulistUrl string) ([]DoulistItem, error) { - res, err := http.Get(doulistUrl) +func ParseDoulist(doulistUrl string) (*importlist.Response, error) { + if !strings.Contains(doulistUrl, "doulist") { + return nil, fmt.Errorf("not doulist") + } + + req, err := http.NewRequest("GET", doulistUrl, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", ua) + + res, err := http.DefaultClient.Do(req) if err != nil { return nil, err } @@ -26,6 +37,64 @@ func ParseDoulist(doulistUrl string) ([]DoulistItem, error) { if err != nil { return nil, err } - doc.Find("") + doc.Find("div[class=doulist-item]").Each(func(i int, selection *goquery.Selection) { + titleDiv := selection.Find("div[class=title]") + link := titleDiv.Find("div>a") + href, ok := link.Attr("href") + if !ok { + return + } + abstract := selection.Find("div[class=abstract]") + + lines := strings.Split(abstract.Text(), "\n") + year := 0 + for _, l := range lines { + if strings.Contains(l, "年份") { + ppp := strings.Split(l, ":") + if len(ppp) < 2 { + continue + } else { + n := ppp[1] + n1, err := strconv.Atoi(n) + if err != nil { + log.Errorf("convert year number %s to int error: %v", n, err) + continue + } + year = n1 + } + } + } + + item := importlist.Item{ + Title: strings.TrimSpace(link.Text()), + Year: year, + } + _ = item + println(link.Text(), href) + }) return nil, nil } + +func parseDetailPage(url string) (string, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return "", err + } + req.Header.Set("User-Agent", ua) + + res, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer res.Body.Close() + if res.StatusCode != 200 { + return "", fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status) + + } + doc, err := goquery.NewDocumentFromReader(res.Body) + if err != nil { + return "", err + } + _ = doc + return "", nil +} diff --git a/pkg/importlist/douban/douban_test.go b/pkg/importlist/douban/douban_test.go new file mode 100644 index 0000000..d469e62 --- /dev/null +++ b/pkg/importlist/douban/douban_test.go @@ -0,0 +1,11 @@ +package douban + +import ( + "polaris/log" + "testing" +) + +func TestParseDoulist(t *testing.T) { + r, err := ParseDoulist("https://www.douban.com/doulist/166422/") + log.Info(r, err) +} diff --git a/pkg/importlist/importlist.go b/pkg/importlist/importlist.go index 3591c68..f2cafde 100644 --- a/pkg/importlist/importlist.go +++ b/pkg/importlist/importlist.go @@ -2,6 +2,7 @@ package importlist type Item struct { Title string + Year int ImdbID string TvdbID string TmdbID string diff --git a/ui/lib/settings/dialog.dart b/ui/lib/settings/dialog.dart index f6b07ad..87d881a 100644 --- a/ui/lib/settings/dialog.dart +++ b/ui/lib/settings/dialog.dart @@ -17,7 +17,7 @@ Future showSettingDialog( content: SingleChildScrollView( child: SizedBox( width: 400, - child: body, + child: SelectionArea(child: body), ), ), actions: [ diff --git a/ui/lib/settings/importlist.dart b/ui/lib/settings/importlist.dart index 4b9886b..ed0a6c4 100644 --- a/ui/lib/settings/importlist.dart +++ b/ui/lib/settings/importlist.dart @@ -17,6 +17,7 @@ class Importlist extends ConsumerStatefulWidget { } class _ImportlistState extends ConsumerState { + String? _selectedType; @override Widget build(BuildContext context) { var importlists = ref.watch(importlistProvider); @@ -42,16 +43,39 @@ class _ImportlistState extends ConsumerState { Future showImportlistDetails(ImportList list) { final _formKey = GlobalKey(); + setState(() { + _selectedType = list.type; + }); var body = FormBuilder( key: _formKey, initialValue: { "name": list.name, "url": list.url, "qulity": list.qulity, + "type": list.type, "storage_id": list.storageId }, child: Column( children: [ + FormBuilderDropdown( + name: "type", + decoration: const InputDecoration( + labelText: "类型", + hintText: + "Plex Watchlist: https://support.plex.tv/articles/universal-watchlist/"), + items: const [ + DropdownMenuItem(value: "plex", child: Text("Plex Watchlist")), + ], + onChanged: (value) { + setState(() { + _selectedType = value; + }); + }, + ), + _selectedType == "plex" + ? const Text( + "Plex Watchlist: https://support.plex.tv/articles/universal-watchlist/") + : const Text(""), FormBuilderTextField( name: "name", decoration: Commons.requiredTextFieldStyle(text: "名称"), @@ -103,12 +127,12 @@ class _ImportlistState extends ConsumerState { onSubmit() async { if (_formKey.currentState!.saveAndValidate()) { var values = _formKey.currentState!.value; - + return ref.read(importlistProvider.notifier).addImportlist(ImportList( id: list.id, name: values["name"], url: values["url"], - type: "plex", + type: values["type"], qulity: values["qulity"], storageId: values["storage_id"], ));