我可以向 sonarr api 发出请求,它将给我以下输出:
curl -sL "http://192.168.2.15:8005/api/series/lookup?term=tvdb:81189&apikey=[[REMOVED]]" | jq .[]
{
"title": "Breaking Bad",
"sortTitle": "breaking bad",
"seasonCount": 5,
"status": "ended",
"overview": "When Walter White, a chemistry teacher, is diagnosed with Stage III cancer and given a prognosis of two years left to live, he chooses to enter a dangerous world of drugs and crime with the intent to secure his family's financial security.",
"network": "Netflix",
"airTime": "21:00",
"images": [
{
"coverType": "banner",
"url": "https://artworks.thetvdb.com/banners/graphical/81189-g21.jpg"
},
{
"coverType": "poster",
"url": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg"
},
{
"coverType": "fanart",
"url": "https://artworks.thetvdb.com/banners/fanart/original/81189-21.jpg"
}
],
"remotePoster": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg",
"seasons": [
{
"seasonNumber": 0,
"monitored": false
},
{
"seasonNumber": 1,
"monitored": true
},
{
"seasonNumber": 2,
"monitored": true
},
{
"seasonNumber": 3,
"monitored": true
},
{
"seasonNumber": 4,
"monitored": true
},
{
"seasonNumber": 5,
"monitored": true
}
],
"year": 2008,
"profileId": 0,
"languageProfileId": 0,
"seasonFolder": false,
"monitored": true,
"useSceneNumbering": false,
"runtime": 47,
"tvdbId": 81189,
"tvRageId": 18164,
"tvMazeId": 169,
"firstAired": "2008-01-20T00:00:00Z",
"seriesType": "standard",
"cleanTitle": "breakingbad",
"imdbId": "tt0903747",
"titleSlug": "breaking-bad",
"certification": "TV-MA",
"genres": [
"Crime",
"Drama",
"Suspense",
"Thriller"
],
"tags": [],
"added": "0001-01-01T00:00:00Z",
"ratings": {
"votes": 31714,
"value": 9.4
},
"qualityProfileId": 0
}
我想从该输出中获取键和值,并用它们创建一个新的有效 json 输出。这些是需要在新输出中的键(+ 值):
title
profileId
tvdbId
titleSlug
images
(及其所有内容和子项)seasons
(及其所有内容和子项)
对于一个键,值需要更改。新值存储在一个变量中:
profileId
(变量$profile_id
)
需要将一些东西添加到新输出中(不在 api 的输出中):
"rootFolderPath": ".....",
(键的值存储在变量中$root_folder_path
)
"addOptions":
{
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": false
}
结果(以上面的示例输出为例)应如下所示:
{
"title": "Breaking Bad",
"rootFolderPath": "/home/cas/plex-media/series",
"profileId": 1,
"tvdbId": 81189,
"titleSlug": "breaking-bad",
"images": [
{
"coverType": "banner",
"url": "https://artworks.thetvdb.com/banners/graphical/81189-g21.jpg"
},
{
"coverType": "poster",
"url": "https://artworks.thetvdb.com/banners/posters/81189-10.jpg"
},
{
"coverType": "fanart",
"url": "https://artworks.thetvdb.com/banners/fanart/original/81189-21.jpg"
}
],
"seasons": [
{
"seasonNumber": 0,
"monitored": false
},
{
"seasonNumber": 1,
"monitored": true
},
{
"seasonNumber": 2,
"monitored": true
},
{
"seasonNumber": 3,
"monitored": true
},
{
"seasonNumber": 4,
"monitored": true
},
{
"seasonNumber": 5,
"monitored": true
}
],
"addOptions":
{
"ignoreEpisodesWithFiles": false,
"ignoreEpisodesWithoutFiles": false,
"searchForMissingEpisodes": false
}
}
我强烈希望它能够在不保存到文件的情况下工作。所以它可以是一小段 bash 代码或单线/长管道,只要它不涉及保存到文件或其他东西。例如,只需完成所有需要完成的工作并将新输出保护在变量中。
您可以使用 将简单的字符串传递给
jq
过滤器--arg
,而 JSON 对象也可以类似地使用--argjson
. 然后使用{}
对象构造来组装所需的输出:请注意,该值
$profile_id
需要作为 JSON 对象传递才能被视为数字。也可以看看: