const dayjs = require('dayjs');
const getHeadlines = require('./utils/getHeadlines');
const issue = require('./utils/issue');
const fs = require('fs');

// run every Monday at 00:01 UTC
const run = async (date) => {
  const contents = await getHeadlines(date);
  console.log(contents)
  const res = await issue.open({
    owner: 'headllines',
    repo: 'hackernews-weekly',
    title: `Hacker News Weekly Top 10 @${new Date(date).toISOString().slice(0, 10)}`,
    body: contents
  });
  const issueNumber = res.data.number;

  
  await issue.lock({
    owner: 'headllines',
    repo: 'hackernews-weekly', 
    issueNumber,
  });
  const readmeContent = `
# Hacker News Weekly Top 10 posts

[![](https://badgen.net/badge/icon/RSS/orange?icon=rss&label)](http://rsshub.app/github/issue/headllines/hackernews-weekly) 
[![](https://badgen.net/badge/icon/Telegram?icon=telegram&label)](https://t.me/headllines) 
[![](https://img.shields.io/github/issues-raw/headllines/hackernews-weekly?label=Issues&logo=github&style=social)](https://github.com/headllines/hackernews-weekly/issues)
[![](https://img.shields.io/github/watchers/headllines/hackernews-weekly?style=social)](https://github.com/headllines/hackernews-weekly/watchers)
[![Follow on Feeds Pub](https://img.shields.io/badge/dynamic/json?label=follow&query=%24.data.feed.followerCount&url=https%3A%2F%2Fapi.feeds.pub%2Fgraphql%3Fquery%3Dquery%2520feed%28%2524id%253A%2520String%21%29%257B%2520feed%28id%253A%2520%2524id%29%2520%257B%2520followerCount%2520%257D%2520%257D%26variables%3D%257B%2522id%2522%253A%2520%2522https%3A%2F%2Frsshub.app%2Fgithub%2Fissue%2Fheadllines%2Fhackernews-weekly%2522%257D%26operationName%3Dfeed&style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEUAAABFCAYAAAAcjSspAAAACXBIWXMAAAInAAACJwG+ElQIAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAASkSURBVHgB7Ztdcts2EMf/oDyeNO5MnBOUN4hzAtNvrZ0H8QSimwPEPoHlE9g5gZkTSJ46mU77YPYEVd/6yN5AnWkzrVNxuxAhN3UA0YL46eA3Y30AkDT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zAybKNHc045ikpxWFxRiiAKJZ7WkGq+lDt1v+u/Q+yaTJXW5e1LL4nXl2rjO7ZFMi+m/DCrEx5IcUq/zCvGbcTHbOUhTh61pNB/DicMxZ+5n0CynuQW4hV0M0xMqiqABLx9I4Ip9Gsz1ejsdLhiV5hr/Z/K2Fo/1r21BYLAyulH8Io9GLgB2wLLQ/ExzWZ5T9Js+aNF0uXdDI8S518Qlaio1P8Q3tKR4IFRfDuokTRYONKKmh3ccDwVmKBieKBieKBgtRHpmCtKLTjp1hZVHkPsbQ9fmK8jlgK4rWWpaXLrqDbT5FK4oqWHUeN300OFE0OFE02KYjU13r7Ob973gA2GXziY5xZwXKE83N/NdF2VifT8mPXHlHXAN6knEFry2pRIfD4XA4usm//qauBcoh1b8AAAAASUVORK5CYII=)](https://feeds.pub/feed/https%3A%2F%2Frsshub.app%2Fgithub%2Fissue%2Fheadllines%2Fhackernews-weekly)

## How to subscribe

Top posts are stored in [issues](https://github.com/headllines/hackernews-weekly/issues). You can subscribe by [watching this repo](#how-does-it-work) or via [RSS](https://feeds.pub/feed/http%3A%2F%2Frsshub.app%2Fgithub%2Fissue%2Fheadllines%2Fhackernews-weekly).. 

Email and other ways to subscribe updates are still under development, you can join [the mailing list](https://headllines.com) to get notified.

## How does it work

1. A script fetches top posts on hackernews every day
2. It opens an issue on this repo and store the headlines
3. People can subscribe to new issue by watching this repo or via [RSS](https://feeds.pub/feed/http%3A%2F%2Frsshub.app%2Fgithub%2Fissue%2Fheadllines%2Fhackernews-weekly)

<img src="https://timqian-imgs.s3.ap-southeast-1.amazonaws.com/2020-09-Screen%20Shot%202020-09-01%20at%205.15.54%20PM.png" alt="hackernews weekly" width="400"/>

## Thanks

- [Algolia](https://hn.algolia.com/) for the hacker news API
- [RSSHub](https://github.com/diygod/rsshub) for generating RSS file from issues
- [Feeds.Pub](https://feeds.pub) for hosting the RSS feed
- [github-trending-repos](https://github.com/vitalets/github-trending-repos) for inspiration

## Related headline collectors

- [Hacker News Daily](https://github.com/headllines/hackernews-Daily)
- [Hacker News Monthly](https://github.com/headllines/hackernews-monthly)

## Contribute

We at [headllines.com](https://headllines.com) build open source headline collectors, if you are interested in writing an headline collector and join this orgnization, feel free to join our [telegram group](https://t.me/headllines)

## Sponsors

[琚致远](https://github.com/juzhiyuan) | [Bytebase](https://bytebase.com/) | [Madao](https://madao.me/) | [SecondState](https://bit.ly/3gfWwps)

[Become a sponsor](https://github.com/sponsors/timqian)

## Authors

- [timqian](https://github.com/timqian)
- [leadream](https://github.com/leadream)

> Updated at ${new Date()}
  `;
  fs.writeFileSync('./README.md', readmeContent, 'utf8');
}

run(new Date())
  .catch(err => {throw err});
// run(dayjs().subtract(3, 'days').toDate());
// run(dayjs().subtract(10, 'days').toDate());
// run(dayjs().subtract(17, 'days').toDate());
// run(dayjs().subtract(24, 'days').toDate());
// run(dayjs().subtract(31, 'days').toDate());
// run(dayjs().subtract(38, 'days').toDate());
// run(dayjs().subtract(45, 'days').toDate());
// run(dayjs().subtract(7 * 7 + 3, 'days').toDate());
// run(dayjs().subtract(7* 8 + 3, 'days').toDate());
// run(dayjs().subtract(7* 9 + 3, 'days').toDate());
// run(dayjs().subtract(7* 10 + 3, 'days').toDate());
