Consider the following URLs
http://m3u.com/tunein.m3u http://asxsomeurl.com/listen.asx:8024 http://www.plssomeotherurl.com/station.pls?id=111 http://22.198.133.16:8024
Whats the proper way to determine the file extensions (.m3u/.asx/.pls)? Obviously the last one doesn't have a file extension.
EDIT: I forgot to mention that m3u/asx/pls are playlists (textfiles) for audio streams and must be parsed differently. The goal determine the extension and then send the url to the proper parsing-function. E.g.
url = argv[1] ext = GetExtension(url) if ext == "pls": realurl = ParsePLS(url) elif ext == "asx": realurl = ParseASX(url) (etc.) else: realurl = url Play(realurl)
GetExtension() should return the file extension (if any), preferrably without connecting to the URL.
String extension = uri. substring(url. lastIndexOf(".") + 1);
Derived from the standard Uniform Resource Locator abbreviation (URL), the . url filename extension stands for the Internet Shortcut (. url) file type. The Internet Shortcut feature is part of all Microsoft Windows operating systems, allowing to create quick Internet links on the desktop or in any folder.
Purpose: Identifies requests by whether the requested URL points to an asset with a file extension that matches a literal. The only exception is % which is used for URL encoding.
Use urlparse
to parse the path out of the URL, then os.path.splitext
to get the extension.
import urlparse, os url = 'http://www.plssomeotherurl.com/station.pls?id=111' path = urlparse.urlparse(url).path ext = os.path.splitext(path)[1]
Note that the extension may not be a reliable indicator of the type of the file. The HTTP Content-Type
header may be better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With