Repository: takeontom/PyPeri

Issue:
Retrieve Broadcast ID from "https://www.periscope.tv/w/<broadcast id>" URLs

Discussion:

Patch:
diff --git a/pyperi/pyperi.py b/pyperi/pyperi.py
index b919ee8..672412b 100644
--- a/pyperi/pyperi.py
+++ b/pyperi/pyperi.py
@@ -1,10 +1,12 @@
 import requests
 import urllib
 from collections import OrderedDict
+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/'
 
     def request_api(self, endpoint, **params):
         """
@@ -45,3 +47,28 @@ class PyPeri(object):
         endpoint = 'getUserPublic'
         result = self.request_api(endpoint, user_id=user_id)
         return result['user']
+
+    def parse_periscope_url(self, url):
+        """
+        Get any key information available from the supplied URL.
+
+        Will attempt to retrieve one, or combination of:
+
+        * broadcast_id
+        * user_id
+        * username
+        """
+        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)
+
+        return out


Test patch:
diff --git a/tests/test_pyperi.py b/tests/test_pyperi.py
index d9f4db1..17e1d75 100644
--- a/tests/test_pyperi.py
+++ b/tests/test_pyperi.py
@@ -77,3 +77,16 @@ def test_get_user_info():
     result = pp.get_user_info(user_id)
     assert result['id'] == user_id
     assert result['username'] == 'george_clinton'
+
+
+def test_parse_periscope_url():
+    pp = PyPeri()
+
+    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