Python urllib.parse.parse_qs() Examples
The following are 30
code examples of urllib.parse.parse_qs().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
urllib.parse
, or try the search function
.
Example #1
Source File: rse.py From rucio with Apache License 2.0 | 6 votes |
def GET(self, rse): """ Get RSE usage information. :param rse: the RSE name. """ header('Content-Type', 'application/x-json-stream') source = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'source' in params: source = params['source'][0] try: for usage in list_rse_usage_history(rse=rse, issuer=ctx.env.get('issuer'), source=source): yield render_json(**usage) + '\n' except RSENotFound as error: raise generate_http_error(404, 'RSENotFound', error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #2
Source File: login.py From floyd-cli with Apache License 2.0 | 6 votes |
def do_GET(self): params = urlparse.parse_qs(urlparse.urlparse(self.path).query) key = params.get('apikey') if not key: self.send_response(400) return self.server.key_queue.put(key[0]) self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS') self.send_header('Content-type', 'text/html') self.end_headers() page_content = (""" <html> <header> <script> window.location.replace("%s/cli_login?keystate=sent"); </script> </header> </html> """ % (floyd.floyd_web_host)).encode('utf-8') self.wfile.write(page_content)
Example #3
Source File: auth_fitbit.py From fitbit-googlefit with GNU General Public License v3.0 | 6 votes |
def headless_authorize(self): """ Authorize without a display using only TTY. """ url, _ = self.oauth.authorize_token_url(redirect_uri=self.redirect_uri) # Ask the user to open this url on a system with browser print('\n-------------------------------------------------------------------------') print('\t\tOpen the below URL in your browser\n') print(url) print('\n-------------------------------------------------------------------------\n') print('NOTE: After authenticating on Fitbit website, you will redirected to a URL which ') print('throws an ERROR. This is expected! Just copy the full redirected here.\n') redirected_url = input('Full redirected URL: ') params = urlparse.parse_qs(urlparse.urlparse(redirected_url).query) print(params['code'][0]) self.authenticate_code(code=params['code'][0])
Example #4
Source File: api.py From insightconnect-plugins with MIT License | 6 votes |
def get_agents(self, count=1000, name="", listening=""): agents = [] url = f"{self.url}/agents" params = {'count': count, 'name': name, 'listening': listening} while True: response = self._call_api("GET", url, params=params) agents = agents + response.get('value', []) try: # Only retrieve start parameter from next url href since API does NOT include name and listening # parameters even if included in original request so the next href can't be used blindly query_params = parse_qs(urlparse(response['links']['next']['href']).query) # Add or overwrite start parameter for next request params['start'] = query_params['start'] except KeyError: # Return current list of agents when there are no more links to follow return agents
Example #5
Source File: test_serializers.py From normandy with Mozilla Public License 2.0 | 6 votes |
def test_it_cachebusts_x5u(self, settings): signature = SignatureFactory() # If none, do not cache bust settings.AUTOGRAPH_X5U_CACHE_BUST = None serializer = SignatureSerializer(instance=signature) url_parts = list(urlparse.urlparse(serializer.data["x5u"])) query = urlparse.parse_qs(url_parts[4]) assert "cachebust" not in query # If set, cachebust using the value settings.AUTOGRAPH_X5U_CACHE_BUST = "new" serializer = SignatureSerializer(instance=signature) url_parts = list(urlparse.urlparse(serializer.data["x5u"])) query = urlparse.parse_qs(url_parts[4]) assert "cachebust" in query assert len(query["cachebust"]) == 1 assert query["cachebust"][0] == "new"
Example #6
Source File: core.py From XSSCon with MIT License | 6 votes |
def get_method(self): bsObj=BeautifulSoup(self.body,"html.parser") links=bsObj.find_all("a",href=True) for a in links: url=a["href"] if url.startswith("http://") is False or url.startswith("https://") is False or url.startswith("mailto:") is False: base=urljoin(self.url,a["href"]) query=urlparse(base).query if query != "": Log.warning("Found link with query: "+G+query+N+" Maybe a vuln XSS point") query_payload=query.replace(query[query.find("=")+1:len(query)],self.payload,1) test=base.replace(query,query_payload,1) query_all=base.replace(query,urlencode({x: self.payload for x in parse_qs(query)})) Log.info("Query (GET) : "+test) Log.info("Query (GET) : "+query_all) _respon=self.session.get(test) if self.payload in _respon.text or self.payload in self.session.get(query_all).text: Log.high("Detected XSS (GET) at "+_respon.url) else: Log.info("This page is safe from XSS (GET) attack but not 100% yet...")
Example #7
Source File: PaginatedList.py From gist-alfred with MIT License | 6 votes |
def totalCount(self): if not self.__totalCount: params = {} if self.__nextParams is None else self.__nextParams.copy() # set per_page = 1 so the totalCount is just the number of pages params.update({"per_page": 1}) headers, data = self.__requester.requestJsonAndCheck( "GET", self.__firstUrl, parameters=params, headers=self.__headers ) if 'link' not in headers: if data and "total_count" in data: self.__totalCount = data["total_count"] elif data: self.__totalCount = len(data) else: self.__totalCount = 0 else: links = self.__parseLinkHeader(headers) lastUrl = links.get("last") self.__totalCount = int(parse_qs(lastUrl)['page'][0]) return self.__totalCount
Example #8
Source File: oauth_test.py From earthengine with MIT License | 6 votes |
def testRequestToken(self): class MockResponse(object): def __init__(self, code): self.code = code.decode() def read(self): return ('{"refresh_token": "' + self.code + '456"}').encode() def mock_urlopen(unused_url, param): return MockResponse(urlparse.parse_qs(param)[b'code'][0]) # Choose urlopen function to mock based on Python version if sys.version_info[0] < 3: urlopen_lib = 'urllib2.urlopen' else: urlopen_lib = 'urllib.request.urlopen' with mock.patch(urlopen_lib, new=mock_urlopen): auth_code = '123' refresh_token = ee.oauth.request_token(auth_code) self.assertEqual('123456', refresh_token)
Example #9
Source File: util.py From tekore with MIT License | 6 votes |
def parse_code_from_url(url: str) -> str: """ Parse an URL for parameter 'code'. Returns ------- str value of 'code' Raises ------ KeyError if 'code' is not available or has multiple values """ query = urlparse(url).query code = parse_qs(query).get('code', None) if code is None: raise KeyError('Parameter `code` not available!') elif len(code) > 1: raise KeyError('Multiple values for `code`!') return code[0]
Example #10
Source File: utils.py From normandy with Mozilla Public License 2.0 | 6 votes |
def urlparams(url, fragment=None, **kwargs): """ Add a fragment and/or query parameters to a URL. Existing query string parameters are preserved, unless they conflict with the new parameters, in which case they're overridden. """ parsed = urlparse(url) query = dict(parse_qs(parsed.query), **kwargs) return urlunparse( ( parsed.scheme, parsed.netloc, parsed.path, parsed.params, urlencode(query, doseq=True), fragment if fragment is not None else parsed.fragment, ) )
Example #11
Source File: rse.py From rucio with Apache License 2.0 | 6 votes |
def GET(self, rse): """ Get RSE usage information. :param rse: the RSE name. """ header('Content-Type', 'application/x-json-stream') source = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'source' in params: source = params['source'][0] try: for usage in list_rse_usage_history(rse=rse, issuer=ctx.env.get('issuer'), source=source): yield render_json(**usage) + '\n' except RSENotFound as error: raise generate_http_error(404, 'RSENotFound', error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #12
Source File: social_context.py From news-popularity-prediction with Apache License 2.0 | 6 votes |
def get_video_id(url): parsed_url = urlparse(url) if parsed_url.netloc == "youtu.be": video_id = parsed_url.path[1:] if len(video_id) != 11: video_id = None else: parsed_query = parse_qs(parsed_url.query) try: video_id = parsed_query["v"] if type(video_id) == type(list()): # TODO: Use isinstance() video_id = video_id[0] except KeyError: video_id = None return video_id
Example #13
Source File: test_oidc.py From rucio with Apache License 2.0 | 5 votes |
def get_auth_init_and_mock_response(self, code_response, account=None, polling=False, auto=True, session=None): """ OIDC creates entry in oauth_requests table returns: auth_query_string (state=xxx&code=yyy as would be returned from the IdP after a successful authentication) """ if not account: account = self.accountstring try: kwargs = {'auth_scope': 'openid profile', 'audience': 'rucio', 'issuer': 'dummy_admin_iss_nickname', 'auto': auto, 'polling': polling, 'refresh_lifetime': 96, 'ip': None, 'webhome': 'https://rucio-test.cern.ch/ui' } auth_url = get_auth_oidc(InternalAccount(account), session=session, **kwargs) # get the state from the auth_url and add an arbitrary code value to the query string # to mimick a return of IdP with authz_code urlparsed = urlparse(auth_url) urlparams = parse_qs(urlparsed.query) if ('_polling' in auth_url) or (not polling and not auto): auth_url = redirect_auth_oidc(urlparsed.query, session=session) urlparsed = urlparse(auth_url) urlparams = parse_qs(urlparsed.query) state = urlparams["state"][0] nonce = urlparams["nonce"][0] auth_query_string = "state=" + state + "&code=" + code_response return {'state': state, 'nonce': nonce, 'auth_url': auth_url, 'auth_query_string': auth_query_string} except: print(traceback.format_exc())
Example #14
Source File: lock.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope, name): """ get locks for a given scope, name. HTTP Success: 200 OK HTTP Error: 404 Not Found 406 Not Acceptable 500 InternalError :returns: JSON dict containing informations about the requested user. """ header('Content-Type', 'application/x-json-stream') did_type = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'did_type' in params: did_type = params['did_type'][0] try: if did_type == 'dataset': for lock in get_dataset_locks(scope, name): yield render_json(**lock) + '\n' else: raise InternalError('Wrong did_type specified') except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: raise InternalError(error)
Example #15
Source File: lock.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, rse): """ get locks for a given rse. HTTP Success: 200 OK HTTP Error: 404 Not Found 406 Not Acceptable 500 InternalError :returns: JSON dict containing informations about the requested user. """ header('Content-Type', 'application/x-json-stream') did_type = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'did_type' in params: did_type = params['did_type'][0] try: if did_type == 'dataset': for lock in get_dataset_locks_by_rse(rse): yield render_json(**lock) + '\n' else: raise InternalError('Wrong did_type specified') except RSENotFound as error: raise generate_http_error(404, error.__class__.__name__, error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: raise InternalError(error)
Example #16
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def GET(self): """ List the suspicious replicas on a lsit of RSEs. HTTP Success: 200 OK HTTP Error: 406 Not Acceptable 500 InternalError """ header('Content-Type', 'application/json') result = [] rse_expression, younger_than, nattempts = None, None, None if ctx.query: try: params = loads(unquote(ctx.query[1:])) except ValueError: params = parse_qs(ctx.query[1:]) print(params) if 'rse_expression' in params: rse_expression = params['rse_expression'][0] if 'younger_than' in params and params['younger_than'][0]: younger_than = datetime.strptime(params['younger_than'][0], "%Y-%m-%dT%H:%M:%S") if 'nattempts' in params: nattempts = int(params['nattempts'][0]) try: result = get_suspicious_files(rse_expression=rse_expression, younger_than=younger_than, nattempts=nattempts) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error) return render_json_list(result)
Example #17
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def DELETE(self, scope, name): """ Deletes the specified key from the DID HTTP Success: 200 OK HTTP Error: 401 Unauthorized 404 KeyNotFound """ key = "" if ctx.query: params = parse_qs(ctx.query[1:]) if 'key' in params: key = params['key'][0] else: raise generate_http_error(404, 'KeyNotFound', 'No key provided to remove') try: delete_did_meta(scope=scope, name=name, key=key) except KeyNotFound as error: raise generate_http_error(404, 'KeyNotFound', error.args[0]) except DataIdentifierNotFound as error: raise generate_http_error(404, 'DataIdentifierNotFound', error.args[0]) except NotImplementedError: raise generate_http_error(409, 'NotImplementedError', 'Feature not in current database') except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error) raise OK()
Example #18
Source File: rse.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, rse): """ Get RSE usage information. :param rse: the RSE name. """ header('Content-Type', 'application/x-json-stream') usage = None source = None per_account = False if ctx.query: params = parse_qs(ctx.query[1:]) if 'source' in params: source = params['source'][0] if 'per_account' in params: per_account = params['per_account'][0] == 'True' try: usage = get_rse_usage(rse, issuer=ctx.env.get('issuer'), source=source, per_account=per_account) except RSENotFound as error: raise generate_http_error(404, 'RSENotFound', error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error) for u in usage: yield render_json(**u) + '\n'
Example #19
Source File: lock.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope, name): """ get locks for a given scope, name. HTTP Success: 200 OK HTTP Error: 404 Not Found 406 Not Acceptable 500 InternalError :returns: JSON dict containing informations about the requested user. """ header('Content-Type', 'application/x-json-stream') did_type = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'did_type' in params: did_type = params['did_type'][0] try: if did_type == 'dataset': for lock in get_dataset_locks(scope, name): yield render_json(**lock) + '\n' else: raise InternalError('Wrong did_type specified') except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: raise InternalError(error)
Example #20
Source File: lock.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, rse): """ get locks for a given rse. HTTP Success: 200 OK HTTP Error: 404 Not Found 406 Not Acceptable 500 InternalError :returns: JSON dict containing informations about the requested user. """ header('Content-Type', 'application/x-json-stream') did_type = None if ctx.query: params = parse_qs(ctx.query[1:]) if 'did_type' in params: did_type = params['did_type'][0] try: if did_type == 'dataset': for lock in get_dataset_locks_by_rse(rse): yield render_json(**lock) + '\n' else: raise InternalError('Wrong did_type specified') except RSENotFound as error: raise generate_http_error(404, error.__class__.__name__, error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: raise InternalError(error)
Example #21
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def DELETE(self, scope, name): """ Deletes the specified key from the DID HTTP Success: 200 OK HTTP Error: 401 Unauthorized 404 KeyNotFound """ key = "" if ctx.query: params = parse_qs(ctx.query[1:]) if 'key' in params: key = params['key'][0] else: raise generate_http_error(404, 'KeyNotFound', 'No key provided to remove') try: delete_did_meta(scope=scope, name=name, key=key) except KeyNotFound as error: raise generate_http_error(404, 'KeyNotFound', error.args[0]) except DataIdentifierNotFound as error: raise generate_http_error(404, 'DataIdentifierNotFound', error.args[0]) except NotImplementedError: raise generate_http_error(409, 'NotImplementedError', 'Feature not in current database') except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error) raise OK()
Example #22
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def GET(self): """ List all data identifiers in a scope(optional) which match a given metadata. HTTP Success: 200 OK HTTP Error: 406 Not Acceptable 500 Server Error :param scope: The scope name. """ select = {} scope = "" if ctx.query: params = parse_qs(ctx.query[1:]) if 'scope' in params: scope = params['scope'][0] if 'select' in params: select = loads(params['select'][0]) try: dids = list_dids_by_meta(scope=scope, select=select) yield dumps(dids, cls=APIEncoder) + '\n' except NotImplementedError: raise generate_http_error(409, 'NotImplementedError', 'Feature not in current database') except Exception as error: print(format_exc()) raise InternalError(error)
Example #23
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def GET(self): """ Returns list of recent identifiers. HTTP Success: 200 OK HTTP Error: 401 Unauthorized 406 Not Acceptable :param type: The DID type. """ header('Content-Type', 'application/x-json-stream') params = parse_qs(ctx.query[1:]) type = None if 'type' in params: type = params['type'][0] try: for did in list_new_dids(type): yield dumps(did, cls=APIEncoder) + '\n' except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: raise InternalError(error)
Example #24
Source File: test_url.py From ACE with Apache License 2.0 | 5 votes |
def test_protected_url_sharepoint(self): root = create_root_analysis() root.initialize_storage() # taken from an actual sample url = root.add_observable(F_URL, 'https://lahia-my.sharepoint.com/:b:/g/personal/secure_onedrivemsw_bid/EVdjoBiqZTxMnjAcDW6yR4gBqJ59ALkT1C2I3L0yb_n0uQ?e=naeXYD') url.add_directive(DIRECTIVE_CRAWL) root.save() root.schedule() engine = TestEngine() engine.enable_module('analysis_module_protected_url_analyzer', 'test_groups') engine.controlled_stop() engine.start() engine.wait() root.load() url = root.get_observable(url.id) from saq.modules.url import ProtectedURLAnalysis, PROTECTION_TYPE_SHAREPOINT analysis = url.get_analysis(ProtectedURLAnalysis) self.assertIsNotNone(analysis) self.assertEquals(analysis.protection_type, PROTECTION_TYPE_SHAREPOINT) from urllib.parse import urlparse, parse_qs parsed_url = urlparse(analysis.extracted_url) self.assertEquals(parsed_url.path, '/personal/secure_onedrivemsw_bid/_layouts/15/download.aspx') parsed_qs = parse_qs(parsed_url.query) self.assertEquals(parsed_qs['e'][0], 'naeXYD') self.assertEquals(parsed_qs['share'][0], 'EVdjoBiqZTxMnjAcDW6yR4gBqJ59ALkT1C2I3L0yb_n0uQ') extracted_url = analysis.get_observables_by_type(F_URL) self.assertEquals(len(extracted_url), 1) extracted_url = extracted_url[0] self.assertTrue(extracted_url.has_directive(DIRECTIVE_CRAWL))
Example #25
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope, name): """ Retrieve a single data identifier. HTTP Success: 200 OK HTTP Error: 401 Unauthorized 406 Not Acceptable 404 Not Found :param scope: The scope name. :param name: The data identifier name. """ header('Content-Type', 'application/json') try: dynamic = False if ctx.query: params = parse_qs(ctx.query[1:]) if 'dynamic' in params: dynamic = True did = get_did(scope=scope, name=name, dynamic=dynamic) return render_json(**did) except ScopeNotFound as error: raise generate_http_error(404, 'ScopeNotFound', error.args[0]) except DataIdentifierNotFound as error: raise generate_http_error(404, 'DataIdentifierNotFound', error.args[0]) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #26
Source File: did.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope): """ Return all data identifiers in the given scope. HTTP Success: 200 OK HTTP Error: 401 Unauthorized 406 Not Acceptable 404 Not Found :param scope: The scope name. """ header('Content-Type', 'application/x-json-stream') name = None recursive = False if ctx.query: params = parse_qs(ctx.query[1:]) if 'name' in params: name = params['name'][0] if 'recursive' in params: recursive = True try: for did in scope_list(scope=scope, name=name, recursive=recursive): yield render_json(**did) + '\n' except DataIdentifierNotFound as error: raise generate_http_error(404, 'DataIdentifierNotFound', error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #27
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope, name): """ List dataset replicas for a DID (scope:name) using the Virtual Placement service. NOTICE: This is an RnD function and might change or go away at any time. HTTP Success: 200 OK HTTP Error: 401 Unauthorized 406 Not Acceptable 500 InternalError :returns: If VP exists a list of dicts of sites, otherwise nothing """ header('Content-Type', 'application/x-json-stream') deep = False if ctx.query: try: params = loads(unquote(ctx.query[1:])) except ValueError: params = parse_qs(ctx.query[1:]) if 'deep' in params: deep = params['deep'][0] try: for row in list_dataset_replicas_vp(scope=scope, name=name, deep=deep): yield dumps(row, cls=APIEncoder) + '\n' except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #28
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def GET(self, scope, name): """ List dataset replicas replicas. HTTP Success: 200 OK HTTP Error: 401 Unauthorized 406 Not Acceptable 500 InternalError :returns: A dictionary containing all replicas information. """ header('Content-Type', 'application/x-json-stream') deep = False if ctx.query: try: params = loads(unquote(ctx.query[1:])) except ValueError: params = parse_qs(ctx.query[1:]) if 'deep' in params: deep = params['deep'][0] try: for row in list_dataset_replicas(scope=scope, name=name, deep=deep): yield dumps(row, cls=APIEncoder) + '\n' except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error)
Example #29
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def GET(self): """ List the suspicious replicas on a lsit of RSEs. HTTP Success: 200 OK HTTP Error: 406 Not Acceptable 500 InternalError """ header('Content-Type', 'application/json') result = [] rse_expression, younger_than, nattempts = None, None, None if ctx.query: try: params = loads(unquote(ctx.query[1:])) except ValueError: params = parse_qs(ctx.query[1:]) print(params) if 'rse_expression' in params: rse_expression = params['rse_expression'][0] if 'younger_than' in params and params['younger_than'][0]: younger_than = datetime.strptime(params['younger_than'][0], "%Y-%m-%dT%H:%M:%S") if 'nattempts' in params: nattempts = int(params['nattempts'][0]) try: result = get_suspicious_files(rse_expression=rse_expression, younger_than=younger_than, nattempts=nattempts) except RucioException as error: raise generate_http_error(500, error.__class__.__name__, error.args[0]) except Exception as error: print(format_exc()) raise InternalError(error) return render_json_list(result)
Example #30
Source File: get_youtube_video_id.py From Some-Examples-of-Simple-Python-Script with GNU Affero General Public License v3.0 | 5 votes |
def get_yt_video_id(url): """Returns Video_ID extracting from the given url of Youtube Examples of URLs: Valid: 'http://youtu.be/_lOT2p_FCvA', 'www.youtube.com/watch?v=_lOT2p_FCvA&feature=feedu', 'http://www.youtube.com/embed/_lOT2p_FCvA', 'http://www.youtube.com/v/_lOT2p_FCvA?version=3&hl=en_US', 'https://www.youtube.com/watch?v=rTHlyTphWP0&index=6&list=PLjeDyYvG6-40qawYNR4juzvSOg-ezZ2a6', 'youtube.com/watch?v=_lOT2p_FCvA', 'https://www.youtube.com/watch?v=S6q41Rfltsk' Invalid: 'youtu.be/watch?v=_lOT2p_FCvA', """ if url.startswith(('youtu', 'www')): url = 'http://' + url query = urlparse(url) if 'youtube' in query.hostname: if query.path == '/watch': return parse_qs(query.query)['v'][0] elif query.path.startswith(('/embed/', '/v/')): return query.path.split('/')[2] elif 'youtu.be' in query.hostname: return query.path[1:] else: raise ValueError