Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url_only option to random_picture to improve plugin compability #1026

Closed
wants to merge 2 commits into from

Conversation

JBrandonS
Copy link

This patch adds a url_only option to tf.web.random_picture() to enable better compatibility with other plugins, i.e. Banners. This value defaults to false to maintain the current behavior. Documentation has been updated to add this option.

@slambert
Copy link

I was just looking for this! Would love to see it merged.

@JBrandonS
Copy link
Author

@slambert

In the mean time, what I have done is save the following script and enabled it in templater settings. This allows you to use the function and not have to use a custom version.

I wish I had kept attribution for the real author of this code but I found it somewhere online.

// scripts/random_banner.js
async function random_banner(size, query) {
	const resp = await fetch(`https://source.unsplash.com/random/${size ?? ""}?${query ?? ""}`);
	if (!resp.ok) {
		return "Error: Couldn't fetch banner";
	}
	return resp.url;
}

module.exports = random_banner;

Example usage:

---
cssclass: note
aliases:
tags:
banner: "<% tp.user.random_banner("1080x800", "banner," + tp.file.title) %>"
---

@slambert
Copy link

Being sort of tenacious I kept going and trying to solve it myself without the code.

I spent some time and figured out the unsplash URL structure. A lot of stuff I found about the URL structure is out of date and no longer works.

Here's what works best:

https://source.unsplash.com/random?sig=UNIQUESTRING/&SEARCHTERM

So

https://source.unsplash.com/random?sig=1/&birthday
https://source.unsplash.com/random?sig=2/&birthday

would give you this:


But I didn't want repeat images if I had the same search term, so I needed that sig= value to be more complex that just 1 or 2.

So for a daily note, I used the name of the note as the string. I just change the search keyword every so often depending on my mood:
banner: "https://source.unsplash.com/random?sig=<% tp.file.title.slice(0,10) %>/&abstract"

And for other files, I used the file creation date for the string. I also added a prompt so I could enter in a keyword for the image.

banner: "https://source.unsplash.com/random?<% tp.file.creation_date("YYYYmmDDHHmm") %>/&<% tp.system.prompt('What photo?', 'abstract', false, false) %>"

And it works!

I didn't bother trying I tried to incorporate a size specification into the URL because I am tenacious. But couldn't get anything to work. Again, everything I found in a search was out of date or didn't work.

@Zachatoo
Copy link
Collaborator

Zachatoo commented Oct 1, 2024

Going to close this in favor of #1385, since I believe it fulfills the same use case, thanks!

@Zachatoo Zachatoo closed this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants