Skip to content

Commit

Permalink
A-T name format. fix scraper bugs. fix move bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyChen777 committed Jun 24, 2022
1 parent 900d67e commit 17f7c41
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/preload/interactors/entity-interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class EntityInteractor {
const updatePromise = async (entityDrafts: PaperEntityDraft[]) => {
const movedEntityDrafts = await Promise.all(
entityDrafts.map((entityDraft: PaperEntityDraft) =>
this.fileRepository.move(entityDraft)
this.fileRepository.move(entityDraft, true)
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export class BibExporter extends Exporter {

for (const entity of entityDrafts) {
let citeKey = "";
const nameArray = entity.authors.split(", ")[0].split(" ");
let nameArray;
if (entity.authors.includes(";")) {
nameArray = entity.authors.split(";")[0].split(" ");
} else {
nameArray = entity.authors.split(", ")[0].split(" ");
}
const lastName = nameArray[nameArray.length - 1];
citeKey += lastName.toLowerCase();
citeKey += entity.pubTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export interface FileBackend {

check(): void;
access(url: string, download: boolean): Promise<string>;
move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null>;
move(
entity: PaperEntityDraft,
fourceDelete: boolean
): Promise<PaperEntityDraft | null>;
remove(entity: PaperEntityDraft): Promise<boolean>;
removeFile(url: string): Promise<boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export class LocalFileBackend implements FileBackend {
}
}

async _move(sourceURL: string, targetURL: string): Promise<boolean> {
async _move(
sourceURL: string,
targetURL: string,
forceDelete: boolean = false
): Promise<boolean> {
const _sourceURL = sourceURL.replace("file://", "");
const _targetURL = targetURL.replace("file://", "");
const stat = await fsPromise.lstat(_sourceURL);
Expand All @@ -54,8 +58,9 @@ export class LocalFileBackend implements FileBackend {
try {
await fsPromise.copyFile(_sourceURL, _targetURL);
if (
(this.preference.get("deleteSourceFile") as boolean) &&
_sourceURL !== _targetURL
((this.preference.get("deleteSourceFile") as boolean) &&
_sourceURL !== _targetURL) ||
forceDelete
) {
await fsPromise.unlink(sourceURL);
}
Expand All @@ -69,8 +74,12 @@ export class LocalFileBackend implements FileBackend {
}
}

async move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null> {
async move(
entity: PaperEntityDraft,
forceDelete: boolean = false
): Promise<PaperEntityDraft | null> {
let title = entity.title.replace(/[^a-zA-Z0-9 ]/g, "").replace(/\s/g, "_");
let id = entity._id.toString();
if (this.preference.get("renamingFormat") === "short") {
title = title
.split("_")
Expand All @@ -83,9 +92,16 @@ export class LocalFileBackend implements FileBackend {
})
.filter((c: string) => c && c === c.toUpperCase())
.join("");
} else if (this.preference.get("renamingFormat") === "authortitle") {
let author = entity.authors.split(",")[0];
if (author !== entity.authors) {
author = `${author} et al`;
}
title = `${author} - ${title.slice(0, 20)}`;
id = id.slice(-5, -1);
}

const targetFileName = title + "_" + entity._id.toString();
const targetFileName = title + "_" + id;

// 1. Move main file.
const sourceMainURL = constructFileURL(
Expand All @@ -100,7 +116,11 @@ export class LocalFileBackend implements FileBackend {
false,
this.preference.get("appLibFolder") as string
);
const mainSuccess = await this._move(sourceMainURL, targetMainURL);
const mainSuccess = await this._move(
sourceMainURL,
targetMainURL,
forceDelete
);
if (mainSuccess) {
entity.mainURL = path.basename(targetMainURL);
} else {
Expand All @@ -122,7 +142,11 @@ export class LocalFileBackend implements FileBackend {
sourceSupURL: string,
targetSupURL: string
) => {
const supSuccess = await this._move(sourceSupURL, targetSupURL);
const supSuccess = await this._move(
sourceSupURL,
targetSupURL,
forceDelete
);
if (supSuccess) {
return path.basename(targetSupURL);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,26 @@ export class WebDavFileBackend implements FileBackend {
async _move(
sourceURL: string,
targetURL: string,
targetCacheURL: string
targetCacheURL: string,
forceDelete: boolean = false
): Promise<boolean> {
try {
let success;
if (sourceURL.startsWith("file://")) {
success = await this._local2localMove(sourceURL, targetCacheURL);
success = await this._local2serverMove(sourceURL, targetURL);
if (this.preference.get("deleteSourceFile") as boolean) {
if (
(this.preference.get("deleteSourceFile") as boolean) ||
forceDelete
) {
await fsPromise.unlink(sourceURL);
}
} else if (sourceURL.startsWith("webdav://")) {
success = await this._server2serverMove(sourceURL, targetURL);
if (this.preference.get("deleteSourceFile") as boolean) {
if (
(this.preference.get("deleteSourceFile") as boolean) ||
forceDelete
) {
await this.webdavClient?.deleteFile(
sourceURL.replace("webdav://", "/paperlib/")
);
Expand All @@ -219,9 +226,13 @@ export class WebDavFileBackend implements FileBackend {
}
}

async move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null> {
async move(
entity: PaperEntityDraft,
forceDelete: boolean = false
): Promise<PaperEntityDraft | null> {
await this.check();
let title = entity.title.replace(/[^a-zA-Z0-9 ]/g, "").replace(/\s/g, "_");
let id = entity._id.toString();
if (this.preference.get("renamingFormat") === "short") {
title = title
.split("_")
Expand All @@ -234,9 +245,16 @@ export class WebDavFileBackend implements FileBackend {
})
.filter((c: string) => c && c === c.toUpperCase())
.join("");
} else if (this.preference.get("renamingFormat") === "authortitle") {
let author = entity.authors.split(",")[0];
if (author !== entity.authors) {
author = `${author} et al`;
}
title = `${author} - ${title.slice(0, 20)}`;
id = id.slice(-5, -1);
}

const targetFileName = title + "_" + entity._id.toString();
const targetFileName = title + "_" + id;

// 1. Move main file.
let sourceMainURL;
Expand Down Expand Up @@ -268,7 +286,8 @@ export class WebDavFileBackend implements FileBackend {
const mainSuccess = await this._move(
sourceMainURL,
targetMainURL,
targetMainCacheURL
targetMainCacheURL,
forceDelete
);
if (mainSuccess) {
entity.mainURL = path.basename(targetMainURL);
Expand Down Expand Up @@ -296,7 +315,8 @@ export class WebDavFileBackend implements FileBackend {
const supSuccess = await this._move(
sourceSupURL,
targetSupURL,
targetSupCacheURL
targetSupCacheURL,
forceDelete
);
if (supSuccess) {
return path.basename(targetSupURL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export class FileRepository {
async access(url: string, download: boolean): Promise<string> {
return await this.backend.access(url, download);
}
async move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null> {
return await this.backend.move(entity);
async move(
entity: PaperEntityDraft,
fourceDelete = false
): Promise<PaperEntityDraft | null> {
return await this.backend.move(entity, fourceDelete);
}
async remove(entity: PaperEntityDraft): Promise<boolean> {
return await this.backend.remove(entity);
Expand Down
12 changes: 12 additions & 0 deletions packages/preload/repositories/scraper-repository/scrapers/cvf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class CVFScraper extends Scraper {
type: string;
ENTRYTYPE: string;
pages: string;
author: string;
};
if (typeof response.year !== "undefined") {
const pubTime = response.year;
Expand All @@ -62,6 +63,17 @@ export class CVFScraper extends Scraper {
entityDraft.setValue("pubType", pubType);
entityDraft.setValue("publication", publication);
entityDraft.setValue("pages", response.pages || "");

if (response.author) {
const authorList = response.author.split("and").map((author) => {
const first_last = author
.trim()
.split(",")
.map((v) => v.trim());
return `${first_last[1]} ${first_last[0]}`;
});
entityDraft.setValue("authors", authorList.join(", "));
}
}
return entityDraft;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/preload/repositories/scraper-repository/scrapers/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ export class PDFScraper extends Scraper {
const firstPageText = rawResponse.firstPageText;

entityDraft.setValue("title", metaData.info.Title);
entityDraft.setValue("authors", metaData.info.Author);
let authors;
if (metaData.info.Author.includes(";")) {
authors = metaData.info.Author.split(";")
.map((author) => {
return author.trim();
})
.join(", ");
} else {
authors = metaData.info.Author;
}
entityDraft.setValue("authors", authors);

// Extract arXiv ID
const arxivIds = firstPageText.match(
Expand Down
2 changes: 1 addition & 1 deletion packages/preload/utils/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface PreferenceStore {
invertColor: boolean;
sidebarSortBy: "name" | "count" | "color";
sidebarSortOrder: "asce" | "desc";
renamingFormat: "full" | "short";
renamingFormat: "full" | "short" | "authortitle";

enableExportReplacement: boolean;
exportReplacement: Array<{ from: string; to: string }>;
Expand Down
4 changes: 2 additions & 2 deletions packages/renderer/src/ui/preference-view/general-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const onThemeUpdate = (value: string) => {
<Options
class="mb-5"
title="Renaming Format"
info="Full: FullTitle_id.pdf; Short: FirstCharTitle_id.pdf"
info="Full: FullTitle_id.pdf; Short: FirstCharTitle_id.pdf; A-T: Author-Title_id.pdf"
:selected="preference.renamingFormat"
:options="{ short: 'Short', full: 'Full' }"
:options="{ short: 'Short', full: 'Full', authortitle: 'A-T' }"
@update="
(value) => {
onUpdate('renamingFormat', value);
Expand Down

0 comments on commit 17f7c41

Please sign in to comment.