ashish
ashish

Reputation: 151

how to get the exact name of downloading video from python script

url = "http://r2---sn-pgpq5-gv8e.googlevideo.com/videoplayback?beids=%5B9452306%5D&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&dur=18.250&itag=18&mime=video%2Fmp4&expire=1488221864&lmt=1417031606585030&upn=GKIFYrlNn_U&source=youtube&id=o-ADqh1mH-ZsRDFnyLkiBlBWkyMjV76jLUJ6q_ZdYUsoZ4&ratebypass=yes&pl=24&mm=31&mn=sn-pgpq5-gv8e&key=yt6&ip=91.222.120.113&signature=B3E780A72E0AA7C86D1562EE983283E7AEB0D35A.68FE2B8E21DA731E56AD6B44E658D5F61501890C&ipbits=0&ms=au&mt=1488200127&initcwndbps=3640000&mv=m&title=20+second+video"

import urllib, urlparse,time

split = urlparse.urlsplit(url)

filename = split.path.split("/")[-1]

it gives the filename as videoplayback but it should be 20 second video

how to solve it

Upvotes: 0

Views: 87

Answers (1)

ewcz
ewcz

Reputation: 13097

If I understand you question correctly, you want to extract the parameter title from the query string of your URL. In order to do this, one might use the function urlsplit. The query string is then available via the attribute query as shown in the code snippet below.

In the next step, it is necessary to parse as well as decode the query string itself. Although it might be tempting to do it ab initio, I would recommend the function parse_qs from the same module. This function returns a dictionary encapsulating all parameters/variables found in the query string. However, or a given parameter name, there might be multiple values present - that's way params['title'] below is in general a list. Nevertheless, for the URL in question, there is only one value of "20 second video".

Finally, note that for Python 3.X, it's necessary to replace the import statement with from urllib.parse import parse_qs, urlsplit.

from urlparse import parse_qs, urlsplit

url = ...

parts = urlsplit(url)
params = parse_qs(parts.query)
print(params['title'][0])

Upvotes: 1

Related Questions