format.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { Tweet } from './twitter.ts';
  2. class SafeString {
  3. constructor(readonly raw: string) {}
  4. get length(): number {
  5. return this.raw.length;
  6. }
  7. toString(): string {
  8. return this.raw;
  9. }
  10. toJSON(): string {
  11. return this.raw;
  12. }
  13. }
  14. type StringLike = string | SafeString;
  15. function escapeHTML(unsafe: string): StringLike {
  16. return new SafeString(
  17. unsafe
  18. .replace(/&/g, "&")
  19. .replace(/</g, "&lt;")
  20. .replace(/>/g, "&gt;")
  21. .replace(/"/g, "&quot;")
  22. .replace(/'/g, "&#039;")
  23. );
  24. }
  25. function unescapeHTML(safe: string): string {
  26. return safe
  27. .replace(/&amp;/g, "&")
  28. .replace(/&lt;/g, "<")
  29. .replace(/&gt;/g, ">")
  30. .replace(/"/g, "&quot;")
  31. .replace(/'/g, "&#039;");
  32. }
  33. function joinChildren(children: StringLike[]): SafeString {
  34. return new SafeString(children
  35. .map(child => typeof child === 'string' ? escapeHTML(child) : child)
  36. .join('')
  37. );
  38. }
  39. function tag(tag: string, attributes: Record<string, string> = {}, children: StringLike | StringLike[] = []): SafeString {
  40. const attrs = Object.entries(attributes).map(([ key, value ]) => {
  41. return ` ${key}="${escapeHTML(value)}"`;
  42. }).join('');
  43. if (children.length === 0) {
  44. return new SafeString(`<${tag}${attrs} />`);
  45. } else {
  46. const childrenArray = Array.isArray(children) ? children : [children];
  47. const normalizedChildren = joinChildren(childrenArray);
  48. return new SafeString(`<${tag}${attrs}>${normalizedChildren}</${tag}>`);
  49. }
  50. }
  51. function buildTwitterUrl(url: string): string {
  52. return new URL(url, 'https://twitter.com').toString();
  53. }
  54. function buildTweetUrl(tweet: Tweet): string {
  55. return buildTwitterUrl(`/${tweet.user.screen_name}/status/${tweet.id_str}`);
  56. }
  57. function buildProxyUrl(url: string): string {
  58. const search = new URLSearchParams({ target: url }).toString();
  59. return `/__proxy?${search}`;
  60. }
  61. function formatPlainText(text: string): SafeString {
  62. // apparently twitter already escapes the text for you
  63. return new SafeString(text.replace(/\n/g, "<br />"));
  64. }
  65. class TextFormatter {
  66. private splices: { text: StringLike, indices: [number, number] }[];
  67. private media: { type: 'video' | 'img', url: string, loop: boolean, link?: string }[];
  68. private characters: string[];
  69. constructor(readonly tweet: Tweet, readonly useProxy: boolean) {
  70. this.characters = [...tweet.full_text];
  71. this.splices = [];
  72. this.media = [];
  73. for (const { indices, text } of tweet.entities.hashtags) {
  74. const url = buildTwitterUrl(`/hashtag/${text}`);
  75. this.splices.push({
  76. indices,
  77. text: tag('a', { href: url }, `#${text}`),
  78. });
  79. }
  80. const quoteLink = tweet.quoted_status ? buildTweetUrl(tweet.quoted_status) : undefined;
  81. for (const link of tweet.entities.urls) {
  82. if (quoteLink && link.expanded_url === quoteLink) {
  83. // skip links for quoted status that are displayed inline
  84. continue;
  85. }
  86. const url = new URL(link.expanded_url).toString();
  87. this.splices.push({
  88. indices: link.indices,
  89. text: tag('a', { href: url }, link.display_url),
  90. });
  91. }
  92. for (const { indices, name, screen_name } of tweet.entities.user_mentions) {
  93. const url = buildTwitterUrl(`/${screen_name}`);
  94. this.splices.push({
  95. indices: indices,
  96. text: tag('a', { href: url, title: name }, `@${screen_name}`),
  97. });
  98. }
  99. const media = tweet.extended_entities?.media ?? [];
  100. for (const item of media) {
  101. if (item.type === 'photo') {
  102. const url = new URL(item.media_url_https).toString();
  103. this.media.push({ type: 'img', url, loop: false });
  104. } else if (item.video_info !== undefined) {
  105. let max = -1;
  106. let maxUrl: string | undefined = undefined;
  107. for (const variant of item.video_info.variants) {
  108. if (variant.bitrate === undefined) {
  109. continue;
  110. }
  111. if (variant.bitrate > max) {
  112. max = variant.bitrate;
  113. maxUrl = variant.url;
  114. }
  115. }
  116. const loop = item.type === 'animated_gif';
  117. if (maxUrl !== undefined) {
  118. const url = new URL(maxUrl).toString();
  119. this.media.push({ type: 'video', url, loop });
  120. } else {
  121. const url = new URL(item.media_url_https).toString();
  122. this.media.push({ type: 'img', url, link: item.expanded_url, loop });
  123. }
  124. }
  125. }
  126. }
  127. getRange(start: number, end?: number): string {
  128. const max = this.tweet.display_text_range[1];
  129. return this.characters.slice(start, end ?? max).join('');
  130. }
  131. headerHTML(): SafeString {
  132. const date = new Date(this.tweet.created_at);
  133. const dateOptions = {
  134. weekday: 'short',
  135. year: 'numeric',
  136. month: '2-digit',
  137. day: '2-digit',
  138. hour: '2-digit',
  139. minute: '2-digit',
  140. timeZoneName: 'short',
  141. } as const;
  142. const imageUrl = new URL(this.tweet.user.profile_image_url_https).toString();
  143. const imageSrc = this.useProxy ? buildProxyUrl(imageUrl) : imageUrl;
  144. const profileUrl = buildTwitterUrl(`/${this.tweet.user.screen_name}`);
  145. const tweetUrl = buildTweetUrl(this.tweet);
  146. const html = [
  147. tag('img', { loading: 'lazy', src: imageSrc, height: '24px', width: '24px' }),
  148. ' ',
  149. tag('strong', {}, this.tweet.user.name),
  150. ' ',
  151. tag('a', { href: profileUrl }, `@${this.tweet.user.screen_name}`),
  152. tag('br'),
  153. 'Posted ',
  154. tag('a', { href: tweetUrl }, date.toLocaleString(this.tweet.lang, dateOptions)),
  155. ];
  156. return joinChildren(html);
  157. }
  158. bodyHTML(): SafeString {
  159. const splices = this.splices
  160. .filter(({ indices }) => indices[0] !== undefined && indices[1] !== undefined)
  161. .sort((a, b) => a.indices[0] - b.indices[0]);
  162. let index = 0;
  163. const html: StringLike[] = [];
  164. for (const { text, indices } of splices) {
  165. const start = index;
  166. const end = indices[0];
  167. html.push(formatPlainText(this.getRange(start, end)));
  168. html.push(text);
  169. index = indices[1];
  170. }
  171. html.push(formatPlainText(this.getRange(index)));
  172. for (const { type, url, link, loop } of this.media) {
  173. html.push(tag('br'));
  174. html.push(tag('br'));
  175. const src = this.useProxy ? buildProxyUrl(url) : url;
  176. if (type === 'img') {
  177. html.push(tag('a', { href: link ?? url }, [
  178. tag('img', { loading: 'lazy', src }),
  179. ]));
  180. } else if (type === 'video') {
  181. html.push(tag('video', { controls: '', src, loop: `${loop}` }));
  182. }
  183. }
  184. return joinChildren(html);
  185. }
  186. toHTML(): SafeString {
  187. return joinChildren([ this.headerHTML(), tag('br'), this.bodyHTML() ]);
  188. }
  189. }
  190. const STYLES = `
  191. body > div {
  192. margin: 10px;
  193. padding: 10px;
  194. border: solid 1px gray;
  195. border-radius: 10px;
  196. }
  197. blockquote {
  198. padding: 10px;
  199. border: solid 1px lightgray;
  200. border-radius: 10px;
  201. }
  202. div {
  203. max-width: 600px;
  204. }
  205. img, video {
  206. max-width: 100%;
  207. }
  208. `;
  209. export function timelineAsHTML(tweets: Tweet[]): string {
  210. const body = tweets.map(tweet => {
  211. const displayTweet = tweet.retweeted_status ?? tweet;
  212. const children: StringLike[] = [];
  213. children.push(new TextFormatter(displayTweet, true).toHTML());
  214. const quoteTweet = displayTweet.quoted_status;
  215. if (quoteTweet !== undefined) {
  216. children.push(tag('blockquote', {}, new TextFormatter(quoteTweet, true).toHTML()));
  217. }
  218. return tag('div', {}, children);
  219. }).join('\n');
  220. return `
  221. <html>
  222. <head>
  223. <style>${STYLES}</style>
  224. </head>
  225. <body>${body}</body>
  226. </html>
  227. `;
  228. }
  229. export function timelineAsJSON(username: string, tweets: Tweet[]): string {
  230. const items = tweets.map(tweet => {
  231. const displayTweet = tweet.retweeted_status ?? tweet;
  232. const children: StringLike[] = [];
  233. children.push(new TextFormatter(displayTweet, false).bodyHTML());
  234. const quoteTweet = displayTweet.quoted_status;
  235. if (quoteTweet !== undefined) {
  236. children.push(tag('blockquote', {}, [
  237. new TextFormatter(quoteTweet, false).toHTML(),
  238. ]));
  239. }
  240. const html = joinChildren(children);
  241. return {
  242. id: tweet.id_str,
  243. title: unescapeHTML(tweet.full_text.split('\n')[0]),
  244. url: buildTweetUrl(tweet),
  245. content_html: html,
  246. date_published: new Date(tweet.created_at).toISOString(),
  247. authors: [{
  248. name: `${displayTweet.user.name} - @${displayTweet.user.screen_name}`,
  249. url: buildTwitterUrl(`/${displayTweet.user.screen_name}`),
  250. avatar: displayTweet.user.profile_image_url_https,
  251. }],
  252. };
  253. });
  254. return JSON.stringify({
  255. version: '1.1',
  256. title: `Twitter @${username}`,
  257. home_page_url: buildTwitterUrl(`/${username}`),
  258. items,
  259. });
  260. }