search download

This commit is contained in:
Simon Ding
2024-07-07 15:02:40 +08:00
parent 1c75ceb6ba
commit 6debad6eb9
6 changed files with 72 additions and 10 deletions

13
pkg/utils/utils.go Normal file
View File

@@ -0,0 +1,13 @@
package utils
import "unicode"
func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}

View File

@@ -49,9 +49,9 @@ func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) {
}
type searchAndDownloadIn struct {
Title string `json:"title"`
Season int `json:"season"`
Episode int `json:"episode"`
ID int `json:"id"`
Season int `json:"season"`
Episode int `json:"episode"`
}
func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
@@ -64,8 +64,16 @@ func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
if err != nil {
return nil, errors.Wrap(err, "connect transmission")
}
log.Infof("search episode resources link: %v", in)
series := s.db.GetSeriesDetails(in.ID)
if series == nil {
return nil, fmt.Errorf("no tv series of id %v", in.ID)
}
res := s.searchTvWithTorznab(in.Title, in.Season, in.Episode)
res := s.searchTvWithTorznab(series.OriginalName, in.Season, in.Episode)
if len(res) == 0 {
return "", fmt.Errorf("no resource found")
}
r1 := res[0]
log.Infof("found resource to download: %v", r1)
torrent, err := trc.Download(r1.Magnet)
@@ -78,7 +86,9 @@ func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
// return nil, errors.Wrap(err, "download torrent")
// }
log.Errorf("success add %s to download task", r1.Name)
return nil, nil
return gin.H{
"name": r1.Name,
}, nil
}
type downloadClientIn struct {

View File

@@ -6,6 +6,7 @@ class APIs {
static final settingsUrl = "$_baseUrl/api/v1/setting/do";
static final watchlistUrl = "$_baseUrl/api/v1/tv/watchlist";
static final seriesDetailUrl = "$_baseUrl/api/v1/tv/series/";
static final searchAndDownloadUrl = "$_baseUrl/api/v1/indexer/download";
static const tmdbImgBaseUrl = "https://image.tmdb.org/t/p/w500/";

View File

@@ -55,7 +55,7 @@ class MyApp extends StatelessWidget {
routes: [
GoRoute(
path: WelcomePage.route,
builder: (context, state) => WelcomePage(),
builder: (context, state) => const WelcomePage(),
),
GoRoute(
path: SearchPage.route,
@@ -63,7 +63,7 @@ class MyApp extends StatelessWidget {
),
GoRoute(
path: SystemSettingsPage.route,
builder: (context, state) => SystemSettingsPage(),
builder: (context, state) => const SystemSettingsPage(),
),
GoRoute(
path: TvDetailsPage.route,
@@ -107,3 +107,16 @@ class MyApp extends StatelessWidget {
);
}
}
CustomTransitionPage buildPageWithDefaultTransition<T>({
required BuildContext context,
required GoRouterState state,
required Widget child,
}) {
return CustomTransitionPage<T>(
key: state.pageKey,
child: child,
transitionsBuilder: (context, animation, secondaryAnimation, child) =>
FadeTransition(opacity: animation, child: child),
);
}

View File

@@ -30,7 +30,6 @@ class _TvDetailsPageState extends State<TvDetailsPage> {
@override
void initState() {
super.initState();
_querySeriesDetails();
}
@@ -62,7 +61,12 @@ class _TvDetailsPageState extends State<TvDetailsPage> {
),
Text("${ep.title}", textAlign: TextAlign.left),
const Expanded(child: Text("")),
IconButton(onPressed: () {}, icon: const Icon(Icons.search))
IconButton(
onPressed: () {
_searchAndDownload(
context, seriesId, ep.seasonNumber!, ep.episodeNumber!);
},
icon: const Icon(Icons.search))
],
),
);
@@ -148,6 +152,24 @@ class _TvDetailsPageState extends State<TvDetailsPage> {
details = SeriesDetails.fromJson(rsp.data);
});
}
void _searchAndDownload(BuildContext context, String seriesId, int seasonNum,
int episodeNum) async {
var resp = await Dio().post(APIs.searchAndDownloadUrl, data: {
"id": int.parse(seriesId),
"season": seasonNum,
"episode": episodeNum,
});
var sp = ServerResponse.fromJson(resp.data);
if (sp.code != 0 && context.mounted) {
Utils.showAlertDialog(context, sp.message);
return;
}
var name = (sp.data as Map<String, dynamic>)["name"];
if (context.mounted) {
Utils.showSnakeBar(context, "$name 开始下载...");
}
}
}
class SeriesDetails {

View File

@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
class Utils {
static Future<void> showAlertDialog(BuildContext context, String msg) async {
return showDialog<void>(
context: context,
barrierDismissible: true, // user must tap button!
@@ -28,4 +27,8 @@ class Utils {
},
);
}
static showSnakeBar(BuildContext context, String msg) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
}