I'm downloading episodes from a series, but I'm tired for joining in the site, copying the link and downloading it for each episode.
First, I join in the site and I get the HTML code. The code is big, but I'm interested in this in particular (it is where the link is)
var e=Array(),d=1;e[1]='<div><object width="720" height="450" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=""><param name="movie" value="//"><param name="bgcolor" value="#FFFFFF"><param name="allowfullscreen" value="true"><param name="flashvars" value="flvurl="><embed src="//" type="application/x-shockwave-flash"width="720" height="450" bgcolor="#FFFFFF"flashvars="flvurl="allowFullScreen="true" /></object></div>';e[2]='<div><iframe src="" width="720" height="450" scrolling="no" frameborder="0"></iframe></div>';Well, its a looong code, but I'm interested to get this. (There are 2 identical links, both work without the '&' in the end)
Now, I'm using this regEX (Note I'm noob with this, I test it and works)
\s*flvurl\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));*%26key%3Dck2&And, in the terminal I use
wget -q -O - "serverlink" | sed -e "s/\s*flvurl\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));*%26key%3Dck2&//g"I get error with unexpected token) '`
Does anyone know how I can fix it? (Or, maybe, someone knows a better regex to get the link from the first code box)
102 Answers
Here are some simpler approaches to get what you want:
grepwith PCREswget -q -O - "serverlink" | grep -oP 'flvurl=\K[^;]+(?=&)'Explanation
In PCREs, the
\Kmeans "discard everything matched until here". The-ooption togrepmeans "print only the matched portion of a string". So,grep -oP 'flvurl=\Kmeans: "look forflvurl=and start matching after it." The[^;]+means "match as many non-;characters as possible.The
foo(?=bar)format is called a positive lookahead. It is not part of the actual matching string. It means _"matchfoofollowed bybarbut discard thebar. Putting all this together, the regular expression above will print everything betweenflvurl=and&.sedwget -q -O - "serverlink" | sed -nr 's/.*flvurl=([^;]+)&.*/\1/p'Explanation
The substitution operator (I use the traditional
s///instead of thes\\\you used but its the same idea), will substitute everything with the text betweenflvurl=and&. The parentheses are there to "capture" the matched string, making it available as\1. The-roption enables extended regular expressions which can deal with parentheses without needing to escape (\(and\)) them. That's the reason you were getting the error by the way.The
-nsuppresses normal output, no lines will be printed by default. Thepat the end of the substitution (s///p) means "print the current line if the substitution was successful".Perl
Using the same logic as the
sedabove:wget -q -O - "serverlink" | perl -ne 's/.*flvurl=([^;]+)&.*/$1/ && print'
Your command line is piping the output of wget into sed - in order to substitute the result of the sed command as a URL argument on the wget command line, you will need to do something like
wget -q -O- -- $(echo -n "urlstring" | sed -e 's/pattern/replacement/')or
wget -q -O- -- $(sed -e 's/pattern/replacement/' <<< "urlstring")The -e may be omitted in this context.