Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

A sample implementation of Breadboard Service Endpoint (BSE) protocol. For more information, see Breadboard service node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
NodeDescriberResult,
service,
} from "https://esm.town/v/dglazkov/servicefactory";
import {
parseXml,
XmlCdata,
XmlComment,
XmlDocument,
XmlElement,
XmlText,
} from "npm:@rgrove/parse-xml";
const firstChild = (element: XmlElement | XmlDocument) => {
return element.children[0] as XmlElement;
};
const elementsByName = (element: XmlElement, name: string): XmlElement[] => {
return element.children.filter((child) =>
("name" in child) && child.name === name
) as XmlElement[];
};
const elementsToJson = (element: XmlElement) => {
const json: Record<string, string> = {};
for (const child of element.children) {
if ("name" in child) {
const text = ("text" in child) ? child.text : null;
if (text) {
json[child.name] = text;
}
}
}
return json;
};
const parseNews = (xml: XmlDocument) => {
const channel = firstChild(firstChild(xml));
const date = firstChild(elementsByName(channel, "lastBuildDate")[0]).text;
const items = elementsByName(channel, "item").map(elementsToJson);
return { date, items };
};
const getFeedUrl = (query: string) => {
let search = "";
let q = "";
if (query) {
q = `&q=${query}`;
search = "/search";
}
return `https://news.google.com/rss${search}?hl=en-US&gl=US&ceid=US%3Aen${q}`;
};
const getNews = async ({ query }: { query: string }) => {
const response = await fetch(
getFeedUrl(query),
);
if (response.status !== 200) {
const error = await response.text();
return { $error: error };
}
const newsData = await response.text();
const xml = parseXml(newsData);
const result = parseNews(xml);
return { result };
};
const describe = () => ({
title: "Google News",
description:
"Get news from Google News, provided a query. If query is omitted, top news will be returned.",
inputSchema: {
type: "object",
properties: {
query: {
title: "Query",
description:
"The query to supply to Google News. If empty, tops news stories will be returned.",
type: "string",
default: "",
},
},
},
outputSchema: {
type: "object",
properties: {
result: {
title: "News",
type: "object",
},
},
},
} as NodeDescriberResult);
export default service(describe, getNews, { private: true });
dglazkov-privategooglenews.web.val.run
August 25, 2024