Skip to content

Commit

Permalink
redcircle exporter for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
woolfg committed Aug 13, 2024
1 parent eaff8f3 commit 6cda387
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
3 changes: 3 additions & 0 deletions redcircle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
episode_stats*
node_modules
13 changes: 13 additions & 0 deletions redcircle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Extract Stats from RedCircle

## Install

```bash
npm install
```

## Run

- Get the bearer token from a valid RedCircle session (just look it up in an arbitrary api request)
- Store the bearer token in .env
- run the exporter to get all stats: `node exporter.js`
1 change: 1 addition & 0 deletions redcircle/env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BEARER_TOKEN=4789d55a-2463-460c-9899-e04247416c2c
74 changes: 74 additions & 0 deletions redcircle/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require('dotenv').config();
const axios = require('axios');
const fs = require('fs');

// Load bearer token from environment variable
const BEARER_TOKEN = process.env.BEARER_TOKEN;

// Calculate current time in seconds and one year ago in seconds
const currentTime = Math.floor(Date.now() / 1000);

// Define URLs
const statsUrl = `https://app.redcircle.com/api/stats/downloads?arbitraryTimeRange=1639090800%2C${currentTime}&bucketTerms=download.episodeUUID&interval=1y&isUnique=true&showUUID=0ecfdfd7-fda1-4c3d-9515-476727f9df5e&timezone=Europe%2FVienna`;
const episodesUrl = 'https://app.redcircle.com/api/shows/0ecfdfd7-fda1-4c3d-9515-476727f9df5e/episodes';

// Define headers
const headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0',
'Accept': '*/*',
'Accept-Language': 'en-GB,en;q=0.8,de;q=0.6,de-AT;q=0.4,nl;q=0.2',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Referer': 'https://app.redcircle.com/stats',
'Authorization': `Bearer ${BEARER_TOKEN}`
};

// Fetch data from the given URL
async function fetchData(url) {
try {
const response = await axios.get(url, { headers });
return response.data;
} catch (error) {
console.error(`Error fetching data from ${url}:`, error.message);
return null;
}
}

// Combine data from episodes and download stats
async function processStatsAndEpisodes() {
const episodes = await fetchData(episodesUrl);
const downloadStats = await fetchData(statsUrl);

if (!episodes || !downloadStats) {
console.error('Failed to fetch episodes or download stats.');
return;
}

// Map download counts by episode UUID
const downloadCountMap = downloadStats.reduce((map, stat) => {
if (map[stat.pathValues[1]] === undefined) {
map[stat.pathValues[1]] = 0;
}
map[stat.pathValues[1]] += stat.count;
return map;
}, {});

// Combine episodes with their download counts
const result = episodes.map(episode => {
return {
uuid: episode.uuid,
title: episode.title,
guid: episode.guid,
count: downloadCountMap[episode.uuid] || 0,
publishedAt: new Date(episode.publishedAt * 1000).toISOString(),
statsTill: new Date(currentTime * 1000).toISOString(),
};
});

// Write the result to a JSON file
const nowFileString = (new Date()).toISOString().replace(/:/g, '-');
fs.writeFileSync(`episode_stats_${nowFileString}.json`, JSON.stringify(result, null, 2));
console.log(`Wrote episode stats to episode_stats_${nowFileString}.json`);
}

// Execute the main function
processStatsAndEpisodes();
114 changes: 114 additions & 0 deletions redcircle/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions redcircle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"axios": "^1.7.3",
"dotenv": "^16.4.5"
}
}

0 comments on commit 6cda387

Please sign in to comment.