From 613b99cd573986b8f870ace82fbc344ac2b1445b Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 9 Mar 2020 11:53:17 +0800 Subject: [PATCH] Remove aria2p dependeny: use json library instead aria2p. --- README.md | 7 +++---- core/browser.py | 16 ++++++--------- core/pyaria2.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 core/pyaria2.py diff --git a/README.md b/README.md index 2d3a31b..a308e0f 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ EAF is an extensible framework, one can develop any Qt5 application and integrat 1. Make sure to have ```python3``` installed, and use ```pip3``` to install all EAF dependencies (see below list for details) ```Bash -sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser aria2p +sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser ``` If you use Arch Linux, we recommend you install dependencies with below command: @@ -55,7 +55,7 @@ sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcod ```Bash sudo pacman -S python-pyqt5 python-pyqt5-sip python-pyqtwebengine python-xlib python-qrcode python-feedparser python-dbus -yay -S python-pymupdf python-grip aria2p +yay -S python-pymupdf python-grip ``` Because Arch's QtWebEngine build with proprietary codec library that can play video file with browser. @@ -115,7 +115,6 @@ Packages listed as **Core** are mandatory for EAF to work, whereas other package | grip | pip3 | Markdown Previewer | Markdown rendering server | | qrcode | pip3 | File Sender, File Receiver, Airshare | Render QR code pointing to local files | | feedparser | pip3 | RSS Reader | Parse RSS feeds | -| aria2p | pip3 | Browser | Send download requests to Aria2 daemon | | aria2 | pacman (Arch) | Browser | Download files from the web | | wetty | yarn | Terminal | Communicate between browser and local TTY | | libreoffice | pacman | Doc Viewer | Convert doc file to pdf | @@ -195,7 +194,7 @@ If you got "undefined symbol" error after start EAF, and you use Arch Linux, yes You need use pip install all dependences after you upgrade your Arch system, then undefine symbol error will fix. ```Bash -sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser aria2p --force-reinstall +sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser --force-reinstall ``` ### What is Github Personal Access Tokens? diff --git a/core/browser.py b/core/browser.py index de61afd..8785e50 100644 --- a/core/browser.py +++ b/core/browser.py @@ -448,19 +448,15 @@ class BrowserBuffer(Buffer): self.message_to_emacs.emit("Save image: " + image_path) else: - from shutil import which + self.try_start_aria2_daemon() - if which("aria2p") is not None: + from core.pyaria2 import Jsonrpc - self.try_start_aria2_daemon() + download_url = download_item.url().toString() + jsonrpc = Jsonrpc('localhost', 6800) + resp = jsonrpc.addUris(download_url) - download_data = download_item.url().toString() - with open(os.devnull, "w") as null_file: - subprocess.Popen(["aria2p", "add", download_data], stdout=null_file) - - self.message_to_emacs.emit("Start download: " + download_data) - else: - self.message_to_emacs.emit("Please install aria2p first.") + self.message_to_emacs.emit("Start download: " + download_url) def destroy_buffer(self): # Record close page. diff --git a/core/pyaria2.py b/core/pyaria2.py new file mode 100644 index 0000000..8c35e03 --- /dev/null +++ b/core/pyaria2.py @@ -0,0 +1,53 @@ + +#!/usr/bin/env python +# coding=utf-8 + +import json +import requests + +class Jsonrpc(object): + + MUTI_METHOD = 'system.multicall' + ADDURI_METHOD = 'aria2.addUri' + + def __init__(self, host, port, token=None): + self._idCount = 0 + self.host = host + self.port = port + self.serverUrl = "http://{host}:{port}/jsonrpc".format(**locals()) + + def _genParams(self, method , uris=None, options=None, cid=None): + p = { + 'jsonrpc': '2.0', + 'id': self._idCount, + 'method': method, + 'test': 'test', + 'params': [] + } + if uris: + p['params'].append(uris) + if options: + p['params'].append(options) + return p + + def _post(self, action, params, onSuccess, onFail=None): + if onFail is None: + onFail = Jsonrpc._defaultErrorHandle + paramsObject = self._genParams(action, *params) + resp = requests.post(self.serverUrl, data=json.dumps(paramsObject)) + result = resp.json() + if "error" in result: + return onFail(result["error"]["code"], result["error"]["message"]) + else: + return onSuccess(resp) + + def addUris(self, uri, options=None): + def success(response): + return response.text + return self._post(Jsonrpc.ADDURI_METHOD, [[uri,], options], success) + + + @staticmethod + def _defaultErrorHandle(code, message): + print ("ERROR: {},{}".format(code, message)) + return None