feat: add paginated series inventory with bulk Emby SDK queries (1.3.0)
This commit is contained in:
1 parent
e7e690f60c
commit
208d7045f9
14 files changed
+555
-41
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read-only baseline against official Emby REST API; NOT a deployed MCP test.
|
||||
Run where EMBY_URL and EMBY_API_KEY already exist. Never prints credentials or URLs.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
base = os.environ['EMBY_URL'].rstrip('/')
|
||||
key = os.environ['EMBY_API_KEY']
|
||||
calls = 0
|
||||
|
||||
def get(path, query):
|
||||
global calls
|
||||
calls += 1
|
||||
request = urllib.request.Request(base + path + '?' + urllib.parse.urlencode(query), headers={'X-Emby-Token': key})
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
return json.load(response)
|
||||
|
||||
series = get('/Items', {'SearchTerm': 'Proper People', 'IncludeItemTypes': 'Series', 'Recursive': 'true'})['Items']
|
||||
assert len(series) == 1, 'Expected exactly one Proper People series; select ID explicitly if ambiguous'
|
||||
series_id = series[0]['Id']
|
||||
fields = 'Path,ProviderIds,MediaSources,AlternateMediaSources'
|
||||
common = {'ParentId': series_id, 'Recursive': 'true', 'IncludeItemTypes': 'Episode',
|
||||
'Fields': fields, 'EnableImages': 'false', 'EnableUserData': 'false',
|
||||
'SortBy': 'SortName,Path', 'SortOrder': 'Ascending'}
|
||||
results = []
|
||||
for season in (None, 2022, 2023, 999999):
|
||||
query = dict(common)
|
||||
if season is not None:
|
||||
query['ParentIndexNumber'] = season
|
||||
baseline = get('/Items', {**query, 'StartIndex': 0, 'Limit': 10000})
|
||||
total = baseline['TotalRecordCount']
|
||||
assert len(baseline['Items']) == total, 'Baseline larger than verification cap'
|
||||
seen = []
|
||||
start = 0
|
||||
page_count = 0
|
||||
while True:
|
||||
page = get('/Items', {**query, 'StartIndex': start, 'Limit': 7})
|
||||
page_count += 1
|
||||
assert page['TotalRecordCount'] == total, 'Library changed during pagination'
|
||||
seen.extend(page['Items'])
|
||||
start += len(page['Items'])
|
||||
if start >= total:
|
||||
break
|
||||
assert page['Items'], 'Unexpected premature empty page'
|
||||
ids = [item['Id'] for item in seen]
|
||||
assert len(ids) == len(set(ids)) == total
|
||||
assert set(ids) == {item['Id'] for item in baseline['Items']}
|
||||
assert not get('/Items', {**query, 'StartIndex': total, 'Limit': 7})['Items']
|
||||
if season in (2022, 2023):
|
||||
assert total == {2022: 18, 2023: 17}[season], 'Proper People reference count changed'
|
||||
assert all(item.get('Path', '').lower().endswith('.mkv') for item in seen)
|
||||
assert all(item.get('ParentIndexNumber') == season for item in seen)
|
||||
assert all(item.get('IndexNumber') is not None for item in seen)
|
||||
assert all(item.get('ProviderIds', {}).get('Tvdb') for item in seen)
|
||||
assert all(item.get('MediaSources') and item.get('RunTimeTicks') for item in seen)
|
||||
results.append({'season': season, 'total': total, 'pages_of_7': page_count, 'unique_and_complete': True})
|
||||
# Never rely on Emby's treatment of invalid ParentId (could be server-dependent).
|
||||
# New MCP validates existence AND Series type first; those cases are covered by SDK test doubles.
|
||||
print(json.dumps({'test': 'live_official_REST_baseline_not_new_MCP', 'results': results,
|
||||
'requests': calls, 'episode_detail_requests': 0}, indent=2))
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
// Read-only acceptance test AFTER the separately approved plugin restart.
|
||||
// Run on the Emby host. No tokens or episode paths are printed.
|
||||
$config = $argv[1] ?? '/mnt/nvme-storage/appdata/Emby/plugins/configurations/eMCPy.xml';
|
||||
$x = simplexml_load_file($config);
|
||||
if (!$x) throw new RuntimeException('Cannot read local plugin configuration');
|
||||
$calls = 0;
|
||||
function rpc($method, $params) {
|
||||
global $x, $calls;
|
||||
$calls++;
|
||||
$c = curl_init('http://127.0.0.1:'.(int)$x->McpPort.'/mcp');
|
||||
curl_setopt_array($c, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_POST=>true,
|
||||
CURLOPT_HTTPHEADER=>['Content-Type: application/json','X-Api-Key: '.(string)$x->ApiKey],
|
||||
CURLOPT_POSTFIELDS=>json_encode(['jsonrpc'=>'2.0','id'=>$calls,'method'=>$method,'params'=>$params]),CURLOPT_TIMEOUT=>60]);
|
||||
$body = curl_exec($c); $code = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_close($c);
|
||||
if ($code !== 200) throw new RuntimeException('MCP request failed: HTTP '.$code);
|
||||
$r = json_decode($body,true,512,JSON_THROW_ON_ERROR);
|
||||
if (isset($r['error'])) throw new RuntimeException('MCP JSON-RPC error');
|
||||
return $r['result'];
|
||||
}
|
||||
function requireTrue($ok,$message) { if (!$ok) throw new RuntimeException($message); }
|
||||
function callTool($name,$args,$expectError=false) {
|
||||
$r=rpc('tools/call',['name'=>$name,'arguments'=>$args]);
|
||||
if ($expectError) {requireTrue(!empty($r['isError']), 'Expected MCP tool error');return null;}
|
||||
requireTrue(empty($r['isError']), 'MCP tool failed: '.$name);
|
||||
return json_decode($r['content'][0]['text'],true,512,JSON_THROW_ON_ERROR);
|
||||
}
|
||||
$init=rpc('initialize',(object)[]);
|
||||
requireTrue($init['serverInfo']['version']==='1.3.0','Expected loaded eMCPy 1.3.0');
|
||||
$tools=rpc('tools/list',(object)[]);
|
||||
requireTrue(count($tools['tools'])===18,'Expected 18 tools');
|
||||
$search=callTool('emby_search',['query'=>'Proper People','type'=>'Series']);
|
||||
requireTrue(count($search['items'])===1,'Select explicit series ID; search ambiguous');
|
||||
$id=$search['items'][0]['id'];
|
||||
$results=[];
|
||||
foreach ([null,2022,2023,999999] as $season) {
|
||||
$base=['seriesId'=>$id]; if ($season!==null) $base['season']=$season;
|
||||
$seen=[];$start=0;$revision=null;$pageCount=0;$total=null;
|
||||
do {
|
||||
$a=$base+['start'=>$start,'limit'=>7];if($revision!==null)$a['expectedRevision']=$revision;
|
||||
$p=callTool('emby_series_inventory',$a);$pageCount++;
|
||||
if($total!==null)requireTrue($p['total']===$total,'Total changed');
|
||||
$total=$p['total'];$revision=$p['inventoryRevision'];
|
||||
requireTrue($p['returned']===count($p['items']),'Wrong returned count');
|
||||
foreach($p['items'] as $item) {
|
||||
requireTrue(!isset($seen[$item['id']]),'Duplicate item');$seen[$item['id']]=true;
|
||||
foreach(['id','seriesId','title','seasonNumber','episodeNumber','providerIds','tvdbId','path','mediaSources','runtimeTicks'] as $field)
|
||||
requireTrue(array_key_exists($field,$item),'Missing response field: '.$field);
|
||||
if($season===2022||$season===2023) {
|
||||
requireTrue(str_ends_with(strtolower($item['path']??''),'.mkv'),'Expected MKV');
|
||||
requireTrue($item['seasonNumber']===$season && $item['episodeNumber']!==null,'Missing numbering');
|
||||
requireTrue($item['tvdbId']!==null && !empty($item['mediaSources']) && $item['runtimeTicks']!==null,'Missing metadata');
|
||||
}
|
||||
}
|
||||
requireTrue($p['hasMore']===($start+$p['returned']<$total),'Invalid hasMore');
|
||||
if($p['hasMore']){requireTrue($p['nextStart']>$start,'Pagination stalled');$start=$p['nextStart'];}
|
||||
else requireTrue($p['nextStart']===null,'Last nextStart must be null');
|
||||
} while($p['hasMore']);
|
||||
requireTrue(count($seen)===$total,'Incomplete inventory');
|
||||
if($season===2022||$season===2023)requireTrue($total===($season===2022?18:17),'Reference count changed');
|
||||
$empty=callTool('emby_series_inventory',$base+['start'=>$total]);
|
||||
requireTrue($empty['returned']===0 && !$empty['hasMore'],'End must be empty');
|
||||
$results[]=['season'=>$season,'total'=>$total,'pagesOf7'=>$pageCount];
|
||||
}
|
||||
callTool('emby_series_inventory',['seriesId'=>'not-an-id'],true);
|
||||
callTool('emby_series_inventory',['seriesId'=>'9223372036854775807'],true);
|
||||
callTool('emby_series_inventory',['seriesId'=>$id,'limit'=>201],true);
|
||||
callTool('emby_series_inventory',['seriesId'=>$id,'expectedRevision'=>'wrong'],true);
|
||||
echo json_encode(['version'=>$init['serverInfo']['version'],'results'=>$results,'calls'=>$calls,'episodeDetailCalls'=>0],JSON_PRETTY_PRINT).PHP_EOL;
|
||||
Reference in new issue
Block a user