Skip to content

Commit

Permalink
add reading meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaohongxuan committed Dec 30, 2023
1 parent 0389f70 commit ee67b73
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

20 changes: 19 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { settingsStore } from './settings';
import { get } from 'svelte/store';
import { getCookieString } from './utils/cookiesUtil';
import { Cookie, parse, splitCookiesString } from 'set-cookie-parser';
import { HighlightResponse, BookReviewResponse, ChapterResponse } from './models';
import {
HighlightResponse,
BookReviewResponse,
ChapterResponse,
BookReadInfoResponse
} from './models';
export default class ApiManager {
readonly baseUrl: string = 'https://i.weread.qq.com';

Expand Down Expand Up @@ -209,6 +214,19 @@ export default class ApiManager {
console.error('get book chapters error' + bookId, e);
}
}
async getBookReadInfo(bookId: string): Promise<BookReadInfoResponse> {
try {
const url = `${this.baseUrl}/book/readinfo?bookId=${bookId}&readingDetail=1&readingBookIndex=1&finishedDate=1`;
const req: RequestUrlParam = { url: url, method: 'GET', headers: this.getHeaders() };
const resp = await requestUrl(req);
return resp.json;
} catch (e) {
new Notice(
'Failed to fetch weread notebook read info . Please check your Cookies and try again.'
);
console.error('get book read info error' + bookId, e);
}
}

private updateCookies(respCookie: string) {
let refreshCookies: Cookie[];
Expand Down
87 changes: 87 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,80 @@ export type ChapterResponse = {
}[];
};

export type BookReadInfoResponse = {
finishedBookCount: number;
finishedBookIndex: number;
finishedDate: number;
readingBookCount: number;
readingBookDate: number;
readingProgress: number;
readingReviewId: string;
canCancelReadstatus: number;
markedStatus: number;
readingTime: number;
totalReadDay: number;
recordReadingTime: number;
continueReadDays: number;
continueBeginDate: number;
continueEndDate: number;
showSummary: number;
showDetail: number;
readDetail: {
totalReadingTime: number;
totalReadDay: number;
continueReadDays: number;
continueBeginDate: number;
continueEndDate: number;
beginReadingDate: number;
lastReadingDate: number;
longestReadingDate: number;
avgReadingTime: number;
longestReadingTime: number;
data: {
readDate: number;
readTime: number;
}[];
};
bookInfo: {
bookId: string;
title: string;
author: string;
translator: string;
intro: string;
cover: string;
version: number;
format: string;
type: number;
soldout: number;
bookStatus: number;
payType: number;
finished: number;
maxFreeChapter: number;
free: number;
mcardDiscount: number;
ispub: number;
extra_type: number;
cpid: number;
publishTime: string;
lastChapterIdx: number;
paperBook: {
skuId: string;
};
centPrice: number;
readingCount: number;
maxfreeInfo: {
maxfreeChapterIdx: number;
maxfreeChapterUid: number;
maxfreeChapterRatio: number;
};
blockSaveImg: number;
language: string;
hideUpdateTime: boolean;
isEPUBComics: number;
webBookControl: number;
};
};

export type Chapter = {
chapterUid: number;
chapterIdx: number;
Expand Down Expand Up @@ -154,6 +228,19 @@ export type Metadata = {
file?: AnnotationFile;
totalWords?: number;
rating?: string;
readInfo?: {
markedStatus: number;
readingTime: number;
totalReadDay: number;
continueReadDays: number;
readingBookCount: number;
readingBookIndex: number;
readingBookDate: number;
finishedBookCount: number;
finishedBookIndex: number;
finishedDate: number;
readingProgress: number;
};
};

export type Highlight = {
Expand Down
18 changes: 12 additions & 6 deletions src/syncNotebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ export default class SyncNotebooks {
private async convertToNotebook(metaData: Metadata): Promise<Notebook> {
const bookDetail = await this.apiManager.getBook(metaData.bookId);
if (bookDetail) {
metaData['category'] = bookDetail['category'];
metaData['publisher'] = bookDetail['publisher'];
metaData['isbn'] = bookDetail['isbn'];
metaData['intro'] = bookDetail['intro'];
metaData['totalWords'] = bookDetail['totalWords'];
metaData.category = bookDetail['category'];
metaData.publisher = bookDetail['publisher'];
metaData.isbn = bookDetail['isbn'];
metaData.intro = bookDetail['intro'];
metaData.totalWords = bookDetail['totalWords'];
const newRating = parseInt(bookDetail['newRating']);
metaData['rating'] = `${newRating / 10}%`;
metaData.rating = `${newRating / 10}%`;
}

const readInfo = await this.apiManager.getBookReadInfo(metaData.bookId);
if (readInfo) {
metaData.readInfo = Object.assign({}, readInfo);
}
console.log('meta read info', readInfo);

const highlightResp = await this.apiManager.getNotebookHighlights(metaData.bookId);
const reviewResp = await this.apiManager.getNotebookReviews(metaData.bookId);
const chapterResp = await this.apiManager.getChapters(metaData.bookId);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const formatTimeDuration = (duration: number): string => {
const hours = Math.floor(duration / 60);
const minutes = duration % 60;

const formattedDuration = `${hours}小时${minutes}分钟`;

return formattedDuration;
};

export const formatTimestampToDate = (readingBookDate: number): string => {
return window.moment(readingBookDate * 1000).format('YYYY-MM-DD');
};
26 changes: 26 additions & 0 deletions src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Notice, parseYaml, stringifyYaml, TFile } from 'obsidian';
import type { Notebook } from '../models';
import { formatTimeDuration, formatTimestampToDate } from './dateUtil';

type FrontMatterContent = {
doc_type: string;
Expand All @@ -8,10 +9,23 @@ type FrontMatterContent = {
cover: string;
noteCount: number;
reviewCount: number;
readingStatus?: string;
progress?: string;
readingTime?: string;
totalReadDay?: number;
readingDate?: string;
finishedDate?: string;
};

export const frontMatterDocType = 'weread-highlights-reviews';

enum ReadingStatus {
'未标记' = 1,
'在读' = 2,
'读过' = 3,
'读完' = 4
}

export const buildFrontMatter = (
markdownContent: string,
noteBook: Notebook,
Expand All @@ -25,6 +39,18 @@ export const buildFrontMatter = (
reviewCount: noteBook.metaData.reviewCount,
noteCount: noteBook.metaData.noteCount
};

const readInfo = noteBook.metaData.readInfo;
if (readInfo) {
frontMatter.readingStatus = ReadingStatus[readInfo.markedStatus];
frontMatter.progress = readInfo.readingProgress + '%';
frontMatter.totalReadDay = readInfo.totalReadDay;
frontMatter.readingTime = formatTimeDuration(readInfo.readingTime);
frontMatter.readingDate = formatTimestampToDate(readInfo.readingBookDate);
if (readInfo.finishedDate) {
frontMatter.finishedDate = formatTimestampToDate(readInfo.finishedDate);
}
}
let existFrontMatter = Object();
if (existFile) {
const cache = app.metadataCache.getFileCache(existFile);
Expand Down

0 comments on commit ee67b73

Please sign in to comment.