74 }
75
76 console.log(`Fetching contributions for ${username}`);
77 try {
78 let vals;
88 }
89
90 console.log(`Fetched ${vals.length} contributions`);
91 const contributionData = processVals(vals, useVersions);
92 console.log(`Processed ${contributionData.length} contributions`);
269}
270
271export default (typeof Deno !== "undefined" && Deno.env.get("valtown")) ? app.fetch : app;
272
55 if (urlQuery) {
56 setQuery(urlQuery);
57 fetchResults(urlQuery);
58 }
59 }, []);
60
61 const fetchResults = async (searchQuery) => {
62 setLoading(true);
63 setError(null);
64 try {
65 const response = await fetch(`/api/search?query=${encodeURIComponent(searchQuery)}`);
66 if (!response.ok) {
67 throw new Error(`HTTP error! status: ${response.status}`);
70 setResults(data.results);
71 } catch (e) {
72 console.error("Error fetching results:", e);
73 setError("An error occurred while fetching results. Please try again.");
74 } finally {
75 setLoading(false);
81 if (query) {
82 window.history.pushState({}, "", `?query=${encodeURIComponent(query)}`);
83 fetchResults(query);
84 }
85 };
221if (typeof document !== "undefined") { client(); }
222
223async function fetchDBpediaResults(query: string) {
224 const lookupResponse = await fetch(
225 `https://lookup.dbpedia.org/api/search?query=${encodeURIComponent(query)}&format=json&maxResults=10`
226 );
251
252 try {
253 const results = await fetchDBpediaResults(query);
254
255 if (path === "/api/raw") {
25
26 if (user && user.id) {
27 // Automatically fetches more pages as needed.
28 for await (const basicVal of client.users.vals.list(user.id, {
29 limit: 100,
48 const valId = typeof valOrId === 'string' ? valOrId : valOrId.id; // Determine if input is a val or valId
49
50 // Automatically fetches more pages as needed.
51 for await (const versionListResponse of client.vals.versions.list(valId, {
52 limit: 20, // Default limit
67 const valsArray = []; // Initialize an array to store values with versions
68 if (user && user.id) {
69 // Automatically fetches more pages as needed.
70 const semaphore = new Sema(concurrency); // Assuming 5 concurrent operations are allowed
71 const processVal = async (basicVal) => {
24 if (urlQuery) {
25 setQuery(urlQuery);
26 fetchResults(urlQuery);
27 }
28 }, []);
29
30 const fetchResults = async (searchQuery) => {
31 const response = await fetch(`/api/search?query=${encodeURIComponent(searchQuery)}`);
32 const data = await response.json();
33 setResults(data);
38 if (query) {
39 window.history.pushState({}, "", `?query=${encodeURIComponent(query)}`);
40 fetchResults(query);
41 }
42 };
125if (typeof document !== "undefined") { client(); }
126
127async function fetchDBpediaResults(query: string) {
128 const lookupResponse = await fetch(
129 `https://lookup.dbpedia.org/api/search?query=${encodeURIComponent(query)}&format=json&maxResults=5`,
130 );
143 `;
144
145 const sparqlResponse = await fetch(
146 `https://dbpedia.org/sparql?query=${encodeURIComponent(sparqlQuery)}&format=json`,
147 );
171 }
172
173 const results = await fetchDBpediaResults(query);
174
175 return new Response(JSON.stringify(results), {
12 const [author, name] = subdomain.split("-");
13
14 const resp = await fetch(`https://api.val.town/v1/alias/${author}/${name}`);
15 if (!resp.ok) {
16 console.error("failed to fetch val infos");
17 return;
18 }
57 }
58
59 const fetch_response = await fetch(url.toString(), {
60 method: req.method,
61 headers: new Headers({
64 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
65 "Accept-Language": "en-US,en;q=0.5",
66 "Sec-Fetch-Site": "cross-site",
67 "Sec-Fetch-Mode": "navigate",
68 "Sec-Fetch-User": "?1",
69 "Sec-Fetch-Dest": "document",
70 "Referer": "https://www.google.com/",
71 "sec-ch-ua": `"Not A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"`,
76 }),
77 });
78 const contentType = fetch_response.headers.get("content-type");
79 const response_text = await fetch_response.text();
80 // We only handle html, pass content through otherwise
81 if (!contentType?.includes("text/html")) {
2import React, { useEffect, useState } from "https://esm.sh/react";
3import { createRoot } from "https://esm.sh/react-dom/client";
4import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
5
6const subreddits = ["IdiotsInCars"];
7
8const fetchWithImage = async (subreddit: string) => {
9 const { data: { children } } = await fetchJSON(
10 `https://www.reddit.com/r/${subreddit}.json`,
11 );
24};
25
26const fetchAllReddits = async () => {
27 const posts = (await Promise.all(subreddits.map(fetchWithImage))).flat();
28 return posts;
29};
45
46 useEffect(() => {
47 fetch("/api/posts").then(ok => ok.json())
48 .then(posts => setPosts(posts));
49 }, []);
64export default async function server(req: Request): Promise<Response> {
65 if (req.url.endsWith("/api/posts")) {
66 const posts = await fetchAllReddits();
67 posts.sort((a, b) => b.score - a.score);
68 return Response.json(posts);
548 req: Request,
549): Promise<Response> {
550 return await app.fetch(req);
551}
2import React, { useEffect, useState } from "https://esm.sh/react";
3import { createRoot } from "https://esm.sh/react-dom/client";
4import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
5
6const subreddits = ["Battletops", "AverageBattlestations", "desksetup"];
7
8const fetchWithImage = async (subreddit: string) => {
9 const { data: { children } } = await fetchJSON(
10 `https://www.reddit.com/r/${subreddit}.json`,
11 );
24};
25
26const fetchAllReddits = async () => {
27 const posts = (await Promise.all(subreddits.map(fetchWithImage))).flat();
28 return posts;
29};
45
46 useEffect(() => {
47 fetch("/api/posts").then(ok => ok.json())
48 .then(posts => setPosts(posts));
49 }, []);
64export default async function server(req: Request): Promise<Response> {
65 if (req.url.endsWith("/api/posts")) {
66 const posts = await fetchAllReddits();
67 posts.sort((a, b) => b.score - a.score);
68 return Response.json(posts);
12 * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
13 * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
14 * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
15 * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
16 * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.