Skip to content

Commit

Permalink
Merge pull request #50 from mvdicarlo/develop
Browse files Browse the repository at this point in the history
v3.0.35
  • Loading branch information
mvdicarlo authored Jun 18, 2021
2 parents fd74734 + 79c9dc0 commit e2562cd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
2 changes: 1 addition & 1 deletion electron-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postybirb-plus",
"version": "3.0.34",
"version": "3.0.35",
"description": "(ClientServer) PostyBirb is an application that helps artists post art and other multimedia to multiple websites more quickly.",
"main": "dist/main.js",
"author": "Michael DiCarlo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import { LoginResponse } from '../interfaces/login-response.interface';
import { ScalingOptions } from '../interfaces/scaling-options.interface';
import { Website } from '../website.base';

interface DeviantArtFolder {
description: string;
folderid: string;
has_subfolders: boolean;
name: string;
parent?: string;
subfolders: DeviantArtFolder[];
}

@Injectable()
export class DeviantArt extends Website {
readonly BASE_URL = 'https://www.deviantart.com';
Expand Down Expand Up @@ -104,9 +113,24 @@ export class DeviantArt extends Website {
return renewed;
}

private flattenFolders(folder: DeviantArtFolder): DeviantArtFolder[] {
const folders = [folder];

if (!folder) {
return [];
}

if (!folder.has_subfolders) {
return folders;
}

folder.subfolders.forEach((sf) => folders.push(...this.flattenFolders(sf)));
return folders;
}

private async getFolders(profileId: string, token: string) {
const res = await Http.get<{
results: Array<{ folderid: string; name: string; parent: string }>;
results: Array<DeviantArtFolder>;
}>(
`${this.BASE_URL}/api/v1/oauth2/gallery/folders?calculate_size=false&limit=50&access_token=${token}`,
undefined,
Expand All @@ -115,9 +139,14 @@ export class DeviantArt extends Website {

const folders: Folder[] = [];
const results = res.body.results || [];
results.forEach((folder) => {
const flattenedFolders: DeviantArtFolder[] = [];
results.forEach((r: DeviantArtFolder) =>
flattenedFolders.push(...this.flattenFolders(r)),
);

flattenedFolders.forEach((folder) => {
const parent = folder.parent
? results.find((f) => f.folderid === folder.parent && f.name !== 'Featured')
? flattenedFolders.find((f) => f.folderid === folder.parent && f.name !== 'Featured')
: undefined;
folders.push({
value: folder.folderid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class FurryNetwork extends Website {
.formatTags(tags, { spaceReplacer: '-', maxLength: 30, minLength: 3 })
.map(tag =>
tag
.replace(/(\(|\)|:|#|;|\]|\[|')/g, '')
.replace(/(\(|\)|:|#|;|\]|\[|\.|')/g, '')
.replace(/(\\|\/)/g, '-')
.replace(/\?/g, 'unknown'),
)
Expand Down
89 changes: 51 additions & 38 deletions electron-app/src/server/websites/telegram/telegram.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ import FormContent from 'src/server/utils/form-content.util';
export class Telegram extends Website {
readonly BASE_URL: string;
readonly defaultDescriptionParser = PlaintextParser.parse;
readonly acceptsFiles: string[] = ['jpg', 'gif', 'png']; // TODO expand functionality here
readonly acceptsFiles: string[] = [];
private readonly instances: Record<string, MTProto> = {};
private authData: Record<string, { phone_code_hash: string; phone_number: string }> = {};
public acceptsAdditionalFiles = true;
public waitBetweenPostsInterval = 30_000;
private lastCall: number;
private DEFAULT_WAIT: number = 3000;
private usernameMap: Record<string, string> = {};

private async callApi<T>(appId: string, protocol: string, data: any): Promise<T> {
if (this.lastCall) {
const now = Date.now();
if (now - this.lastCall <= 2000) {
await WaitUtil.wait(Math.max(2000 - (now - this.lastCall), 1000));
if (now - this.lastCall <= this.DEFAULT_WAIT) {
await WaitUtil.wait(Math.max(this.DEFAULT_WAIT - (now - this.lastCall), 1000));
}
}

Expand All @@ -59,7 +61,8 @@ export class Telegram extends Website {
this.lastCall = Date.now(); // Cautious set in case anything unexpected happens
res = await this.instances[appId].call(protocol, data);
} catch (err) {
if (err.error_message.startsWith('FLOOD_WAIT')) {
this.logger.error(err);
if (err?.error_message?.startsWith('FLOOD_WAIT')) {
const wait = Number(err.error_message.split('_').pop());
if (wait > 60) {
// Too long of a wait
Expand All @@ -84,19 +87,19 @@ export class Telegram extends Website {
return resErr ? Promise.reject(resErr) : (res as T);
}

public authenticate(data: { appId: string; code: string }) {
return this.callApi(data.appId, 'auth.signIn', {
phone_number: this.authData[data.appId].phone_number,
phone_code: data.code,
phone_code_hash: this.authData[data.appId].phone_code_hash,
})
.then(() => {
result: true;
})
.catch((err) => {
this.logger.error(err);
return { result: false, message: err.error_message };
public async authenticate(data: { appId: string; code: string }) {
try {
const signIn: any = await this.callApi(data.appId, 'auth.signIn', {
phone_number: this.authData[data.appId].phone_number,
phone_code: data.code,
phone_code_hash: this.authData[data.appId].phone_code_hash,
});
this.usernameMap[data.appId] = signIn?.user?.username;
return { result: true };
} catch (err) {
this.logger.error(err);
return { result: false, message: err.error_message };
}
}

public async startAuthentication(data: TelegramAccountData) {
Expand Down Expand Up @@ -154,7 +157,13 @@ export class Telegram extends Website {

await this.loadChannels(data._id, appId);
status.loggedIn = true;
status.username = '<Your Telegram Account>';
status.username = data.data.username || this.usernameMap[appId] || '<Your Telegram Account>';
// Roundabout way to set username and save it after authentication
if (this.usernameMap[appId]) {
status.data = {
username: this.usernameMap[appId],
};
}
return status;
}

Expand Down Expand Up @@ -206,6 +215,28 @@ export class Telegram extends Website {
private async upload(appId: string, file: PostFileRecord) {
const parts = _.chunk(file.file.value, 512000); // 512KB
const file_id = Date.now();
const props: {
_: 'inputMediaUploadedDocument' | 'inputMediaUploadedPhoto';
mime_type?: string;
nosound_video?: boolean;
attributes?: any[];
} = {
_: 'inputMediaUploadedDocument',
};
if (
file.file.options.contentType === 'image/png' ||
file.file.options.contentType === 'image/jpg' ||
file.file.options.contentType === 'image/jpeg'
) {
props._ = 'inputMediaUploadedPhoto';
} else {
props.attributes = [];
props.mime_type = file.file.options.contentType;
if (props.mime_type === 'image/gif') {
props.nosound_video = true;
}
}

if (file.file.value.length >= FileSize.MBtoBytes(10)) {
// Big file path
for (let i = 0; i < parts.length; i++) {
Expand All @@ -220,7 +251,7 @@ export class Telegram extends Website {
}

return {
_: 'inputMediaUploadedPhoto',
...props,
file: {
_: 'inputFileBig',
id: file_id,
Expand All @@ -240,7 +271,7 @@ export class Telegram extends Website {
}

return {
_: 'inputMediaUploadedPhoto',
...props,
file: {
_: 'inputFile',
id: file_id,
Expand Down Expand Up @@ -378,31 +409,13 @@ export class Telegram extends Website {
);
submissionPart.data.channels.forEach((f) => {
if (!WebsiteValidator.folderIdExists(f, folders)) {
problems.push(`Folder (${f}) not found.`);
problems.push(`Channel (${f}) not found.`);
}
});
} else {
problems.push('No channel(s) selected.');
}

const files = [
submission.primary,
...(submission.additional || []).filter(
(f) => !f.ignoredAccounts!.includes(submissionPart.accountId),
),
];

let hasAddedProblem = false;
files.forEach((file) => {
if (hasAddedProblem) {
return;
}
if (!WebsiteValidator.supportsFileType(file, this.acceptsFiles)) {
problems.push(`Currently supported file formats: ${this.acceptsFiles.join(', ')}`);
hasAddedProblem = true;
}
});

const description = this.defaultDescriptionParser(
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postybirb-plus",
"version": "3.0.34",
"version": "3.0.35",
"description": "PostyBirb is an application that helps artists post art and other multimedia to multiple websites more quickly..",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postybirb-plus-ui",
"version": "3.0.34",
"version": "3.0.35",
"license": "BSD-3-Clause",
"private": true,
"Author": "Michael DiCarlo",
Expand Down

0 comments on commit e2562cd

Please sign in to comment.