Repository: takeontom/PyPeri

Issue:
Retrieve Username from "https://www.periscope.tv/<username>/" URLs

Discussion:

Patch:
diff --git a/docs/usage.rst b/docs/usage.rst
index 8b39b7c..aa54fc3 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/<username>
 
 Example usage::
 
diff --git a/pyperi/__init__.py b/pyperi/__init__.py
index d5fb17e..4005d09 100644
--- a/pyperi/__init__.py
+++ b/pyperi/__init__.py
@@ -2,4 +2,4 @@
 
 __author__ = """Tom Smith"""
 __email__ = 'tom@takeontom.com'
-__version__ = '0.1.0'
+__version__ = '0.1.1'
diff --git a/pyperi/pyperi.py b/pyperi/pyperi.py
index 2c8d5c5..5dcfaa5 100644
--- a/pyperi/pyperi.py
+++ b/pyperi/pyperi.py
@@ -5,8 +5,8 @@ from re import match
 
 
 class PyPeri(object):
-    PERISCOPE_API_BASE_URL = 'https://api.periscope.tv/api/v2/'
-    PERISCOPE_WEB_BASE_URL = 'https://www.periscope.tv/'
+    PERISCOPE_API_BASE_URL = 'https://api.periscope.tv/api/v2'
+    PERISCOPE_WEB_BASE_URL = 'https://www.periscope.tv'
 
     def request_api(self, endpoint, **params):
         """
@@ -25,7 +25,7 @@ class PyPeri(object):
         # order.
         params_ordered = OrderedDict(sorted(params.items()))
 
-        url = '{base_url}{endpoint}?{params}'.format(
+        url = '{base_url}/{endpoint}?{params}'.format(
             base_url=PyPeri.PERISCOPE_API_BASE_URL,
             endpoint=endpoint,
             params=urllib.parse.urlencode(params_ordered),
@@ -62,17 +62,73 @@ class PyPeri(object):
         * user_id
         * username
         """
+
+        url_parsers = [
+            self.parse_periscope_w_url,
+            self.parse_periscope_username_url,
+        ]
+
         out = {
             'broadcast_id': None,
             'user_id': None,
             'username': None,
         }
 
-        w_url_pattern = '{base_url}w/([A-Za-z0-9]+)'.format(
+        for url_parser in url_parsers:
+            parse_result = url_parser(url)
+            if parse_result != out:
+                return parse_result
+
+        return out
+
+    def parse_periscope_w_url(self, url):
+        """
+        Retrieve the `broadcast_id` from the supplied "/w/" URL.
+
+        "/w/" URLs look like this:
+
+            * 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 = {
+            'broadcast_id': None,
+            'user_id': None,
+            'username': None,
+        }
+
+        w_url_pattern = '{base_url}/w/([A-Za-z0-9]+)'.format(
             base_url=PyPeri.PERISCOPE_WEB_BASE_URL
         )
+
         w_result = match(w_url_pattern, url)
         if w_result:
-            out['broadcast_id'] = w_result.group(1)
+            broadcast_id = w_result.group(1)
+            out['broadcast_id'] = broadcast_id
+        return out
+
+    def parse_periscope_username_url(self, url):
+        """
+        Extracts the username from URLs formatted like this:
+
+            * https://www.periscope.tv/<username>
 
+        Returns a dict of `username`, `broadcast_id` and `user_id` values.
+        """
+        out = {
+            'broadcast_id': None,
+            'user_id': None,
+            'username': None,
+        }
+
+        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)
+            out['username'] = username
         return out
diff --git a/setup.cfg b/setup.cfg
index 0f0caa5..ae330d1 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,5 +1,5 @@
 [bumpversion]
-current_version = 0.1.0
+current_version = 0.1.1
 commit = True
 tag = True
 
@@ -16,3 +16,4 @@ universal = 1
 
 [flake8]
 exclude = docs
+
diff --git a/setup.py b/setup.py
index 87dde27..732ab8e 100644
--- a/setup.py
+++ b/setup.py
@@ -19,7 +19,7 @@ test_requirements = [
 
 setup(
     name='pyperi',
-    version='0.1.0',
+    version='0.1.1',
     description="Use Periscope with Python.",
     long_description=readme + '\n\n' + history,
     author="Tom Smith",


Test patch:
diff --git a/tests/test_pyperi.py b/tests/test_pyperi.py
index 09b7ce0..2ffb5e2 100644
--- a/tests/test_pyperi.py
+++ b/tests/test_pyperi.py
@@ -78,14 +78,69 @@ def test_get_user_info():
     assert result['username'] == 'george_clinton'
 
 
-def test_parse_periscope_url():
+def test_parse_periscope_url_w():
     pp = PyPeri()
+    broadcast_ids = [
+        '1zqKVWybqeDGB',
+    ]
 
-    broadcast_id = '1zqKVWybqeDGB'
-    w_url = 'https://www.periscope.tv/w/{broadcast_id}'.format(
-        broadcast_id=broadcast_id
-    )
-    w_result = pp.parse_periscope_url(w_url)
-    assert w_result['broadcast_id'] == broadcast_id
-    assert w_result['user_id'] is None
-    assert w_result['username'] is None
+    for broadcast_id in broadcast_ids:
+        w_url = 'https://www.periscope.tv/w/{broadcast_id}'.format(
+            broadcast_id=broadcast_id
+        )
+        assert pp.parse_periscope_url(w_url) == {
+            'broadcast_id': broadcast_id, 'user_id': None, 'username': None
+        }
+
+
+def test_parse_periscope_url_username():
+    pp = PyPeri()
+    usernames = [
+        'someusername',
+        'some_username',
+        'SomeUsername',
+    ]
+
+    for username in usernames:
+        username_url = 'https://www.periscope.tv/{username}'.format(
+            username=username
+        )
+        assert pp.parse_periscope_url(username_url) == {
+            'broadcast_id': None, 'user_id': None, 'username': username
+        }
+
+
+def test_parse_periscope_w_url():
+    pp = PyPeri()
+    broadcast_ids = [
+        '1zqKVWybqeDGB',
+    ]
+
+    for broadcast_id in broadcast_ids:
+        w_url = 'https://www.periscope.tv/w/{broadcast_id}'.format(
+            broadcast_id=broadcast_id
+        )
+        assert pp.parse_periscope_w_url(w_url) == {
+            'user_id': None,
+            'username': None,
+            'broadcast_id': broadcast_id,
+        }
+
+
+def test_parse_periscope_username_url():
+    pp = PyPeri()
+    usernames = [
+        'someusername',
+        'some_username',
+        'SomeUsername',
+    ]
+
+    for username in usernames:
+        username_url = 'https://www.periscope.tv/{username}'.format(
+            username=username
+        )
+        assert pp.parse_periscope_username_url(username_url) == {
+            'user_id': None,
+            'username': username,
+            'broadcast_id': None,
+        }