Skip to content

Commit ad7aa8e

Browse files
authored
fix: args work now (#54)
1 parent e077f27 commit ad7aa8e

File tree

4 files changed

+83
-39
lines changed

4 files changed

+83
-39
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
uses: actions/setup-node@v4
2828
with:
2929
node-version-file: package.json
30+
cache: npm
3031

3132
- name: Install dependencies
3233
run: npm ci
@@ -37,5 +38,16 @@ jobs:
3738
- name: Lint
3839
run: npm run lint
3940

40-
- name: Test
41+
- name: Unit tests
4142
run: npm run test:run
43+
44+
- name: Integration tests
45+
run: |
46+
node dist/index.js --lineupId=USA-DITV751-X --timespan=3 --postalCode=80020 --outputFile=dtv.xml
47+
node dist/index.js --lineupId=USA-OTA80020 --timespan=3 --postalCode=80020 --outputFile=ota.xml
48+
49+
# Error if they are the same
50+
if cmp -s dtv.xml ota.xml; then
51+
echo "DTV and OTA outputs are the same, which is unexpected."
52+
exit 1
53+
fi

src/config.ts

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,64 @@
11
import { UserAgent } from "./useragents.js";
22

3-
export const config = {
4-
baseUrl: "https://tvlistings.gracenote.com/api/grid",
5-
lineupId:
3+
function processLineupId(): string {
4+
const lineupId =
65
process.env["LINEUP_ID"] ||
76
process.argv.find((arg) => arg.startsWith("--lineupId="))?.split("=")[1] ||
8-
"USA-lineupId-DEFAULT",
9-
timespan:
10-
process.env["TIMESPAN"] ||
11-
process.argv.find((arg) => arg.startsWith("--timespan="))?.split("=")[1] ||
12-
"6",
13-
country:
14-
process.env["COUNTRY"] ||
15-
process.argv.find((arg) => arg.startsWith("--country="))?.split("=")[1] ||
16-
"USA",
17-
postalCode:
18-
process.env["POSTAL_CODE"] ||
19-
process.argv
20-
.find((arg) => arg.startsWith("--postalCode="))
21-
?.split("=")[1] ||
22-
"30309",
23-
pref:
24-
process.env["PREF"] ||
25-
process.argv.find((arg) => arg.startsWith("--pref="))?.split("=")[1] ||
26-
"",
27-
timezone: process.env.TZ || "America/New_York",
28-
userAgent:
29-
process.env["USER_AGENT"] ||
30-
process.argv.find((arg) => arg.startsWith("--userAgent="))?.split("=")[1] ||
31-
UserAgent,
32-
outputFile:
33-
process.env["OUTPUT_FILE"] ||
34-
process.argv
35-
.find((arg) => arg.startsWith("--outputFile="))
36-
?.split("=")[1] ||
37-
"xmltv.xml",
38-
};
7+
"USA-lineupId-DEFAULT";
8+
9+
if (lineupId.includes("OTA")) {
10+
return "USA-lineupId-DEFAULT";
11+
}
12+
13+
return lineupId;
14+
}
15+
16+
function getHeadendId(lineupId: string): string {
17+
const match = lineupId.match(/^(USA|CAN)-(.*?)(?:-[A-Z]+)?$/);
18+
19+
return match?.[2] || "lineup";
20+
}
21+
22+
export function getConfig() {
23+
const lineupId = processLineupId();
24+
const headendId = getHeadendId(lineupId);
25+
26+
return {
27+
baseUrl: "https://tvlistings.gracenote.com/api/grid",
28+
lineupId,
29+
headendId,
30+
timespan:
31+
process.env["TIMESPAN"] ||
32+
process.argv
33+
.find((arg) => arg.startsWith("--timespan="))
34+
?.split("=")[1] ||
35+
"6",
36+
country:
37+
process.env["COUNTRY"] ||
38+
process.argv.find((arg) => arg.startsWith("--country="))?.split("=")[1] ||
39+
"USA",
40+
postalCode:
41+
process.env["POSTAL_CODE"] ||
42+
process.argv
43+
.find((arg) => arg.startsWith("--postalCode="))
44+
?.split("=")[1] ||
45+
"30309",
46+
pref:
47+
process.env["PREF"] ||
48+
process.argv.find((arg) => arg.startsWith("--pref="))?.split("=")[1] ||
49+
"",
50+
timezone: process.env.TZ || "America/New_York",
51+
userAgent:
52+
process.env["USER_AGENT"] ||
53+
process.argv
54+
.find((arg) => arg.startsWith("--userAgent="))
55+
?.split("=")[1] ||
56+
UserAgent,
57+
outputFile:
58+
process.env["OUTPUT_FILE"] ||
59+
process.argv
60+
.find((arg) => arg.startsWith("--outputFile="))
61+
?.split("=")[1] ||
62+
"xmltv.xml",
63+
};
64+
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { writeFileSync } from "node:fs";
22
import { getTVListings } from "./tvlistings.js";
33
import { buildXmltv } from "./xmltv.js";
4-
import { config } from "./config.js";
4+
import { getConfig } from "./config.js";
5+
6+
const config = getConfig();
57

68
function isHelp() {
79
if (process.argv.includes("--help")) {

src/tvlistings.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { config } from "./config.js";
1+
import { getConfig } from "./config.js";
2+
3+
const config = getConfig();
24

35
export interface Program {
46
/** "title": "GMA3" */
@@ -82,7 +84,7 @@ function buildUrl(time: number, timespan: number): string {
8284
const params = {
8385
lineupId: config.lineupId,
8486
timespan: timespan.toString(),
85-
headendId: "lineupId",
87+
headendId: config.headendId,
8688
country: config.country,
8789
timezone: config.timezone,
8890
postalCode: config.postalCode,
@@ -91,6 +93,8 @@ function buildUrl(time: number, timespan: number): string {
9193
aid: "orbebb",
9294
languagecode: "en-us",
9395
time: time.toString(),
96+
device: "X",
97+
userId: "-",
9498
};
9599

96100
const urlParams = new URLSearchParams(params).toString();
@@ -119,7 +123,7 @@ export async function getTVListings(): Promise<GridApiResponse> {
119123
}).then(async (response) => {
120124
if (!response.ok) {
121125
throw new Error(
122-
`Failed to fetch: ${response.status} ${response.statusText}`
126+
`Failed to fetch: ${response.status} ${response.statusText}`,
123127
);
124128
}
125129
const chunkData = (await response.json()) as GridApiResponse;

0 commit comments

Comments
 (0)