Glanceway Glanceway
Alle Quellen

Reddit

Soziales v1.0.0

Hot posts from a subreddit

@codytseng #reddit #social

Konfiguration

Name Schlüssel Typ Erforderlich Standard Beschreibung
Subreddit SUBREDDIT string Ja Subreddit name without r/, multiple with + (e.g., programming+javascript)
Sort SORT select Nein hot Sort order for posts
hotnewtoprising

Quellcode

version: 1.0.0
name: Reddit
description: Hot posts from a subreddit
author: codytseng
author_url: https://github.com/codytseng
category: Social
tags:
  - reddit
  - social

config:
  - key: SUBREDDIT
    name: Subreddit
    type: string
    required: true
    description: Subreddit name without r/, multiple with + (e.g., programming+javascript)
  - key: SORT
    name: Sort
    type: select
    required: false
    default: hot
    description: Sort order for posts
    options:
      - hot
      - new
      - top
      - rising
import type { GlancewayAPI, SourceMethods } from "../../types";

export default (api: GlancewayAPI): SourceMethods => {
  return {
    async refresh() {
      const subreddit = api.config.get("SUBREDDIT") || "programming";
      const sort = api.config.get("SORT") || "hot";

      const response = await api.fetch<{
        data: {
          children: Array<{
            data: {
              id: string;
              title: string;
              url: string;
              permalink: string;
              selftext: string;
              score: number;
              num_comments: number;
              subreddit_name_prefixed: string;
              author: string;
              created_utc: number;
            };
          }>;
        };
      }>(`https://www.reddit.com/r/${subreddit}/${sort}.json?limit=30&raw_json=1`, {
        headers: { "User-Agent": "Glanceway/1.0" },
      });

      if (!response.ok || !response.json) {
        return;
      }

      api.emit(
        response.json.data.children.map((child) => {
          const post = child.data;
          return {
            id: post.id,
            title: post.title,
            subtitle: post.selftext || `↑ ${post.score} · ${post.num_comments} comments · u/${post.author}`,
            url: post.url.startsWith("/")
              ? `https://www.reddit.com${post.url}`
              : post.url,
            timestamp: post.created_utc,
          };
        }),
      );
    },
  };
};