Skip to content

Commit

Permalink
Merge pull request #36 from jaylinski/bugfix/client-id-extraction
Browse files Browse the repository at this point in the history
Adapt client-ID extraction to new asset structure
  • Loading branch information
jaylinski authored Dec 1, 2019
2 parents 780711c + dfbbba9 commit 1a6bc9c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
23 changes: 12 additions & 11 deletions resources/lib/soundcloud/api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,21 @@ def fetch_client_id():
html = requests.get("https://soundcloud.com/", headers=headers).text

# Extract the HREF to the JS file (which contains the API key)
match = re.search(r"=\"(https://a-v2\.sndcdn\.com/assets/app.*)\"", html)
matches = re.findall(r"=\"(https://a-v2\.sndcdn\.com/assets/.*.js)\"", html)

if match:
# Get the JS
response = requests.get(match.group(1), headers=headers)
response.encoding = "utf-8" # This speeds up `response.text` by 3 seconds
if matches:
for match in matches:
# Get the JS
response = requests.get(match, headers=headers)
response.encoding = "utf-8" # This speeds up `response.text` by 3 seconds

# Extract the API key
key = re.search(r"exports={\"api-v2\".*client_id:\"(\w*)\"", response.text)
# Extract the API key
key = re.search(r"exports={\"api-v2\".*client_id:\"(\w*)\"", response.text)

if key:
return key.group(1)
else:
raise Exception("Failed to extract client key from js")
if key:
return key.group(1)

raise Exception("Failed to extract client key from js")
else:
raise Exception("Failed to extract js href from html")

Expand Down
1 change: 1 addition & 0 deletions tests/mocks/html/assets.0-744ba03a-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Asset with no client ID
1 change: 1 addition & 0 deletions tests/mocks/html/assets.49-4786eb1d-3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/mocks/html/soundcloud.com.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SoundCloud – Listen to free music and podcasts on SoundCloud</title>
</head>
<body>
<script crossorigin src="https://a-v2.sndcdn.com/assets/0-744ba03a-3.js"></script>
<script crossorigin src="https://a-v2.sndcdn.com/assets/49-4786eb1d-3.js"></script>
</body>
</html>
32 changes: 31 additions & 1 deletion tests/resources/lib/soundcloud/test_api_v2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import sys
from unittest import TestCase
from unittest import mock, TestCase
from unittest.mock import MagicMock, Mock, DEFAULT, ANY
sys.modules['xbmc'] = MagicMock()
sys.modules['xbmcaddon'] = MagicMock()
Expand Down Expand Up @@ -30,6 +30,29 @@ def _side_effect_settings_get(*args):
else:
return DEFAULT

@staticmethod
def _side_effect_request_get(*args, **keywargs):
if args[0] == "https://soundcloud.com/":
with open("./tests/mocks/html/soundcloud.com.html") as f:
mock_data = f.read()
obj = mock.Mock()
obj.text = mock_data
return obj
elif args[0] == "https://a-v2.sndcdn.com/assets/0-744ba03a-3.js":
with open("./tests/mocks/html/assets.0-744ba03a-3.js") as f:
mock_data = f.read()
obj = mock.Mock()
obj.text = mock_data
return obj
elif args[0] == "https://a-v2.sndcdn.com/assets/49-4786eb1d-3.js":
with open("./tests/mocks/html/assets.49-4786eb1d-3.js") as f:
mock_data = f.read()
obj = mock.Mock()
obj.text = mock_data
return obj
else:
return DEFAULT

def test_search(self):
with open("./tests/mocks/api_v2_search_tracks.json") as f:
mock_data = f.read()
Expand Down Expand Up @@ -173,3 +196,10 @@ def test_call(self):

self.assertEqual(res.items[0].label, "Noisia")
self.assertEqual(res.items[1].label, "NOISIA")

@mock.patch('requests.get')
def test_fetch_client_id(self, mock_method):
mock_method.side_effect = self._side_effect_request_get

client_id = self.api.fetch_client_id()
self.assertEqual(client_id, "1XduoqV99lROqCMpijtDo5WnJmpaLuYm")

0 comments on commit 1a6bc9c

Please sign in to comment.