Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

[mod] soundcloud: faster initialization #2671

Merged
merged 1 commit into from Mar 21, 2021
Merged

[mod] soundcloud: faster initialization #2671

merged 1 commit into from Mar 21, 2021

Conversation

dalf
Copy link
Contributor

@dalf dalf commented Mar 21, 2021

What does this PR do?

The get_cliend_id() function:

This commit fetches the javascript URLs in the reverse order: the client id is in the last javascript URL.

Why is this change important?

Faster searx initialization (and less HTTP requests).

How to test this PR locally?

  • !soundcloud time

Author's checklist

Related issues

The get_cliend_id() function:
* fetches https://soundcloud.com
* then fetches each referenced javascript URL to get the client id.

This commit fetches the javascript URLs in the reverse order: the client id is in the last javascript URL.
Copy link
Contributor

@return42 return42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Faster searx initialization.

Does lazy evaluation makes sense here?

diff --git a/searx/engines/soundcloud.py b/searx/engines/soundcloud.py
index b3e3383b..44fd2d85 100644
--- a/searx/engines/soundcloud.py
+++ b/searx/engines/soundcloud.py
@@ -41,10 +41,14 @@ embedded_url = '<iframe width="100%" height="166" ' +\
     'data-src="https://w.soundcloud.com/player/?url={uri}"></iframe>'
 
 cid_re = re.compile(r'client_id:"([^"]*)"', re.I | re.U)
-guest_client_id = ''
+guest_client_id = None
 
 
 def get_client_id():
+    global guest_client_id
+    if guest_client_id is not None:
+        return guest_client_id
+
     response = http_get("https://soundcloud.com")
 
     if response.ok:
@@ -66,19 +70,13 @@ def get_client_id():
     return ""
 
 
-def init(engine_settings=None):
-    global guest_client_id
-    # api-key
-    guest_client_id = get_client_id()
-
-
 # do search-request
 def request(query, params):
     offset = (params['pageno'] - 1) * 20
 
     params['url'] = search_url.format(query=urlencode({'q': query}),
                                       offset=offset,
-                                      client_id=guest_client_id)
+                                      client_id=get_client_id())
 
     return params

@dalf
Copy link
Contributor Author

dalf commented Mar 21, 2021

Does lazy evaluation makes sense here?

In this case, the first request will most probably time out (the time to get the client id and then user request).

@dalf dalf merged commit a48ec0b into master Mar 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants