Advertisement
Steadyspaghetti

Untitled

Jul 29th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. const apiKey = '';
  2. const cx = '';
  3. const query = 'george costanza'; // Modify this to curate the type of images you want
  4.  
  5. const url = `https://www.googleapis.com/customsearch/v1?q=${encodeURIComponent(query)}&cx=${cx}&key=${apiKey}&searchType=image&fileType=jpg&imgSize=large&imgType=photo`;
  6.  
  7. const widget = await createWidget();
  8. if (!config.runsInWidget) {
  9. await widget.presentLarge();
  10. }
  11. Script.setWidget(widget);
  12. Script.complete();
  13.  
  14. async function createWidget() {
  15. let widget = new ListWidget();
  16. let selection = await getRandomImage();
  17.  
  18. if (!selection || !selection.image) {
  19. console.error("Selection is undefined or does not have an image.");
  20. let errorText = widget.addText("Failed to load image");
  21. errorText.textColor = Color.red();
  22. errorText.font = Font.boldSystemFont(16);
  23. return widget;
  24. }
  25.  
  26. let imageStack = widget.addStack();
  27. let image = selection.image;
  28.  
  29. let img = imageStack.addImage(image);
  30. img.centerAlignImage();
  31. img.imageSize = new Size(800, 400); // Adjust the size as needed to fit the widget
  32.  
  33. widget.addSpacer();
  34.  
  35. let gradient = new LinearGradient();
  36. gradient.colors = [new Color("#000000", 0), new Color("#000000", 0.5)];
  37. gradient.locations = [0, 1];
  38.  
  39. let titleStack = widget.addStack();
  40. titleStack.backgroundGradient = gradient;
  41. titleStack.addSpacer();
  42.  
  43. let titleText = titleStack.addText(selection.title);
  44. titleText.font = Font.boldSystemFont(16);
  45. titleText.textColor = Color.white();
  46. titleText.rightAlignText();
  47.  
  48. let interval = 1000 * 60 * 15; // Refresh every 15 minutes
  49. widget.refreshAfterDate = new Date(Date.now() + interval);
  50.  
  51. return widget;
  52. }
  53.  
  54. async function getRandomImage() {
  55. try {
  56. let response = await new Request(url).loadJSON();
  57. console.log('API Response:', JSON.stringify(response, null, 2)); // Log the response for debugging
  58.  
  59. if (!response.items || response.items.length === 0) {
  60. console.error("No images found in the response.");
  61. return null;
  62. }
  63.  
  64. let images = response.items;
  65. let num = Math.floor(Math.random() * images.length);
  66. let image = images[num];
  67. let imgRequest = new Request(image.link);
  68. let img = await imgRequest.loadImage();
  69. return { image: img, title: image.title };
  70. } catch (e) {
  71. console.error('Error:', e);
  72. if (e.response) {
  73. let errorText = await e.response.text();
  74. console.error('Error Response:', errorText); // Log the detailed error response
  75. }
  76. return null;
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement