« Module:ZoteroAPI » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 20 : | Ligne 20 : | ||
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json' | local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json' | ||
-- | -- Afficher l'URL pour déboguer | ||
local success, | mw.log('Fetching URL: ' .. url) | ||
return mw. | |||
-- Récupérer les données avec getExternalData | |||
local success, result | |||
success, result = pcall(function() | |||
return mw.ext.externalData.getWebData{url = url} | |||
end) | end) | ||
if not success | if not success then | ||
mw.log('Error fetching data: ' .. tostring(result)) | |||
return nil | return nil | ||
end | end | ||
if not result or type(result) ~= "table" then | |||
mw.log('No data or unexpected type: ' .. type(result)) | |||
return nil | return nil | ||
end | end | ||
-- | -- Pour le débogage | ||
if data.data then | mw.log('Response type: ' .. type(result)) | ||
cachedData = | |||
-- ExternalData peut retourner les données dans un format de table spécifique | |||
-- Tentons de trouver la donnée JSON dans cette table | |||
local jsonData | |||
-- Parcourir les résultats pour trouver le contenu JSON | |||
for k, v in pairs(result) do | |||
if type(v) == "string" and v:match("^%s*[{[]") then | |||
-- Cela semble être du JSON | |||
jsonData = v | |||
break | |||
end | |||
end | |||
if not jsonData then | |||
-- Si nous ne trouvons pas de JSON, essayons la première valeur | |||
if result[1] and type(result[1]) == "table" and result[1][1] then | |||
jsonData = result[1][1] | |||
end | |||
end | |||
if not jsonData then | |||
mw.log('No JSON data found in response') | |||
return nil | |||
end | |||
-- Décoder le JSON | |||
local decoded | |||
success, decoded = pcall(mw.text.jsonDecode, jsonData) | |||
if not success or not decoded then | |||
mw.log('Failed to decode JSON: ' .. tostring(decoded)) | |||
return nil | |||
end | |||
-- Traiter les données décodées | |||
if decoded.data then | |||
cachedData = decoded.data | |||
else | else | ||
cachedData = | cachedData = decoded | ||
end | end | ||
| Ligne 45 : | Ligne 86 : | ||
end | end | ||
-- Fonction | -- Fonction de débogage de base | ||
function p. | function p.debugBasic(frame) | ||
local itemKey = frame and frame.args[1] | local itemKey = frame and frame.args[1] | ||
| Ligne 55 : | Ligne 96 : | ||
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json' | local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json' | ||
-- | -- Tester différentes méthodes | ||
local success, | local outputs = {} | ||
return mw. | |||
-- Méthode 1: getExternalData | |||
local success, result = pcall(function() | |||
return mw.ext.externalData.getExternalData{url = url, format = 'json'} | |||
end) | end) | ||
if | table.insert(outputs, "Méthode 1 (getExternalData):") | ||
if success then | |||
table.insert(outputs, " Type: " .. type(result)) | |||
if type(result) == "table" then | |||
table.insert(outputs, " Clés: " .. table.concat(mw.getKeysSortedByValue(result), ", ")) | |||
else | |||
table.insert(outputs, " Valeur: " .. tostring(result)) | |||
end | |||
else | |||
table.insert(outputs, " Erreur: " .. tostring(result)) | |||
end | end | ||
-- Méthode 2: getWebData | |||
return | success, result = pcall(function() | ||
end | return mw.ext.externalData.getWebData{url = url} | ||
end) | |||
table.insert(outputs, "\nMéthode 2 (getWebData):") | |||
if | if success then | ||
table.insert(outputs, " Type: " .. type(result)) | |||
if type(result) == "table" then | |||
table.insert(outputs, " Clés: " .. table.concat(mw.getKeysSortedByValue(result), ", ")) | |||
else | |||
table.insert(outputs, " Valeur: " .. tostring(result)) | |||
end | |||
else | |||
table.insert(outputs, " Erreur: " .. tostring(result)) | |||
end | end | ||
return | return table.concat(outputs, "\n") | ||
end | end | ||