Skip to content

Commit

Permalink
🐛 Support for all in integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 6, 2020
1 parent c3d3d9a commit 7beb74f
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/api/clockify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ClockifyResult = {
}[];

const getTimeData = async (date: Date) => {
if (integrationConfig("clockify").entries) {
if (integrationConfig("clockify", "entries")) {
const {
data,
}: {
Expand Down
2 changes: 1 addition & 1 deletion src/api/google-fit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const saveData = async (data: fitness_v1.Schema$Session[]) => {
}
});
for await (const sessionType of Object.keys(itemsByDateAndType)) {
if (integrationConfig("google-fit")[sessionType])
if (integrationConfig("google-fit", sessionType))
for await (const sessionDate of Object.keys(itemsByDateAndType[sessionType])) {
await write(
join(".", "data", `google-fit-${sessionType.replace(/_/g, "-")}`, "daily", sessionDate, "sessions.json"),
Expand Down
20 changes: 10 additions & 10 deletions src/api/last-fm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const fetchTracks = async (date: Date, page = 1) => {
};

const getLastFmTracks = async (date: Date, page = 1) => {
if (integrationConfig("last-fm").history) {
if (integrationConfig("last-fm", "history")) {
console.log("Last.fm: Fetching tracks for", dayjs(date).format("YYYY-MM-DD"));
const tracks = await fetchTracks(date, page);
const itemsByDate: { [index: string]: ITrack[] } = {};
Expand Down Expand Up @@ -62,7 +62,7 @@ export default class LastDotFm implements Integration {
}
console.log("Last.fm: Added daily summaries");

if (integrationConfig("last-fm")["top-albums"]) {
if (integrationConfig("last-fm", "top-albums")) {
const topAlbumsWeekly = await lastFm.user.getTopAlbums({
user: config("lastfmUsername"),
period: "7day",
Expand All @@ -75,7 +75,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-tracks"]) {
if (integrationConfig("last-fm", "top-tracks")) {
const topTracksWeekly = await lastFm.user.getTopTracks({
user: config("lastfmUsername"),
period: "7day",
Expand All @@ -88,7 +88,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-artists"]) {
if (integrationConfig("last-fm", "top-artists")) {
const topArtistsWeekly = await lastFm.user.getTopArtists({
user: config("lastfmUsername"),
period: "7day",
Expand All @@ -101,7 +101,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-albums"]) {
if (integrationConfig("last-fm", "top-albums")) {
const topAlbumsMonthly = await lastFm.user.getTopAlbums({
user: config("lastfmUsername"),
period: "1month",
Expand All @@ -114,7 +114,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-tracks"]) {
if (integrationConfig("last-fm", "top-tracks")) {
const topTracksMonthly = await lastFm.user.getTopTracks({
user: config("lastfmUsername"),
period: "1month",
Expand All @@ -127,7 +127,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-artists"]) {
if (integrationConfig("last-fm", "top-artists")) {
const topArtistsMonthly = await lastFm.user.getTopArtists({
user: config("lastfmUsername"),
period: "1month",
Expand All @@ -140,7 +140,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-albums"]) {
if (integrationConfig("last-fm", "top-albums")) {
const topAlbumsYearly = await lastFm.user.getTopAlbums({
user: config("lastfmUsername"),
period: "12month",
Expand All @@ -153,7 +153,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-tracks"]) {
if (integrationConfig("last-fm", "top-tracks")) {
const topTracksYearly = await lastFm.user.getTopTracks({
user: config("lastfmUsername"),
period: "12month",
Expand All @@ -166,7 +166,7 @@ export default class LastDotFm implements Integration {
);
}

if (integrationConfig("last-fm")["top-artists"]) {
if (integrationConfig("last-fm", "top-artists")) {
const topArtistsYearly = await lastFm.user.getTopArtists({
user: config("lastfmUsername"),
period: "12month",
Expand Down
8 changes: 4 additions & 4 deletions src/api/oura-ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cosmicSync("stethoscope");

const updateOuraDailyData = async (date: Date) => {
const formattedDate = dayjs(date).format("YYYY-MM-DD");
if (integrationConfig("oura-ring").weight) {
if (integrationConfig("oura-ring", "weight")) {
const {
data: healthData,
}: {
Expand All @@ -43,7 +43,7 @@ const updateOuraDailyData = async (date: Date) => {
);
console.log("Oura: Added summary data");
}
if (integrationConfig("oura-ring").sleep) {
if (integrationConfig("oura-ring", "sleep")) {
const {
data: sleepData,
}: {
Expand All @@ -70,7 +70,7 @@ const updateOuraDailyData = async (date: Date) => {
JSON.stringify(sleepData.sleep, null, 2)
);
}
if (integrationConfig("oura-ring").readiness) {
if (integrationConfig("oura-ring", "readiness")) {
const {
data: readinessData,
}: {
Expand All @@ -97,7 +97,7 @@ const updateOuraDailyData = async (date: Date) => {
JSON.stringify(readinessData.readiness, null, 2)
);
}
if (integrationConfig("oura-ring").activity) {
if (integrationConfig("oura-ring", "activity")) {
const {
data: activityData,
}: {
Expand Down
4 changes: 2 additions & 2 deletions src/api/pocket-casts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export default class PocketCastsI implements Integration {
console.log("Pocket Casts: Starting...");
await pocketCasts.login();

if (integrationConfig("pocket-casts").library) {
if (integrationConfig("pocket-casts", "library")) {
const podcasts = (await pocketCasts.getList()).podcasts;
await write(join(".", "data", "pocket-casts-podcasts", "library.json"), JSON.stringify(podcasts, null, 2));
console.log("Pocket Casts: Added library");
}

if (integrationConfig("pocket-casts").history) {
if (integrationConfig("pocket-casts", "history")) {
let items: Episode[] = [];
try {
const years = await readdir(join(".", "data", "pocket-casts-podcasts", "daily"));
Expand Down
6 changes: 3 additions & 3 deletions src/api/rescuetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const updateRescueTimeDailyData = async (date: Date) => {
const day = dayjs(date).format("DD");
const formattedDate = dayjs(date).format("YYYY-MM-DD");

if (integrationConfig("rescuetime")["top-categories"]) {
if (integrationConfig("rescuetime", "top-categories")) {
console.log("RescueTime: Adding data for", date);
const topCategories = (
await axios.get(
Expand All @@ -65,7 +65,7 @@ const updateRescueTimeDailyData = async (date: Date) => {
JSON.stringify(topCategoriesData, null, 2)
);
}
if (integrationConfig("rescuetime")["top-activities"]) {
if (integrationConfig("rescuetime", "top-activities")) {
const topActivities = (
await axios.get(
`https://www.rescuetime.com/anapi/data?format=json&key=${config(
Expand All @@ -89,7 +89,7 @@ const updateRescueTimeDailyData = async (date: Date) => {
);
}

if (integrationConfig("rescuetime").overview) {
if (integrationConfig("rescuetime", "overview")) {
const topOverview = (
await axios.get(
`https://www.rescuetime.com/anapi/data?format=json&key=${config(
Expand Down
8 changes: 4 additions & 4 deletions src/api/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class Spotify implements Integration {
api.setAccessToken(data.body.access_token);
console.log("Spotify: Refreshed access token");

if (integrationConfig("spotify").history) {
if (integrationConfig("spotify", "history")) {
const history = await api.getMyRecentlyPlayedTracks();
const itemsByDate: { [index: string]: Array<any> } = {};
for await (const item of history.body.items) {
Expand All @@ -137,7 +137,7 @@ export default class Spotify implements Integration {
const month = date.format("MM");
const day = date.format("DD");

if (integrationConfig("spotify").library) {
if (integrationConfig("spotify", "library")) {
const library = await api.getMySavedTracks();
const libraryItems = cleanSpotifyTracksResponse(library.body.items.map((item) => item.track)).map(
(item, index) => ({
Expand All @@ -152,7 +152,7 @@ export default class Spotify implements Integration {
console.log("Spotify: Added library");
}

if (integrationConfig("spotify")["top-tracks"]) {
if (integrationConfig("spotify", "top-tracks")) {
const shortTermTopTracks = cleanSpotifyTracksResponse(
(await api.getMyTopTracks({ time_range: "short_term" })).body.items
);
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class Spotify implements Integration {
console.log("Spotify: Added long-term top tracks");
}

if (integrationConfig("spotify")["top-artists"]) {
if (integrationConfig("spotify", "top-artists")) {
const shortTermTopArtists = cleanSpotifyArtistsResponse(
(await api.getMyTopArtists({ time_range: "short_term" })).body.items
);
Expand Down
4 changes: 2 additions & 2 deletions src/api/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Tweet {
}

const getRecentTweets = async (count: number = 100) => {
if (integrationConfig("twitter").tweets) {
if (integrationConfig("twitter", "tweets")) {
const response: Array<Tweet> = await client.get("statuses/user_timeline", {
screen_name: config("twitterScreenName") || "",
count,
Expand All @@ -46,7 +46,7 @@ const getRecentTweets = async (count: number = 100) => {
}
}

if (integrationConfig("twitter").likes) {
if (integrationConfig("twitter", "likes")) {
const response: Array<Tweet> = await client.get("favorites/list", {
screen_name: config("twitterScreenName") || "",
count,
Expand Down
2 changes: 1 addition & 1 deletion src/api/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const client = new WakaTimeClient(config("wakatimeApiKey") || "example");
const updateWakatimeDailyData = async (date: Date) => {
const formattedDate = dayjs(date).format("YYYY-MM-DD");
console.log("WakaTime: Adding data for", formattedDate);
if (integrationConfig("wakatime").summary) {
if (integrationConfig("wakatime", "summary")) {
const summary = await client.getMySummary({
dateRange: {
startDate: formattedDate,
Expand Down
10 changes: 3 additions & 7 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { config, cosmicSync } from "@anandchowdhary/cosmic";
import { ensureFile, writeFile } from "fs-extra";
cosmicSync("stethoscope");

export const integrationConfig = (service: string) => {
export const integrationConfig = (service: string, key: string) => {
const configs: { [index: string]: { [index: string]: boolean } } = config("integrations") || {};
const items = configs[service] || {};
if (items.all) {
Object.keys(items).forEach((key) => {
items[key] = true;
});
}
return items;
if (items.all) return true;
return items[key];
};

export const write = async (name: string, contents: any) => {
Expand Down

0 comments on commit 7beb74f

Please sign in to comment.