Repository: takeontom/PyPeri

Issue:
Retrieve user id from https://www.periscope.tv/u/<user id> URLs

Discussion:

Patch:
diff --git a/docs/usage.rst b/docs/usage.rst
index 467c0c1..b387fa3 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -114,6 +114,7 @@ of these from the supplied URL.
 Supports the following URL formats:
 
 * https://www.periscope.tv/w/<broadcast_id>
+* https://www.periscope.tv/u/<user_id>
 * https://www.periscope.tv/<username>
 * https://www.periscope.tv/<username>/<broadcast_id>
 
diff --git a/pyperi/pyperi.py b/pyperi/pyperi.py
index 3caca5d..e2345c1 100644
--- a/pyperi/pyperi.py
+++ b/pyperi/pyperi.py
@@ -65,6 +65,7 @@ class PyPeri(object):
 
         url_parsers = [
             self.parse_periscope_w_url,
+            self.parse_periscope_u_url,
             self.parse_periscope_username_broadcast_id_url,
             self.parse_periscope_username_url,
         ]
@@ -90,9 +91,6 @@ class PyPeri(object):
 
             * https://www.periscope.tv/w/<broadcast_id>
 
-        If a `broadcast_id` is not found in the supplied URL, or the URL
-        doesn't particularly look like a "/w/" URL, then will return `None`.
-
         Returns a dict of `username`, `broadcast_id` and `user_id` values.
         """
         out = {
@@ -158,3 +156,29 @@ class PyPeri(object):
             out['username'] = username
             out['broadcast_id'] = broadcast_id
         return out
+
+    def parse_periscope_u_url(self, url):
+        """
+        Retrieve the `user_id` from the supplied "/u/" URL.
+
+        "/u/" URLs look like this:
+
+            * https://www.periscope.tv/u/<user_id>
+
+        Returns a dict of `username`, `broadcast_id` and `user_id` values.
+        """
+        out = {
+            'broadcast_id': None,
+            'user_id': None,
+            'username': None,
+        }
+
+        u_url_pattern = '{base_url}/u/([0-9]+)'.format(
+            base_url=PyPeri.PERISCOPE_WEB_BASE_URL
+        )
+
+        u_result = match(u_url_pattern, url)
+        if u_result:
+            broadcast_id = u_result.group(1)
+            out['user_id'] = broadcast_id
+        return out


Test patch:
diff --git a/tests/test_pyperi.py b/tests/test_pyperi.py
index 16c7adc..6282d0c 100644
--- a/tests/test_pyperi.py
+++ b/tests/test_pyperi.py
@@ -134,6 +134,23 @@ def test_parse_periscope_url_username_broadcast_id():
             }
 
 
+def test_parse_periscope_url_u():
+    pp = PyPeri()
+    user_ids = [
+        '376827',
+    ]
+
+    for user_id in user_ids:
+        url = 'https://www.periscope.tv/u/{user_id}'.format(
+            user_id=user_id
+        )
+        assert pp.parse_periscope_url(url) == {
+            'username': None,
+            'user_id': user_id,
+            'broadcast_id': None,
+        }
+
+
 def test_parse_periscope_w_url():
     pp = PyPeri()
     broadcast_ids = [
@@ -206,3 +223,20 @@ def test_parse_periscope_username_broadcast_id_url():
     assert pp.parse_periscope_username_broadcast_id_url(
         'https://www.periscope.tv/okUsername/'
     ) == blank_result
+
+
+def test_parse_periscope_u_url():
+    pp = PyPeri()
+    user_ids = [
+        '376827',
+    ]
+
+    for user_id in user_ids:
+        url = 'https://www.periscope.tv/u/{user_id}'.format(
+            user_id=user_id
+        )
+        assert pp.parse_periscope_u_url(url) == {
+            'username': None,
+            'user_id': user_id,
+            'broadcast_id': None,
+        }