Skip to content

Commit

Permalink
Logger enhancements (#51)
Browse files Browse the repository at this point in the history
* adjust logger config

* update licenses

* adjust example file

* adjust node ci versions

* update sanity test script

* fix types
  • Loading branch information
RyanHirsch authored Aug 30, 2024
1 parent 4d8fb1c commit 60fc81b
Show file tree
Hide file tree
Showing 7 changed files with 1,405 additions and 647 deletions.
Empty file removed .env.development
Empty file.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IS_LOCAL_DEV=1
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ name: CI
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [lts/*, 12.x, 14.x, 16.x]
node-version: [lts/*, 18.x, 20.x]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Podcast feed parser, originally extracted from podcast index - https://github.co

This package will also identify [new namespace elements](https://github.com/Podcastindex-org/podcast-namespace) and call out the "phases" implemented by the feed in a `pc20support` element.

By default, this will produce log messages that are warnings or errors, but this can be controlled via an environment variable, `PARTYTIME_LOG` can be set to whatever log level you may want.

## Usage

```sh
Expand All @@ -22,9 +24,9 @@ import pt from "podcast-partytime";
pt.checkFeedByUri("https://www.spreaker.com/show/3128218/episodes/feed").then(console.log);

fetch("http://mp3s.nashownotes.com/pc20rss.xml", {
headers: {
headers: {
"user-agent": "partytime/example",
}
},
})
.then((resp) => resp.text())
.then((xml) =>
Expand All @@ -38,9 +40,9 @@ fetch("http://mp3s.nashownotes.com/pc20rss.xml", {

// Parse Feed
fetch("http://mp3s.nashownotes.com/pc20rss.xml", {
headers: {
headers: {
"user-agent": "partytime/example",
}
},
})
.then((resp) => resp.text())
.then((xml) => console.log(pt.parseFeed(xml)));
Expand Down
15 changes: 14 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import dotenv from "dotenv";

dotenv.config();

const trueValues = ["t", "y", "1", "true", "yes"];
// const falseValues = ["f", "n", "0", "false", "no"];

function getBooleanValue(val: string | undefined): boolean {
if (typeof val === "string") {
return trueValues.includes(val.toLowerCase());
}
return false;
}

const isLocalDev = getBooleanValue(process.env.IS_LOCAL_DEV);
const environment = process.env.NODE_ENV ?? "development";
const defaultLevel = new Map([
["development", "info"],
["production", "warn"],
["test", "silent"],
]);

const localDevelopmentLogFallback = isLocalDev ? defaultLevel.get(environment) : undefined;

export default {
logLevel: process.env.LOG ?? defaultLevel.get(environment) ?? "warn",
logLevel: process.env.PARTYTIME_LOG ?? localDevelopmentLogFallback ?? "warn",
environment,
};
43 changes: 30 additions & 13 deletions src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import * as fs from "fs";
import * as path from "path";
import crypto from "crypto";

import fetch from "node-fetch";
import invariant from "tiny-invariant";
import stringify from "fast-json-stable-stringify";
import { getStream$ } from "podping-client";
import { take } from "rxjs/operators";

import { logger } from "./logger";
import { parseFeed } from "./parser";
Expand Down Expand Up @@ -165,18 +165,13 @@ async function getFeed(uri: string): Promise<void> {

if (process.argv[2] === "--latest") {
runPromise(
new Promise((resolve) => {
getStream$()
.pipe(take(1))
.subscribe({
next(val) {
resolve(getFeed(val.url));
},
complete() {
logger.info("complete");
},
});
fetch(`https://api.podcastindex.org/api/1.0/recent/feeds?max=10`, {
headers: getHeaders(),
})
.then((resp) => resp.json())
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
.then((json) => json.feeds.map((x: { url: string }) => x.url))
.then((feedUrls: Array<string>) => Promise.all(feedUrls.map((x) => getFeed(x))))
);
} else if (process.argv[2]) {
runPromise(getFeed(process.argv[2]));
Expand All @@ -190,3 +185,25 @@ function runPromise(prom: Promise<any>): void {
)
.finally(() => process.exit());
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function getHeaders() {
const key = process.env.PI_API_KEY;
const secret = process.env.PI_API_SECRET;
invariant(key);
invariant(secret);
const apiHeaderTime = Math.floor(Date.now() / 1000);
const sha1Algorithm = "sha1";
const sha1Hash = crypto.createHash(sha1Algorithm);
const data4Hash = `${key}${secret}${apiHeaderTime}`;
sha1Hash.update(data4Hash);
const hash4Header = sha1Hash.digest("hex");

return {
"Content-Type": "application/json",
"X-Auth-Date": `${apiHeaderTime}`,
"X-Auth-Key": key,
Authorization: hash4Header,
"User-Agent": `custom/1.0.0`,
};
}
Loading

0 comments on commit 60fc81b

Please sign in to comment.