The topic is dedicated to those who do not like to download heavy files from the file sharing browser, as well as for those who want to better understand the work of zenity.
In Linux, there are a lot of different rockers, from console to heavy java applications, and my script is not another new rocker, it's just a nice eye-candy wrapper for standard Wget. Although potentially he can do everything Wget can do, but I have implemented only its most basic functions. The purpose of this script is to start the download via wget (untie it from the browser) and show the progress of its execution.
There is a great
Zenity utility in the GTK +
environment which I will use to display the dialog boxes. The link shows in detail all aspects of use, so I will not do a review, but I’ll show you how I applied it.
So, in my script I implemented minimal functionality to download comfortably: a list of links, proxy selection, http, ftp protocols, fixing the name of the uploaded file (very useful for videos from YouTube and the like), working from the command line (for work with the Firefox plugin
FlashGot ).
')
#! / bin / bash
#Usage: ./guiwget
# with FlashGot arguments are: [URL] [REFERER] [POST]
#Dependens: wget> = 1.11.1, zenity
Yes Yes! There is a difference in the versions of wget. In version 1.11.1, wget began to support the option - content-disposition which is necessary when downloading from exchangers, for example, ifolder or youtube. Also shown are options for FlashGot to be able to download flv-videos, and transfer downloads to wget.
#Get URL
[ "$ 1" ] && url = $ 1 || url = " $ (zenity --text-info --editable --title =" Enter URL list "--width = 500 --height = 300) " ;
[ " $ url " ] || exit 0 ;
If the link was transmitted via FlashGot, then the parameters will be processed, and if it was launched directly, a window will appear asking for a list of URLs. The script will exit if the download list is empty.
#Proxy
proxy1 = "Angola" ; proxy1addr = "41.210.252.11:80" ;
proxy2 = Bangladesh ; proxy2addr = "202.168.236.178:8080" ;
p = " $ (zenity --list --title =" Select proxy "--print-column = 2 --column =" "--column =" "" $ proxy1 "" $ proxy1addr "" $ proxy2 "" $ proxy2addr ") " ;
[ " $ p " = "" ] || proxy = "export http_proxy = http: // $ p " ;
Working with a proxy, nothing unusual, for example, two proxies are shown. In theory, you need to implement the work of importing a prepared proxy list, but I do not need it. If you want to download directly, click the "Cancel" button in the window.
#Folder selection for file downloading
downdir = " $ (zenity --file-selection --directory --title =" Select the directory to save ") " " ;
[ " $ downdir " ] || exit 0 ;
Select a folder to download. You can hard-write the path in the script, but I love the variety :)
Setting parameters is over, the most basic work begins:
for urs in $ url
do
#Log file
output = "/ tmp / ` date +% N` -log " ;
#Wget start
id = $ ( $ proxy ; wget -b -o " $ output " -P " $ downdir " --referer = "$ 2" --post-data = "$ 3" " $ urs " | egrep -o "[0- 9] + " ) ;
[ " $ id " ] || exit 0 ;
We start wget with the received parameters, send it to the background, write the download progress in the log, which will be processed, remember the process id.
#Name file and size
h = "" ;
while [ " $ h " == "" ] ; do
if [ -x / proc / $ id ]
then
h = $ ( egrep -o "[^ \ /] + '. $" $ output ) ;
[ " $ h " = "" ] && h = $ ( sed -n '/RETR/s/.*RETR \ ([[:: print:]] * \) ... done. $ / \ 1 / p' $ output ) ;
realname = " $ {h% \ '.} " ;
downfile = " $ {realname %% \? *} " ;
[ " $ realname " ! = " $ downfile " ] && [ -f " $ downdir / $ downfile " ] && downfile = ` date + % N` "_" $ downfile ;
else
zenity --text-info --title = "Error" --filename = $ output --width = 700 --height = 400 ;
h = "FAIL" ;
rm $ output ;
fi
done ;
[ " $ h " == "FAIL" ] && continue ;
size = $ ( grep "Length:" $ output | tail -1 | egrep -o "\ (. + \)" | cut -d "(" -f2 | cut -d ")" -f1 ) ;
We define the file name, size and generally make sure that the server gives us the file, if something goes wrong, a window with a log is shown, where wget usually writes exactly what is wrong. The variable downfile is needed in order to correctly display the file name, as well as to turn the file name file? Var = 1 & var = 2 into file.
One nuance - the work goes with the log in Russian and Russian words are intercepted, if you have it wrong, correct the words “ready” and “Length” in your script.
#Grafical progressbar
( tail -f --pid = $ id $ output | sed -u -n 's /.* \. [] * \ ([[:: print:]] * \)% [] * \ ([[:: :]] * \) \ ([[::]] * \) $ / # ' " $ downfile " ': ' $ size ' \ 1% [\ 2B \ / s] (\ 3) \ n \ 1 / p ' ) | zenity --title = "Loading file $ p " --progress --percentage = 0 --auto-close
[ $? -eq 0 ] && notify-send "Download completed" "File: $ downfile " || ( [ -x / proc / $ id ] && kill $ id || zenity --text-info --title = "Error" --filename = $ output --width = 700 --height = 400 ) ;
mv " $ downdir / $ realname " " $ downdir / $ downfile " ;
#Clean
rm $ output ;
done
And here is my know-how, telling how it works.
tail -f --pid is ideal for monitoring the log, and it also monitors the pid of the process, so that when the process with the specified pid is completed, tail also stops the operation of the pipeline. The stream tail sends to the stream (unlike awk) sed file of the wget log file, which processes it, extracts the necessary information and formats it properly, and then the text comes to zenity.
I will a little explain, in zenity --progress it is necessary to give the text in the form:
# text to display
0
# text to display
ten
...
where the second line is the number of percent. This conversion from the wget log is done by tail + sed. Then there is error handling or a pop-up message. If the download was completed successfully, the file should also be renamed, if necessary.
Well, that's it, save the file where it suits you, set the execution bit, add the launch button to the panel and use or send tasks via Firefox + FlashGot.
And the final touch, in order to defeat Rapidshare and other file sharing services that do not like link interceptors, add the following lines to the .wgetrc file:
header = Accept-Language: ru, en-us; q = 0.7, en; q = 0.3
header = Accept: text / html, application / xhtml + xml, application / xml; q = 0.9, * / *; q = 0.8
header = Accept-Encoding: gzip, deflate
header = Accept-Charset: UTF-8, *
header = Keep-Alive: 300
header = Connection: keep-alive
user_agent = Mozilla / 5.0 (X11; U; Linux i686; en; rv: 1.9.1.2) Gecko / 20090804 Shiretoko / 3.5.2
content_disposition = on
User agent correct in accordance with your browser.
The script can be taken here:
nebulosa2007.narod.ru/guiwget.tar.gz