トリガーの場合のみfetchでエラー

実現したいこと

Google Apps Scriptを使用して、pubmedのopen accessの論文から画像を保存しようとしています。GASのエディタから手動で実行すると問題なく動くのですが、トリガーで毎日定期的にmain()を実行しようとするとUrlFetchApp.fetch(figure.url)のところで403エラーが出ます。画像のurlの取得は問題ないようです。トリガーでもエラーが起きないようにしたいです。

発生している問題・エラーメッセージ

Exception: Request failed for https://www.ncbi.nlm.nih.gov returned code 403. Truncated server response: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans... (use muteHttpExceptions option to examine full response) at main(コード:5:34)

該当のソースコード

javascript

1function main() {2 const id = 36439924;//論文のID例3 const figures = getPaperFigureByID(id);4 for (const figure of figures) {5 const response = UrlFetchApp.fetch(figure.url);6 const fileName = `${id}_${figure.cid}.jpg`;7 const fileBlob = response.getBlob().setName(fileName); 8 DriveApp.createFile(fileBlob);9 }10}11 12function getPaperFigureByID(id) {13 const url = `https://pubmed.ncbi.nlm.nih.gov/${id}/`;14 const xml = UrlFetchApp.fetch(url).getContentText();15 const matches = xml.matchAll(/<a\s+class="figure-link"\s+href="(.*?)"/g);16 const figures = [];17 let counter = 1 ;18 for (const match of matches) {19 const url = match[1];20 const cid = "image_" + counter ;21 counter++;22 figures.push({url, cid});23 }24 return figures;25}

コメントを投稿

0 コメント