Wee small hours YouTube scheduler / downloader

Post your working scripts, libraries and tools for AHK v1.1 and older
colmik
Posts: 83
Joined: 11 Mar 2014, 18:21

Wee small hours YouTube scheduler / downloader

11 Mar 2014, 18:45

I wrote this script because I'm such a miserable bugger that I only pay for 10GB download capacity per month. However, under the deal I have with my ISP, I get free Internet time during the night, from midnight till 08:00, so I set up a YouTube download scheduler. I also schedule a daily download of podcasts - but that's another story.

There are two parts to the script. I run an always-active script that does lots of useful things that I use on an hourly basis. Into this always-active script, I wrote the following hot key ...

Code: Select all

#x::
Click R
Sleep 100
Send a
Sleep 100
Fileappend, %clipboard%`n, C:\YTS\YT_Links.txt
return
To use it, open Youtube and position the mouse cursor on the link of a video you want to download (not on the picture!).
Don't click it - just position it. Now press #x (the Windows key and x).
The link is copied into file C:\YTS\YT_Links.txt
Explanation: The YTS directory is where I keep my YouTube Scheduler, and the YT_Links.txt file holds the YouTube Links that I want to download.

There's nothing more to it - when you want to end, simply stop selecting YouTube links. If you open YT_Links.txt, you should see your links in there.

The script that actually schedules and downloads the videos is called YT_Sched.ahk, and lives in the C:\YTS directory
My script doesn't do the actual downloading, but uses a freebie downloader off the web, so if you want to use this script, you will have to download "ilivid.exe". Search for it and install it, but when doing so, keep your wits about you. It is sponsored by advertising, and like many other free programs, it will "offer" to do a number of things you don't want it to do. You will have to uncheck a couple of boxes, or decline certain offers, to opt out of it a) changing your home page, b) changing your default search engine, and c) downloading the ilivid toolbar and Bandoo, which are not required for ilivid to work properly. If you use its advertised search engine, it will swamp you with advertising.

Once ilivid is installed, it's a really nice piece of work. Paste a YouTube link into it to try it out. The download is pretty quick.
If it is set to download too many files simultaneously, they come in slowly, so under Tools/Settings, I have set Maximum Simultaneous Downloads = 2. The way my script runs, this is important.

While iLivid is downloading, it shows the percentage of the download that has taken place, and I use this percentage in the script. I use three bit-map pictures (which are all stored in the same directory as the script) :

* NotStarted.bmp : This is a picture of the "0%" shown before any download starts
* PasteLink.bmp : I search for this simply to locate the "Paste Link" button
* Progress.bmp : This is a picture of the word "Progress" which appears at the header of the column of percentage progress. I use it to locate the column

My three picture files are attached. Copy them to C/YTS. If your iLivid has a different appearance from mine, you may need to re-make these pictures. I did it with MS Paint.
The pictures are of very small details, and the bit-map picture files are tiny.
* Take a screenshot of ilivid at the right moment - paste a link into it and hit DOWNLOAD, but before the download starts, while it still says "0%", hit Screen Capture.
* Paste the screen-shot to MS Paint
* Magnify it to 8x to make it easier to select exactly the right image
* With the selection tool, select only the following, then save the images to the filenames indicated

* NotStarted.bmp : Select the " 0%" on its grey background that is seen before the download starts. The picture should be of a space, then "0%".
The space is so that it cannot detect the 0% in "20%", for example
The gray background on ilivid as I see it is 13 pixels high, and my picture has 3 pixels of white above and below the gray,
and only 1 pixel of gray to the right of the % sign.

* PasteLink.bmp : This is a picture of the words "Paste Link" on a grey background, copied from the button.
There is one grey pixel surrounding the words, except that the "k" in "Link" is taller, and does not have a gray pixel above it.

* Progress.bmp : This is a copy of the word above the percentages column.
The word is in grey characters, and there is a one-pixel white border around it.

Now, you can copy the ahk script into a file in the C:\YTS directory. If you want to use a different one, correct both scripts to suit.

Code: Select all

#Singleinstance force

SetTitleMatchMode 2

; Goto GetYouTube ; Uncomment this line to test the downloader without the scheduler getting in the way

TriggerTime := "04:15"  ; Set this as required

StringSplit,T_,TriggerTime,":",`n
TriggerSecs := T_1 * 3600 + T_2 * 60
NowSecs := SecondsSinceMidnight()
If(TriggerSecs > Nowsecs)  ; later than now, but before midnight, eg. It is now 22:00. Trigger at 23:00
  TimerSecs := TriggerSecs - NowSecs ; Eg. 23 - 22 = 1 hour to wait
