Skip to content

Commit

Permalink
Added image upload to eBay Kleinanzeigen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Honz committed Oct 28, 2021
1 parent 1dc8dfb commit c37a0a6
Show file tree
Hide file tree
Showing 4 changed files with 1,008 additions and 466 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint": "eslint --ext .js,.vue ./"
},
"dependencies": {
"@quasar/extras": "^1.9.15",
"@quasar/extras": "^1.11.3",
"@ungap/custom-elements-builtin": "^0.6.3",
"axios": "^0.18.1",
"core-js": "^3.6.5",
Expand All @@ -25,14 +25,16 @@
"electron-store": "^6.0.1",
"electron-unhandled": "^3.0.2",
"electron-util": "^0.14.2",
"form-data": "^4.0.0",
"lodash": "^4.17.20",
"quasar": "^1.15.2",
"quasar": "^1.16.0",
"vue-i18n": "^8.0.0",
"vue-toasted": "^1.1.28",
"x-frame-bypass": "^1.0.2"
"x-frame-bypass": "^1.0.2",
"xmlbuilder": "^15.1.1"
},
"devDependencies": {
"@quasar/app": "^2.1.14",
"@quasar/app": "^2.2.10",
"babel-eslint": "^10.0.1",
"devtron": "^1.4.0",
"electron": "^9.0.0",
Expand Down
56 changes: 55 additions & 1 deletion src-electron/main-process/kleinanzeigen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { RemoteSystemError, AuthorizationError, RemoteNotFound, AttributeError,
import settings from 'electron-settings';
import crypto from 'crypto';
import _ from 'lodash';
import FormData from 'form-data';
import { readFileAsync } from './utilities';

export class Kleinanzeigen {
APK_APP_VERSION = '13.4.2';
USER_AGENT = 'Dalvik/13.4.2';
USER_AGENT = 'Dalvik/2.2.0';
BASE_URL = 'https://api.ebay-kleinanzeigen.de/api';
EBAYK_APP = '13a6dde3-935d-4cd8-9992-db8a8c4b6c0f1456515662229';
BASIC_AUTH_USER = 'android';
Expand Down Expand Up @@ -194,6 +196,48 @@ export class Kleinanzeigen {
return response;
}

async _httpPostFile(urlSuffix, path) {
const data = new FormData();
let response = null;
let file = null;
let config = null;

try {
file = await readFileAsync(path);
data.append('file', file, 'image.jpg');
config = {
headers: data.getHeaders()
};
} catch (e) {
throw e;
}

try {
response = await this._axios.post(urlSuffix, data, config);
} catch (e) {
console.log(e);
throw AxiosError(e);
}

this._validateHttpResponse(response);

return response.data;
}

async _httpPostJsonFile(urlSuffix, path) {
let data = null;

try {
data = await this._httpPostFile(urlSuffix, path);
} catch (e) {
throw e;
}

const content = this._getJsonContent(data);

return content;
}

async _changeAdStatus(id, status) {
if (['active', 'paused'].indexOf(status) < 0) {
throw AttributeError(`Unknown requested status: ${status}`);
Expand Down Expand Up @@ -343,6 +387,16 @@ export class Kleinanzeigen {
throw e;
}
}

async uploadPicture(path) {
try {
const result = await this._httpPostJsonFile('/pictures.json', path);

return result
} catch (e) {
throw e;
}
}
}


Expand Down
24 changes: 18 additions & 6 deletions src-electron/main-process/utilities.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { RemoteSystemError, AuthorizationError, RemoteNotFound, AttributeError, AxiosError } from './exceptions';

import { readFile } from 'fs';

export const generateExceptionStr = (exc) => {
return `${exc.name}: ${exc.message}`
}

export const exceptionHandler = (exc, ipcEvent) => {
if (exc.constructor == AuthorizationError) {
if (exc.constructor === AuthorizationError) {
ipcEvent.reply('m-error-login', generateExceptionStr(exc));
} else if (exc.constructor == RemoteSystemError) {
} else if (exc.constructor === RemoteSystemError) {
ipcEvent.reply('m-error-general', generateExceptionStr(exc));
} else if (exc.constructor == RemoteNotFound) {
} else if (exc.constructor === RemoteNotFound) {
ipcEvent.reply('m-error-general', generateExceptionStr(exc));
} else if (exc.constructor == AttributeError) {
} else if (exc.constructor === AttributeError) {
ipcEvent.reply('m-error-general', generateExceptionStr(exc));
} else if (exc.constructor == AxiosError) {
} else if (exc.constructor === AxiosError) {
ipcEvent.reply('m-error-axios', exc);
} else {
ipcEvent.reply('m-error-general', exc.message);
}
}

export const readFileAsync = async function (path) {
return new Promise((resolve, reject) => {
readFile(path, (err, data) => {
if (err) {
return reject(err);
}

return resolve(data);
});
});
}
Loading

0 comments on commit c37a0a6

Please sign in to comment.