Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
liruifengv committed Nov 10, 2023
1 parent a310bd8 commit 0c33e9e
Show file tree
Hide file tree
Showing 38 changed files with 885 additions and 1,178 deletions.
64 changes: 32 additions & 32 deletions .github/workflows/get_up.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
name: ci

on:
schedule:
- cron: "0 23 * * *"
schedule:
- cron: "0 23 * * *"

jobs:
CreateImage:
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
matrix:
node-version: [18]
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
with:
persist-credentials: false
CreateImage:
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
matrix:
node-version: [18]
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3
with:
persist-credentials: false

- name: Install PNPM
uses: pnpm/action-setup@v2
with:
version: ^7.0
- name: Install PNPM
uses: pnpm/action-setup@v2
with:
version: ^7.0

- name: Install Deps
run: pnpm install --no-frozen-lockfile
- name: Install Deps
run: pnpm install --no-frozen-lockfile

- name: Build
run: pnpm run build
- name: Build
run: pnpm run build

- name: Create Image
run: pnpm run start --cookie "${{ secrets.BING_COOKIE }}"
- name: Create Image
run: pnpm run start --cookie "${{ secrets.BING_COOKIE }}"

- name: Push New Pic
run: |
git config --local user.email "liruifeng1024@gmail.com"
git config --local user.name "${{ github.actor }}"
git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git add .
git commit -m "generated today pic" || echo "no need to commit "
git push -f || echo "no file change"
- name: Push New Pic
run: |
git config --local user.email "liruifeng1024@gmail.com"
git config --local user.name "${{ github.actor }}"
git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git add .
git commit -m "generated today pic" || echo "no need to commit "
git push -f || echo "no file change"
13 changes: 13 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/dist
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/prettierrc.json",
"semi": true,
"trailingComma": "all",
"singleQuote": false,
"tabWidth": 4,
"printWidth": 120
}
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
### 👉🏽 [Live Demo](https://daily-poetry-image.vercel.app/)

## 项目介绍
- 本项目是基于 [Bing Image DALL-E-3](https://www.bing.com/images/create) 生成的图片,每天一句中国古诗词作为 Prompt。
- GitHub Action 自动触发。
- 诗词由[今日诗词](https://www.jinrishici.com/)提供 API。
- 网站使用 [Astro](https://astro.build) 构建。

- 本项目是基于 [Bing Image DALL-E-3](https://www.bing.com/images/create) 生成的图片,每天一句中国古诗词作为 Prompt。
- GitHub Action 自动触发。
- 诗词由[今日诗词](https://www.jinrishici.com/)提供 API。
- 网站使用 [Astro](https://astro.build) 构建。

## 订阅

Expand All @@ -26,8 +27,8 @@

## 灵感来源

- [yihong0618/2023](https://github.com/yihong0618/2023)
- [yihong0618/2023](https://github.com/yihong0618/2023)

## License

MIT
MIT
117 changes: 58 additions & 59 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,76 @@

import { getImageBySentence } from '../src/get-up';
import type { Response } from '../src/types';
import path from 'path';
import fs from 'fs';
import stream from 'stream';
import { promisify } from 'util';
import { getImageBySentence } from "../src/get-up";
import type { Response } from "../src/types";
import path from "path";
import fs from "fs";
import stream from "stream";
import { promisify } from "util";

const pipeline = promisify(stream.pipeline);

async function init() {
const cwd = process.cwd();
const cwd = process.cwd();

const argv = require("minimist")(process.argv.slice(2));

if (argv.cookie) {
try {
const res: Response = await getImageBySentence(argv.cookie);
console.log("Create Successful: ", res);

const argv = require('minimist')(process.argv.slice(2));

if (argv.cookie) {
try {
const res: Response = await getImageBySentence(argv.cookie);
console.log("Create Successful: ", res);

const outputPath = path.join(cwd, 'website/public');
const outputPath = path.join(cwd, "website/public");

const imagesPath = path.join(outputPath, 'images');
if (!fs.existsSync(imagesPath)) {
fs.mkdirSync(imagesPath);
}
const imagesPath = path.join(outputPath, "images");
if (!fs.existsSync(imagesPath)) {
fs.mkdirSync(imagesPath);
}

// 在 images 目录下,创建一个以时间戳命名的文件夹,将图片放入其中
const imagesFolderName = Date.now().toString();
const imagesFolderPath = path.join(imagesPath, imagesFolderName);
if (!fs.existsSync(imagesFolderPath)) {
fs.mkdirSync(imagesFolderPath);
}
// 在 images 目录下,创建一个以时间戳命名的文件夹,将图片放入其中
const imagesFolderName = Date.now().toString();
const imagesFolderPath = path.join(imagesPath, imagesFolderName);
if (!fs.existsSync(imagesFolderPath)) {
fs.mkdirSync(imagesFolderPath);
}

// 将图片放入 images 目录下的文件夹中
res.images.forEach((image, index) => {
// images 中是网络url,请求图片,将图片保存到 images 目录下的文件夹中
const imageFileName = `${index}.jpg`;
const imageFilePath = path.join(imagesFolderPath, imageFileName);
// 将图片放入 images 目录下的文件夹中
res.images.forEach((image, index) => {
// images 中是网络url,请求图片,将图片保存到 images 目录下的文件夹中
const imageFileName = `${index}.jpg`;
const imageFilePath = path.join(imagesFolderPath, imageFileName);

// 下载图片
fetch(image).then(res => {
if (!res.ok) throw new Error(`unexpected response ${res.statusText}`);
// @ts-ignore
pipeline(res.body, fs.createWriteStream(imageFilePath))
.catch(e => {
console.error('Something went wrong while saving the image', e);
// 下载图片
fetch(image).then((res) => {
if (!res.ok) throw new Error(`unexpected response ${res.statusText}`);
// @ts-ignore
pipeline(res.body, fs.createWriteStream(imageFilePath)).catch((e) => {
console.error("Something went wrong while saving the image", e);
});
});
});
});
});
const options = { timeZone: 'Asia/Shanghai', hour12: false };
const outputData = {
...res,
date: new Date().toLocaleString("zh-CN", options),
localImagesPath: imagesFolderName,
}
const options = { timeZone: "Asia/Shanghai", hour12: false };
const outputData = {
...res,
date: new Date().toLocaleString("zh-CN", options),
localImagesPath: imagesFolderName,
};

const contentPath = path.join(cwd, 'website/src/content/images');
const contentPath = path.join(cwd, "website/src/content/images");

const contentFile = path.join(contentPath, `${imagesFolderName}.json`);
const contentFile = path.join(contentPath, `${imagesFolderName}.json`);

fs.writeFileSync(contentFile, JSON.stringify(outputData));
fs.writeFileSync(contentFile, JSON.stringify(outputData));

setTimeout(() => { // 为了让图片下载完毕,再退出进程
process.exit(0);
}, 5000);
} catch (e) {
throw e
setTimeout(() => {
// 为了让图片下载完毕,再退出进程
process.exit(0);
}, 5000);
} catch (e) {
throw e;
}
} else {
throw new Error("Please provide a cookie using the --cookie argument");
}
} else {
throw new Error('Please provide a cookie using the --cookie argument');
}
}

init().catch((e) => {
console.error(e);
});
console.error(e);
});
Loading

0 comments on commit 0c33e9e

Please sign in to comment.