Else                       ; TriggerSecs <= NowSecs - trigger at an earlier time than now, so after midnight,  eg. Now = 23:00, trigger at 22:00, 23 hours to wait
  TimerSecs := 86400 - NowSecs + TriggerSecs ;    Eg Now=14:00, Trig=13:00.  TriggerSecs = 24 - 14 + 13 = 23 hours to wait
Millisecs := 0 - TimerSecs * 1000  ; A negative number sets the timer only once
Settimer, GetYouTube, %Millisecs%

msgbox % "At " . TriggerTime . ", The YouTube clips in YT_links.txt will be fetched."
Return

GetYouTube:
Run C:\Documents and Settings\Mike\Local Settings\Application Data\iLivid\iLivid.exe
Winactivate, iLivid
winwaitactive, iLivid
WinMaximize, iLivid
Sleep 1000

Loop, Read, YT_Links.txt
{ clipboard := A_LoopReadLine
  if(WaitImage(X, Y, 25, 65, 165, 180, "PasteLink.bmp", 10) = 1)
    click, %X%, %Y%
  else
    msgbox,,,Paste button not found,2
  sleep 2000
  ; If there are two not yet started, wait.
  ImageSearch, XP, YP, 220, 90, A_ScreenWidth, 300, Progress.bmp
  If(Errorlevel != 0)
    msgbox Did not find Progress
  WaitABit:
  Count := 0
  ImageSearch, XZ, YZ, XP, YP, XP+80, A_ScreenHeight, NotStarted.bmp
  If(ErrorLevel = 0)
  { Mousemove, 1384, 976
    Click
    Count++
    Loop
    { ImageSearch, XZ, YZ, XZ-6, YZ+14, XZ+30, YP+900, NotStarted.bmp
      If(Errorlevel = 0)
        Count++
      Else
        Break
    }
    tooltip Count = %Count%
    If(Count > 1)
      Goto WaitABit
  }
}
ExitApp

SecondsSinceMidnight()  ; returns current time in seconds since midnight
{ FormatTime, Secs, T12, ss  ; read time
  FormatTime, Mins, T12, mm
  FormatTime, Hrs, T12, hh
  Secs += Mins * 60           ; convert to seconds since last midnight
  Secs += Hrs * 3600
  return Secs
}
There are a few parameters to check, and a few things that could trip you up if the 'look' of ilivid is different on your platform from the way it appears on mine (mine is an old Toshiba laptop running XP SL 3, so your screen may well be bigger, which will change the locations of things on the screen).

Below, I am using the line numbers given by my editor (Notepad++)

Firstly, check the file name that the ilivid installer used. Mine is iLivid. Note the upper case L.
Now check lines 23, 24, 25, 26 in the script to be sure they are the same. Maybe there's something I'm not understanding, but I found that they have to be the same, case-sensitive, or it doesn't work.

Lines 7 to 20 simply set the timer.

* On line 7, set the time at which you want to do the YouTube downloads.

Lines 22 to 59 operate ilivid to do the downloads

* In Line 21, set the path and filename to your installation of ilivid (or iLivid)
* In line 27, the file YT_Links.txt is assumed to be in the same directory as the script (in the C:\YTS directory).
This file is created by the #x hotkey in my always-active script (see above). Set the path to be the same in both scripts.

Lines 40 to 57 read the Progress column in search of " 0%". If two are found (that means two files are queued, but have not started their download yet because of the Max Downloads = 2 that we set in iLivid above), then the script waits till one of them starts downloading before it pastes another link in.

If you want to test the script immediately without waiting for the scheduler, un-comment line 5. Don't forget to re-comment it before you schedule any downloads.

I have been using the script (not every night) without any issues for the last year. I have tried it with up to around 20 files, and also with a few long files (complete films). I hope it will be useful to somebody out there.
colmik
Posts: 83
Joined: 11 Mar 2014, 18:21

Re: Wee small hours YouTube scheduler / downloader

11 Mar 2014, 18:59

I forgot to mention a couple of things -

* In iLivid, there is a button to press to delete downloaded videos. I'm not sure what happens if you don't use it - I just always do. It doesn't delete the videos themselves - it just makes iLivid forget that it has downloaded them.

* After downloading, the links are left in the YT_Links.txt file. You will have to clear them out manually, or write a bit more code to do it for you if that's what you want. If you forget, and new links that you choose will be added to the file, and when the scheduler triggers, your previously loaded ones will be loaded again. Actually, I think that is you didn't delete downloaded videos (the point above) that may prevent iLivid from loading them again. You may wish to experiment....
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Wee small hours YouTube scheduler / downloader

11 Mar 2014, 22:24

thanks for your meticulous post. i am an aficionado of YouTube downloaders and this one i shall test and report. :ugeek:

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 121 guests