Repository: takeontom/PyPeri

Issue:
Retrieve username and broadcast ID from https://www.periscope.tv/<username>/<broadcast id> urls

Discussion:

Patch:
diff --git a/docs/usage.rst b/docs/usage.rst
index aa54fc3..467c0c1 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -115,6 +115,7 @@ Supports the following URL formats:
 
 * https://www.periscope.tv/w/<broadcast_id>
 * https://www.periscope.tv/<username>
+* https://www.periscope.tv/<username>/<broadcast_id>
 
 Example usage::
 
diff --git a/pyperi/pyperi.py b/pyperi/pyperi.py
index 5dcfaa5..3caca5d 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_username_broadcast_id_url,
             self.parse_periscope_username_url,
         ]
 
@@ -127,8 +128,33 @@ class PyPeri(object):
         username_url_pattern = '{base_url}/([A-Za-z0-9_]+)'.format(
             base_url=PyPeri.PERISCOPE_WEB_BASE_URL
         )
-        username_url = match(username_url_pattern, url)
-        if username_url:
-            username = username_url.group(1)
+        username_result = match(username_url_pattern, url)
+        if username_result:
+            username = username_result.group(1)
             out['username'] = username
         return out
+
+    def parse_periscope_username_broadcast_id_url(self, url):
+        """
+        Extract the `username`and `broadcast_id` from URLs formatted like this:
+
+            * https://www.periscope.tv/<username>/<broadcast_id>
+
+        Returns a dict of `username`, `broadcast_id` and `user_id` values.
+        """
+        out = {
+            'broadcast_id': None,
+            'user_id': None,
+            'username': None,
+        }
+
+        url_pattern = '{base_url}/([A-Za-z0-9_]+)/([A-Za-z0-9]+)'.format(
+            base_url=PyPeri.PERISCOPE_WEB_BASE_URL
+        )
+        result = match(url_pattern, url)
+        if result:
+            username = result.group(1)
+            broadcast_id = result.group(2)
+            out['username'] = username
+            out['broadcast_id'] = broadcast_id
+        return out


Test patch:
diff --git a/tests/test_pyperi.py b/tests/test_pyperi.py
index 2ffb5e2..16c7adc 100644
--- a/tests/test_pyperi.py
+++ b/tests/test_pyperi.py
@@ -110,6 +110,30 @@ def test_parse_periscope_url_username():
         }
 
 
+def test_parse_periscope_url_username_broadcast_id():
+    pp = PyPeri()
+    usernames = [
+        'someusername',
+        'some_username',
+        'SomeUsername',
+    ]
+    broadcast_ids = [
+        '1zqKVWybqeDGB',
+    ]
+
+    for username in usernames:
+        for broadcast_id in broadcast_ids:
+            username_url = 'https://www.periscope.tv/{username}/{broadcast_id}'.format(
+                username=username,
+                broadcast_id=broadcast_id,
+            )
+            assert pp.parse_periscope_url(username_url) == {
+                'user_id': None,
+                'username': username,
+                'broadcast_id': broadcast_id,
+            }
+
+
 def test_parse_periscope_w_url():
     pp = PyPeri()
     broadcast_ids = [
@@ -144,3 +168,41 @@ def test_parse_periscope_username_url():
             'username': username,
             'broadcast_id': None,
         }
+
+
+def test_parse_periscope_username_broadcast_id_url():
+    pp = PyPeri()
+    usernames = [
+        'someusername',
+        'some_username',
+        'SomeUsername',
+    ]
+    broadcast_ids = [
+        '1zqKVWybqeDGB',
+    ]
+
+    for username in usernames:
+        for broadcast_id in broadcast_ids:
+            username_url = 'https://www.periscope.tv/{username}/{broadcast_id}'.format(
+                username=username,
+                broadcast_id=broadcast_id,
+            )
+            assert pp.parse_periscope_username_broadcast_id_url(username_url) == {
+                'user_id': None,
+                'username': username,
+                'broadcast_id': broadcast_id,
+            }
+
+    blank_result = {
+        'user_id': None,
+        'username': None,
+        'broadcast_id': None,
+    }
+
+    assert pp.parse_periscope_username_broadcast_id_url(
+        'https://www.periscope.tv/okUsername/'
+    ) == blank_result
+
+    assert pp.parse_periscope_username_broadcast_id_url(
+        'https://www.periscope.tv/okUsername/'
+    ) == blank_result