Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
empty-233 committed Dec 1, 2023
0 parents commit 6fff197
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/Update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main" ]
schedule:
- cron: '0 3 1 * *'

jobs:
build:

runs-on: ubuntu-latest

permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3

- name: install pnpm
run: npm install -g pnpm

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: pnpm install
run: pnpm install

- name: npm install ts-node
run: npm install -g ts-node

- name: Run main.ts with ts-node-esm
run: ts-node-esm main.ts

# - uses: stefanzweifel/git-auto-commit-action@v5
# with:
# commit_message: Auto Update Data
# create_branch: true
# branch: release
# add_options: '-f'
# file_pattern: 'data/'
# skip_dirty_check: true

- name: Git push assets to release branch
run: |
cd data
git init
git config --local user.name "My GitHub Actions Bot"
git config --local user.email "[email protected]"
git checkout -b release
git add .
git commit -m "Auto Update Data"
git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
git push -f -u origin release
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.pnpm-store

Editor directories and files
.vscode/*
.devcontainer
!.vscode/extensions.json
.idea

# data
original-data/*
data
49 changes: 49 additions & 0 deletions data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @ts-ignore
import { Item } from "./original-data/bangumi-data/data.js";

declare type Season = "SPRING" | "SUMMER" | "FALL" | "WINTER" | "UNDEFINED";
declare type Status = "UPCOMING" | "ONGOING" | "FINISHED";

declare interface Root {
repository: string;
lastUpdate: string;
data: Data[];
}

declare interface Data extends Item {
/**
* 季节和年
*/
animeSeason: AnimeSeason;
/**
* 状态
*/
status: Status;
/**
* 剧集
*/
episodes: number;
/**
* 图片
*/
picture: string;
/**
* 缩略图
*/
thumbnail: string;
/**
* 标题(同义词)
*/
synonyms: Array<string>;
}

declare interface AnimeSeason {
/**
* 季节
*/
season: Season;
/**
* 年份
*/
year: number;
}
145 changes: 145 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import fs from "fs";
import { join as pathJoin } from "path";
import { Root, Data } from "./data.d.js";
import { cloneRepository } from "./utils/git.js";
import { readFilesInFolder, splitUrl, formatDate } from "./utils/utils.js";
// @ts-ignore
import { Item as bangumiDataList } from "./original-data/bangumi-data/data.js";
import {
Root as animeOfflineDatabaseList,
Data as animeOfflineDatabaseData,
} from "./type/anime-offline-database.js";

export const gitClone = (
repositoryUrl: string,
destinationPath: string
): void => {
cloneRepository(repositoryUrl, destinationPath);
};

//拉取仓库
const bangumi_data_path: string = "./original-data/bangumi-data";
const anime_offline_database_path: string =
"./original-data/anime-offline-database";

gitClone("https://github.com/bangumi-data/bangumi-data.git", bangumi_data_path);
gitClone(
"https://github.com/manami-project/anime-offline-database.git",
anime_offline_database_path
);

//读取所有文件
const bangumi_data_paths = readFilesInFolder(
`${bangumi_data_path}/data/items`
).reverse();

const readFile = (path: string) => JSON.parse(fs.readFileSync(path, "utf-8"));

const anime_offline_database: animeOfflineDatabaseList = readFile(
`${anime_offline_database_path}/anime-offline-database.json`
);

//判断是否一致
const findIfExists = (
objects: Array<animeOfflineDatabaseData>,
value: string
) => {
return objects.filter((obj) =>
obj.synonyms.some((element) => element == value)
);
};

//写入文件
const writeJsonFile = (folderPath: string, filePath: string, data: any) => {
try {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
fs.writeFileSync(
pathJoin(folderPath, filePath),
JSON.stringify(data, null, 2)
);
} catch (err) {
console.error(err);
}
};

//匹配
const date = new Date();
let animeData: Root = {
repository: "https://github.com/empty-233/anime-data",
lastUpdate: formatDate(date),
data: [],
};
bangumi_data_paths.map((path) => {
const bangumi_data: Array<bangumiDataList> = readFile(path);
bangumi_data.map((bangumiData) => {
const aod = findIfExists(anime_offline_database.data, bangumiData.title);
if (aod.length !== 0) {
aod[0].relations.map((url) => {
const result = splitUrl(url);
bangumiData.sites.push({
site: result.domain,
id: result.path,
});
});
const data = {
...bangumiData,
animeSeason: aod[0].animeSeason,
status: aod[0].status,
episodes: aod[0].episodes,
picture: aod[0].picture,
thumbnail: aod[0].thumbnail,
synonyms: aod[0].synonyms,
};
//拆分月份,和bangumi一致
const pathSplit = path.split("/");
writeJsonFile(
`./data/years/${pathSplit.at(-2)}`,
`${pathSplit.pop()?.split(".")[0]}.json`,
data
);
animeData.data.push(data);
}
});
});

//拆分季节
interface ClassifiedAnime {
[key: string]: {
[key: string]: Data[];
};
}
const categorizeAnimatedYearsAndSeasons = (data: Data[]) => {
const classified: ClassifiedAnime = {};

data.forEach((anime) => {
const { season, year } = anime.animeSeason;
if (!classified[year]) {
classified[year] = {};
}
if (!classified[year][season]) {
classified[year][season] = [];
}
classified[year][season].push(anime);
});

return classified;
};
const classifiedAnime = categorizeAnimatedYearsAndSeasons(animeData.data);
for (const year in classifiedAnime) {
if (Object.prototype.hasOwnProperty.call(classifiedAnime, year)) {
const datas = classifiedAnime[year];
if (year != null && year != "null")
for (const season in datas) {
if (Object.prototype.hasOwnProperty.call(datas, season)) {
const data = datas[season];
if (season != null && season != "null")
writeJsonFile(`./data/season/${year}`, `${season}.json`, data);
}
}
}
}

//写入全部
writeJsonFile("./data", "data.json", animeData);
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"dependencies": {
"@types/node": "^20.10.1"
}
}
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6fff197

Please sign in to comment.