[gnome-music] scrobbler: Use libsoup for http requests
- From: Marinus Schraal <mschraal src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music] scrobbler: Use libsoup for http requests
- Date: Wed, 2 May 2018 11:33:58 +0000 (UTC)
commit a48fc6883e5435aaed86adb8d784cc5c1cd3a366
Author: Adrian Solom <adrian solom gmail com>
Date: Sat Mar 24 21:55:39 2018 +0000
scrobbler: Use libsoup for http requests
Replace the requests library with Soup provided by pygobject.
Make use of the async queue_message call and also replace the threading
calls.
closes: #144
.gitlab-ci.yml | 2 +-
gnomemusic/scrobbler.py | 45 ++++++++++++++++++++-------------------------
meson.build | 1 +
3 files changed, 22 insertions(+), 26 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f078b022..f57b7c18 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -6,7 +6,7 @@ stages:
variables:
DEPENDENCIES: gettext-devel git pygobject3 pygobject3-devel python3-gobject
python3-cairo-devel gtk3-devel gobject-introspection-devel
- tracker-devel libmediaart-devel grilo-devel
+ tracker-devel libmediaart-devel grilo-devel libsoup-devel
gnome-online-accounts-devel meson ninja-build
.build:
diff --git a/gnomemusic/scrobbler.py b/gnomemusic/scrobbler.py
index d5fb142e..f269a6ef 100644
--- a/gnomemusic/scrobbler.py
+++ b/gnomemusic/scrobbler.py
@@ -23,13 +23,12 @@
# delete this exception statement from your version.
from hashlib import md5
-from threading import Thread
import logging
-import requests
import gi
gi.require_version('Goa', '1.0')
-from gi.repository import GLib, Goa, GObject
+gi.require_version('Soup', '2.4')
+from gi.repository import GLib, Goa, GObject, Soup
from gnomemusic import log
import gnomemusic.utils as utils
@@ -142,6 +141,7 @@ class LastFmScrobbler(GObject.GObject):
self._scrobbled = False
self._authentication = None
self._goa_lastfm = GoaLastFM()
+ self._soup_session = Soup.Session.new()
@GObject.Property(type=bool, default=False)
def scrobbled(self):
@@ -179,7 +179,7 @@ class LastFmScrobbler(GObject.GObject):
if time_stamp is not None:
request_dict.update({
- "timestamp": time_stamp
+ "timestamp": str(time_stamp)
})
request_dict.update({
@@ -192,7 +192,7 @@ class LastFmScrobbler(GObject.GObject):
sig = ""
for key in sorted(request_dict):
- sig += key + str(request_dict[key])
+ sig += key + request_dict[key]
sig += secret
@@ -201,22 +201,25 @@ class LastFmScrobbler(GObject.GObject):
"api_sig": api_sig
})
- try:
- r = requests.post(
- "https://ws.audioscrobbler.com/2.0/", request_dict)
- if r.status_code != 200:
- logger.warning("Failed to {} track: {} {}".format(
- request_type_key, r.status_code, r.reason))
- logger.warning(r.text)
- except Exception as e:
- logger.warning(e)
+ msg = Soup.form_request_new_from_hash(
+ "POST", "https://ws.audioscrobbler.com/2.0/", request_dict)
+ self._soup_session.queue_message(
+ msg, self._lastfm_api_callback, request_type_key)
+
+ @log
+ def _lastfm_api_callback(self, session, msg, request_type_key):
+ """Internall callback method called by queue_message"""
+ status_code = msg.props.status_code
+ if status_code != 200:
+ logger.warning("Failed to {} track {} : {}".format(
+ request_type_key, status_code, msg.props.reason_phrase))
+ logger.warning(msg.props.response_body.data)
@log
def scrobble(self, media, time_stamp):
"""Scrobble a song to Last.fm.
If not connected to Last.fm nothing happens
- Creates a new thread to make the request
:param media: Grilo media item
:param time_stamp: song loaded time (epoch time)
@@ -226,17 +229,13 @@ class LastFmScrobbler(GObject.GObject):
if self._goa_lastfm.disabled:
return
- t = Thread(
- target=self._lastfm_api_call, args=(media, time_stamp, "scrobble"))
- t.setDaemon(True)
- t.start()
+ self._lastfm_api_call(media, time_stamp, "scrobble")
@log
def now_playing(self, media):
"""Set now playing song to Last.fm
If not connected to Last.fm nothing happens
- Creates a new thread to make the request
:param media: Grilo media item
"""
@@ -245,8 +244,4 @@ class LastFmScrobbler(GObject.GObject):
if self._goa_lastfm.disabled:
return
- t = Thread(
- target=self._lastfm_api_call,
- args=(media, None, "update now playing"))
- t.setDaemon(True)
- t.start()
+ self._lastfm_api_call(media, None, "update now playing")
diff --git a/meson.build b/meson.build
index bf3326b4..4421868f 100644
--- a/meson.build
+++ b/meson.build
@@ -25,6 +25,7 @@ PKGLIB_DIR = join_paths(get_option('prefix'), get_option('libdir'), PROJECT_RDNN
dependency('gobject-introspection-1.0', version: '>= 1.35.0')
dependency('gtk+-3.0', version: '>= 3.19.3')
dependency('libmediaart-2.0', version: '>= 1.9.1')
+dependency('libsoup-2.4')
dependency('tracker-sparql-2.0', version: '>= 1.99.1')
dependency('pygobject-3.0', version: '>= 3.29.1')
dependency('py3cairo', version: '>= 1.14.0')
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]