Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat: Update scripts to for Cloudflare Workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjuaneight committed Nov 6, 2021
1 parent 684d311 commit ec52d0d
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 252 deletions.
14 changes: 6 additions & 8 deletions src/airtable-upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import "https://deno.land/x/[email protected]/load.ts";

import { AirtableError, AirtableResp, RecordData } from "./typings.d.ts";
import { AirtableError, AirtableResp, RecordData } from './typings.d.ts';

/**
* Upload podcast record object to Airtable
Expand All @@ -16,10 +14,10 @@ export const airtableUpload = async (
record: RecordData
): Promise<string> => {
const options: RequestInit = {
method: "POST",
method: 'POST',
headers: {
Authorization: `Bearer ${Deno.env.get("AIRTABLE_API")}`,
"Content-Type": "application/json",
Authorization: `Bearer ${AIRTABLE_API}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
records: [
Expand All @@ -34,7 +32,7 @@ export const airtableUpload = async (

try {
const response: Response = await fetch(
`${Deno.env.get("AIRTABLE_BOOKMARKS_ENDPOINT")}/${table}`,
`${AIRTABLE_BOOKMARKS_ENDPOINT}/${table}`,
options
);
const results: AirtableResp | AirtableError = await response.json();
Expand All @@ -50,7 +48,7 @@ export const airtableUpload = async (
const recordFields = (results as AirtableResp).records[0].fields;

return (
(recordFields.title || recordFields.tweet || recordFields.content) ?? ""
(recordFields.title || recordFields.tweet || recordFields.content) ?? ''
);
} catch (error) {
throw new Error(`Uploading repos to Airtable: \n ${error}`);
Expand Down
8 changes: 4 additions & 4 deletions src/bookmark-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { airtableUpload } from "./airtable-upload.ts";
import { airtableUpload } from './airtable-upload.ts';

import { BookmarkingResponse } from "./typings.d.ts";
import { BookmarkingResponse } from './typings.d.ts';

/**
* Upload article|comic to Airtable.
Expand Down Expand Up @@ -29,8 +29,8 @@ export const bookmarkPage = async (
tags,
});

return { success: true, message: airtableResp, source: "bookmarkPage" };
return { success: true, message: airtableResp, source: 'bookmarkPage' };
} catch (error) {
return { success: false, message: error, source: "bookmarkPage" };
return { success: false, message: error, source: 'bookmarkPage' };
}
};
78 changes: 39 additions & 39 deletions src/bookmark-podcasts.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { airtableUpload } from "./airtable-upload.ts";
import { airtableUpload } from './airtable-upload.ts';

import {
BookmarkingResponse,
ParsingPatterns,
ParsingService,
BookmarkData ,
} from "./typings.d.ts";
BookmarkData,
} from './typings.d.ts';

// list of regular expressions to find and replace
const parsing: ParsingPatterns = {
castro: {
title: new RegExp(/<h1>(.*)<\/h1>/, "g"),
creator: new RegExp(/<h2><a\shref=".*"\salt=".*">(.*)<\/a><\/h2>/, "g"),
url: new RegExp(/<source\ssrc="(.*)"\stype="audio\/mp3">/, "g"),
title: new RegExp(/<h1>(.*)<\/h1>/, 'g'),
creator: new RegExp(/<h2><a\shref=".*"\salt=".*">(.*)<\/a><\/h2>/, 'g'),
url: new RegExp(/<source\ssrc="(.*)"\stype="audio\/mp3">/, 'g'),
},
overcast: {
title: new RegExp(
/<h2\sclass="margintop0 marginbottom0"\sclass="title">(.*)<\/h2>/,
"g"
'g'
),
creator: new RegExp(/<a\shref="\/itunes\d+.*"\s?>(.*)<\/a>/, "g"),
url: new RegExp(/<source\ssrc="(.*)"\stype="audio\/mp3"\s?\/>/, "g"),
creator: new RegExp(/<a\shref="\/itunes\d+.*"\s?>(.*)<\/a>/, 'g'),
url: new RegExp(/<source\ssrc="(.*)"\stype="audio\/mp3"\s?\/>/, 'g'),
},
title: [
new RegExp(/^S\d+\s/, "g"),
new RegExp(/^Ep\.\d{1,3}\s?/, "g"),
new RegExp(/^([a-zA-Z\D\s]+)?#\d{1,3}:?\s/, "g"),
new RegExp(/^Hasty Treat\s-\s/, "g"),
new RegExp(/^(Bonus|BONUS):\s?/, "g"),
new RegExp(/^Ep\.\s/, "g"),
new RegExp(/\s\sOvercast/, "g"),
new RegExp(/\s-\sYouTube/, "g"),
new RegExp(/\s+on Vimeo/, "g"),
new RegExp(/\s-\sEp\.?\s\d+$/, "g"),
new RegExp(/:\sArticles\sof\sInterest\s#\d+$/, "g"),
new RegExp(/\s\D\s([0-9A-Za-z]+\s)+\D\s(Overcast)/, "g"),
new RegExp(/^S\d+\s/, 'g'),
new RegExp(/^Ep\.\d{1,3}\s?/, 'g'),
new RegExp(/^([a-zA-Z\D\s]+)?#\d{1,3}:?\s/, 'g'),
new RegExp(/^Hasty Treat\s-\s/, 'g'),
new RegExp(/^(Bonus|BONUS):\s?/, 'g'),
new RegExp(/^Ep\.\s/, 'g'),
new RegExp(/\s\sOvercast/, 'g'),
new RegExp(/\s-\sYouTube/, 'g'),
new RegExp(/\s+on Vimeo/, 'g'),
new RegExp(/\s-\sEp\.?\s\d+$/, 'g'),
new RegExp(/:\sArticles\sof\sInterest\s#\d+$/, 'g'),
new RegExp(/\s\D\s([0-9A-Za-z]+\s)+\D\s(Overcast)/, 'g'),
new RegExp(
/(\D\d{1,3}\s\D\s)|(\d{1,3}\s\D\s)|(\w\d\D\w\d\s\D\s)|(\w+\s\d{1,3}\D\s)|(\d{1,3}\D\s)/,
"g"
'g'
),
new RegExp(/\s()(\s[A-Za-z]+)+/, "g"),
new RegExp(/\s$/, "g"),
new RegExp(/\s()(\s[A-Za-z]+)+/, 'g'),
new RegExp(/\s$/, 'g'),
],
};

Expand All @@ -56,9 +56,9 @@ const paramCleaner = (data: string, pattern: RegExp): string => {
const match = data.match(pattern);

if (match && match?.length > 0) {
return match[0].replace(pattern, "$1");
return match[0].replace(pattern, '$1');
}
const error = "Param Cleaner: Unable to find match.";
const error = 'Param Cleaner: Unable to find match.';

console.error(error);
throw new Error(error);
Expand All @@ -73,8 +73,8 @@ const paramCleaner = (data: string, pattern: RegExp): string => {
*/
const escapedString = (str: string): string =>
str
.replace(/(["':]+)/g, "\\$1")
.replace(/([,]+)/g, "\\$1")
.replace(/(["':]+)/g, '\\$1')
.replace(/([,]+)/g, '\\$1')
.replace(/"/g, '"');

/**
Expand All @@ -88,8 +88,8 @@ const escapedString = (str: string): string =>
const titleCleaner = (string: string, patterns: RegExp[]): string => {
let cleanTitle = string;

patterns.forEach((regexp) => {
cleanTitle = cleanTitle.replace(regexp, "");
patterns.forEach(regexp => {
cleanTitle = cleanTitle.replace(regexp, '');
});

return escapedString(cleanTitle);
Expand All @@ -107,23 +107,23 @@ const titleCleaner = (string: string, patterns: RegExp[]): string => {
const getPodcastDetails = async (
url: string,
source: string
): Promise<BookmarkData > => {
): Promise<BookmarkData> => {
const request = await fetch(url);

try {
const response = await request.text();
// flatten doc; remove breakpoints and excessive spaces
const post = response
.replace(/\n\s+/g, "")
.replace(/\n/g, "")
.replace(/\s+/g, " ");
.replace(/\n\s+/g, '')
.replace(/\n/g, '')
.replace(/\s+/g, ' ');
// extract details from doc
const service = parsing[source] as ParsingService;
const title = paramCleaner(post, service.title);
const creator = paramCleaner(post, service.creator);
const link = paramCleaner(post, service.url).replace(
/^(.*)\.(mp3).*/g,
"$1.$2"
'$1.$2'
);

return {
Expand All @@ -150,14 +150,14 @@ export const bookmarkPodcasts = async (
tags: string[]
): Promise<BookmarkingResponse> => {
try {
const podcastData = await getPodcastDetails(url, "castro");
const airtableResp = await airtableUpload("Podcasts", {
const podcastData = await getPodcastDetails(url, 'castro');
const airtableResp = await airtableUpload('Podcasts', {
...podcastData,
tags,
});

return { success: true, message: airtableResp, source: "bookmarkPodcasts" };
return { success: true, message: airtableResp, source: 'bookmarkPodcasts' };
} catch (error) {
return { success: false, message: error, source: "bookmarkPodcasts" };
return { success: false, message: error, source: 'bookmarkPodcasts' };
}
};
10 changes: 5 additions & 5 deletions src/bookmark-reddits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { airtableUpload } from "./airtable-upload.ts";
import { airtableUpload } from './airtable-upload.ts';

import { BookmarkingResponse, RedditData } from "./typings.d.ts";
import { BookmarkingResponse, RedditData } from './typings.d.ts';

/**
* Get post details via Reddit API.
Expand Down Expand Up @@ -43,13 +43,13 @@ export const bookmarkReddits = async (
): Promise<BookmarkingResponse> => {
try {
const redditData = await getRedditDetails(url);
const airtableResp = await airtableUpload("Reddits", {
const airtableResp = await airtableUpload('Reddits', {
...redditData,
tags,
});

return { success: true, message: airtableResp, source: "bookmarkReddits" };
return { success: true, message: airtableResp, source: 'bookmarkReddits' };
} catch (error) {
return { success: false, message: error, source: "bookmarkReddits" };
return { success: false, message: error, source: 'bookmarkReddits' };
}
};
26 changes: 12 additions & 14 deletions src/bookmark-tweets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import "https://deno.land/x/[email protected]/load.ts";

import { airtableUpload } from "./airtable-upload.ts";
import { airtableUpload } from './airtable-upload.ts';

import {
BookmarkingResponse,
TwitterData,
TwitterResponse,
} from "./typings.d.ts";
} from './typings.d.ts';

// Match unicode and convert to emoji code
const emojiRange = new RegExp(
Expand Down Expand Up @@ -66,7 +64,7 @@ const expandLinks = async (url: string): Promise<string> => {

if (!request.url) {
console.error({
message: "Expand Links: unable to expand URL.",
message: 'Expand Links: unable to expand URL.',
status: request.status,
});

Expand All @@ -93,7 +91,7 @@ const expandShortLink = async (str: string, regex: RegExp): Promise<string> => {
const promises: Promise<string>[] = [];
const pattern = new RegExp(regex);

// deno-lint-ignore no-unused-vars
// eslint-disable-next-line no-unused-vars
str.replace(pattern, (match, ...args) => {
const promise = expandLinks(match);
promises.push(promise);
Expand All @@ -102,7 +100,7 @@ const expandShortLink = async (str: string, regex: RegExp): Promise<string> => {
});

const data = await Promise.all(promises);
const replacer = () => data.shift() ?? "";
const replacer = () => data.shift() ?? '';

return str.replace(regex, replacer);
} catch (error) {
Expand All @@ -119,10 +117,10 @@ const expandShortLink = async (str: string, regex: RegExp): Promise<string> => {
*/
const cleanUrl = (url: string): string => {
const updatedStr = url
.replace(/(https\:\/\/([a-z]+\.)?twitter\.com\/)/g, "")
.replace(/(https:\/\/([a-z]+\.)?twitter\.com\/)/g, '')
.replace(
/.*\/status\/([0-9]+)(.*)?/g,
"https://api.twitter.com/2/tweets/$1"
'https://api.twitter.com/2/tweets/$1'
);

return updatedStr;
Expand All @@ -144,8 +142,8 @@ const getTweetDetails = async (url: string): Promise<TwitterData> => {
`${endpoint}?tweet.fields=created_at&user.fields=username&expansions=author_id`,
{
headers: {
Authorization: `Bearer ${Deno.env.get("TWITTER_KEY")}`,
"Content-Type": "application/json",
Authorization: `Bearer ${TWITTER_KEY}`,
'Content-Type': 'application/json',
},
}
);
Expand Down Expand Up @@ -182,13 +180,13 @@ export const bookmarkTweets = async (
): Promise<BookmarkingResponse> => {
try {
const tweetData = await getTweetDetails(url);
const airtableResp = await airtableUpload("Tweets", {
const airtableResp = await airtableUpload('Tweets', {
...tweetData,
tags,
});

return { success: true, message: airtableResp, source: "bookmarkTweets" };
return { success: true, message: airtableResp, source: 'bookmarkTweets' };
} catch (error) {
return { success: false, message: error, source: "bookmarkTweets" };
return { success: false, message: error, source: 'bookmarkTweets' };
}
};
16 changes: 7 additions & 9 deletions src/bookmark-vimeos.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import "https://deno.land/x/[email protected]/load.ts";

import { airtableUpload } from "./airtable-upload.ts";
import { airtableUpload } from './airtable-upload.ts';

import {
BookmarkData,
BookmarkingResponse,
VimeoResponse,
} from "./typings.d.ts";
} from './typings.d.ts';

/**
* Convert video url to API ready endpoint. Extracts video ID.
Expand All @@ -18,7 +16,7 @@ import {
const cleanUrl = (url: string): string => {
const updatedStr = url.replace(
/(https:\/\/vimeo\.com\/)(.*)/g,
"https://api.vimeo.com/videos/$2"
'https://api.vimeo.com/videos/$2'
);

return updatedStr;
Expand All @@ -38,7 +36,7 @@ const getVimeoDetails = async (url: string): Promise<BookmarkData> => {
const endpoint = cleanUrl(url);
const request = await fetch(endpoint, {
headers: {
Authorization: `Bearer ${Deno.env.get("VIMEO_KEY")}`,
Authorization: `Bearer ${VIMEO_KEY}`,
},
});
const response: VimeoResponse = await request.json();
Expand Down Expand Up @@ -68,13 +66,13 @@ export const bookmarkVimeo = async (
): Promise<BookmarkingResponse> => {
try {
const vimeoData = await getVimeoDetails(url);
const airtableResp = await airtableUpload("Videos", {
const airtableResp = await airtableUpload('Videos', {
...vimeoData,
tags,
});

return { success: true, message: airtableResp, source: "bookmarkVimeo" };
return { success: true, message: airtableResp, source: 'bookmarkVimeo' };
} catch (error) {
return { success: false, message: error, source: "bookmarkVimeo" };
return { success: false, message: error, source: 'bookmarkVimeo' };
}
};
Loading

0 comments on commit ec52d0d

Please sign in to comment.