Edits history of script submission #88 for ' Trigger everytime a new item text on HackerNews match at least one mention (hackernews)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    import {
      getState,
      setState,
    } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100;
    
    /**
     * @param mentions Case **insensitive** phrases that should be searched for.
     */
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK);
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i);
        if (
          mentions.find((mention) => {
            if (!item.text) return false;
    
            const m = mention.trim().toLowerCase();
            return item.text.toLowerCase().includes(m);
          })
        ) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by hugo697 377 days ago

  • deno
    import {
      getState,
      setState,
    } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100;
    
    /**
     * @param mentions Case **insensitive** phrases that should be searched for.
     */
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK);
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i);
        if (
          mentions.find((mention) => {
            if (!item.text) return false;
    
            const m = mention.trim().toLowerCase();
            return item.text.toLowerCase().includes(m);
          })
        ) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by admin 1010 days ago

  • deno
    import { getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100
    
    /**
     * @param mentions Case **insensitive** phrases that should be searched for.
     */
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i)
        if (mentions.find((mention) => {
          if(!item.text) return false;
    
          const m = mention.trim().toLowerCase()
          return item.text.toLowerCase().includes(m)
        })) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by adam186 1141 days ago

  • deno
    import { getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100
    
    /**
     * @param mentions Case **insensitive** phrases that should be searched for.
     */
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i)
        if (mentions.find((mention) => {
          if(!item.text) return false;
    
          const m = mention.trim().toLowerCase()
          return item.text.toLowerCase().includes(m)
        })) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by adam186 1176 days ago

  • deno
    import { getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100
    
    /**
     * @param mentions Case **insensitive** phrases that should be searched for.
     */
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i)
        if (mentions.find((mention) => {
          if(!item.text) return false;
    
          const m = mention.trim().toLowerCase()
          return item.text.toLowerCase().includes(m)
        })) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by adam186 1250 days ago

  • deno
    import { getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100
    
    export async function main(mentions: string[]) {
      let lastState = await getState();
      let maxItem = await getMaxItem();
    
      if (!lastState) {
        lastState = maxItem - MAX_LOOKBACK;
      }
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        const item = await getItem(i)
        if (mentions.find((mention) => item.text?.includes(mention))) {
          items.push(item);
        }
      }
      await setState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by adam186 1250 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    
    const MAX_LOOKBACK = 100
    
    export async function main(mentions: string[]) {
      console.log(wmill.getInternalStatePath());
      let lastState = await wmill.getInternalState();
      console.log(`lastState: ${lastState}`);
    
      let maxItem = await getMaxItem();
      console.log(`maxItem: ${maxItem}`);
    
      if (!lastState) {
        console.log(`First run of trigger, looking back to MAX_LOOKBACK (100) elements`);
        lastState = maxItem - MAX_LOOKBACK;
      }
    
      maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)
    
      const items = [];
      for (let i = lastState; i < maxItem; i++) {
        console.log(`fetching id: ${i}`);
        const item = await getItem(i)
        if (mentions.find((mention) => item.text?.includes(mention))) {
          items.push(item);
        }
      }
      await wmill.setInternalState(maxItem);
    
      return items;
    }
    
    export async function getMaxItem() {
      const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
      return Number(await res.text());
    }
    
    export async function getItem(id: number) {
      const res = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
      );
      return res.json();
    }
    

    Submitted by admin 1384 days ago