mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-26 12:01:45 +00:00
requesting images from reddit redirect
This commit is contained in:
parent
2d0c93cf3b
commit
e95ef79af0
@ -23,13 +23,22 @@
|
|||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
update: boolean;
|
update: boolean;
|
||||||
|
subreddit?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data structure to represent subreddit posts
|
||||||
|
interface RedditPosts {
|
||||||
|
sources: string[];
|
||||||
|
index: number;
|
||||||
|
interval: NodeJS.Timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
// System variables
|
// System variables
|
||||||
let imageURL: string | null;
|
let imageURL: string | null;
|
||||||
let uploadData: UploadStat, matrix: Matrix;
|
let uploadData: UploadStat, matrix: Matrix;
|
||||||
let uploadStarted = false,
|
let uploadStarted = false,
|
||||||
submitData: SubmitData;
|
submitData: SubmitData,
|
||||||
|
posts: RedditPosts;
|
||||||
|
|
||||||
// Update matrix over endpoint
|
// Update matrix over endpoint
|
||||||
async function updateMatrix(data: SubmitData) {
|
async function updateMatrix(data: SubmitData) {
|
||||||
@ -121,7 +130,8 @@
|
|||||||
y: parseInt(fdata.get('y')?.toString() ?? '0'),
|
y: parseInt(fdata.get('y')?.toString() ?? '0'),
|
||||||
width: parseInt(fdata.get('w')?.toString() ?? matrix.width.toString()),
|
width: parseInt(fdata.get('w')?.toString() ?? matrix.width.toString()),
|
||||||
height: parseInt(fdata.get('h')?.toString() ?? matrix.height.toString()),
|
height: parseInt(fdata.get('h')?.toString() ?? matrix.height.toString()),
|
||||||
update: fdata.get('update') ? true : false
|
update: fdata.get('update') ? true : false,
|
||||||
|
subreddit: fdata.get('subreddit')?.toString()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Time upload by saving date
|
// Time upload by saving date
|
||||||
@ -141,6 +151,38 @@
|
|||||||
uploadStarted = true;
|
uploadStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchReddit(data: SubmitData) {
|
||||||
|
if (!data.subreddit) return;
|
||||||
|
|
||||||
|
// Request subreddit data
|
||||||
|
const response = await fetch(`/api/reddit?subreddit=${data.subreddit}`);
|
||||||
|
const mdata = (await response.json()) as APIResponse;
|
||||||
|
|
||||||
|
// Ignore failed
|
||||||
|
if (!mdata.success) return;
|
||||||
|
const sources = mdata.results! as string[];
|
||||||
|
if (sources.length === 0) return;
|
||||||
|
|
||||||
|
// Get images and automatically update
|
||||||
|
imageURL = sources[0];
|
||||||
|
posts = {
|
||||||
|
interval: setInterval(() => {
|
||||||
|
uploadStarted = false;
|
||||||
|
if (posts.index >= posts.sources.length) {
|
||||||
|
clearInterval(posts.interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
imageURL = posts.sources[++posts.index];
|
||||||
|
setTimeout(() => (uploadStarted = true), 15);
|
||||||
|
}, 30000),
|
||||||
|
index: 0,
|
||||||
|
sources
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove subreddit tag from submit data
|
||||||
|
delete data.subreddit;
|
||||||
|
}
|
||||||
|
|
||||||
// On client loaded
|
// On client loaded
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
matrix = {
|
matrix = {
|
||||||
@ -232,16 +274,40 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
class="border-2 border-black hover:bg-gray-200 cursor-pointer"
|
class="border-2 border-black border-b-0 hover:bg-gray-200 cursor-pointer"
|
||||||
type="submit"
|
type="submit"
|
||||||
value="Upload!"
|
value="Upload!"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-2">
|
||||||
|
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="subreddit"
|
||||||
|
>Subreddit name:</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
class="border-2 border-black border-b-0 cursor-text pl-1 bg-gray-100"
|
||||||
|
type="text"
|
||||||
|
name="subreddit"
|
||||||
|
id="subreddit"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<input
|
||||||
|
class="border-2 border-black hover:bg-gray-200 cursor-pointer"
|
||||||
|
type="submit"
|
||||||
|
value="Fetch!"
|
||||||
|
/>
|
||||||
{#if imageURL}
|
{#if imageURL}
|
||||||
<img class="mt-5 block ml-auto mr-auto" src={imageURL} alt="User uploaded" />
|
<img class="mt-5 block ml-auto mr-auto" src={imageURL} alt="User uploaded" />
|
||||||
{/if}
|
{/if}
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{#if uploadStarted}
|
{#if uploadStarted}
|
||||||
|
{#if submitData.subreddit}
|
||||||
|
{#await fetchReddit(submitData)}
|
||||||
|
<p>Fetching subreddit data . . .</p>
|
||||||
|
<p>{uploadData.elapsed} seconds elapsed.</p>
|
||||||
|
{/await}
|
||||||
|
{:else}
|
||||||
{#await fileAsDataURL(submitData)}
|
{#await fileAsDataURL(submitData)}
|
||||||
<p>Loading image data . . .</p>
|
<p>Loading image data . . .</p>
|
||||||
<p>{uploadData.elapsed} seconds elapsed.</p>
|
<p>{uploadData.elapsed} seconds elapsed.</p>
|
||||||
@ -264,4 +330,5 @@
|
|||||||
{/await}
|
{/await}
|
||||||
{/await}
|
{/await}
|
||||||
{/await}
|
{/await}
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
|
Loading…
Reference in New Issue
Block a user