Youtube Video Searcher/Downloader

By sunslayer on Jan 21, 2010

This will search for a youtube video and then download it.
right click anywhere or type /yvs
rest is pretty self explanatory
Image

on *:load:if (!$isdir(YouTube Videos)) mkdir "YouTube Videos"
alias yvs { dialog $iif($dialog(yvs),-v,-md) yvs yvs }
dialog -l yvs {
  title "Youtube Video Searcher"
  size -1 -1 157 44
  option dbu
  box "Search", 1, 0 0 156 48
  edit "", 2, 8 9 140 10
  button "Search", 3, 63 21 36 12
  text "Click Search to Search www.youtube.com", 4, 0 35 156 8, center
  box "Results", 5, 0 48 156 124
  list 6, 4 57 148 111, size
  box "Video Search Info", 7, 0 172 156 64
  text "Name:", 8, 6 182 18 8
  edit "", 9, 30 181 122 10, read autohs
  text "Length:", 10, 6 194 21 8
  edit "", 11, 29 193 44 10, read autohs
  text "Rating:", 12, 78 194 19 8
  edit "", 13, 101 193 50 10, read autohs
  text "Date Added:", 14, 6 205 32 8
  edit "", 15, 44 204 107 10, read autohs
  text "Author:", 16, 78 215 21 8
  edit "", 17, 101 214 50 10, read autohs
  link "", 18, 23 225 109 9
  text "Total Views:", 19, 6 215 30 8
  edit "", 20, 39 214 35 10, read autohs
}
on *:dialog:yvs:*:*: {
  if ($devent == init && !$isdir($mircdirYoutube Videos)) { mkdir "Youtube Videos" }
  elseif ($devent == sclick) {
    if ($did == 3 && $did(2) && !$sock(yvs.yt)) {
      dialog -s yvs -1 -1 314 344
      did -ra yvs 4 Searching www.youtube.com
      did -r yvs 6
      did -ra YVS 5 Results
      if ($hget(YVS)) .hfree YVS
      if ($hget(YTS)) .hfree YTS
      sockopen yvs.yt youtube.com 80
      sockmark yvs.yt $replace($did(2),$chr(32),+)
    }
    elseif ($did == 6) {
      dialog -s yvs -1 -1 314 472
      var %a = 11 18 20 9 17 15 13,%b = 1
      did -ra YVS $replace(%a,$chr(32),$chr(44))
      while ($gettok(%a,%b,32)) {
        did -ra YVS $v1 $iif($gettok($hget(YVS,$did(6).sel),%b,9),$v1,Not Found.)
        inc %b
      }
    }
    elseif ($did == 18) { url -an $did(18) }
  }
  elseif ($devent == dclick && $did(6).sel) {
    yt.s $gettok($hget(YVS,$did(6).sel),2,9)
    .hadd -m YTS Name $did(6).seltext
  }
}
on *:sockopen:yvs.yt: {
  sockwrite -nt $sockname GET $+(/results?search_query=,$sock($sockname).mark,&search_type=&aq=f) HTTP/1.1
  sockwrite -n $sockname HOST: youtube.com $+ $crlf $+ $crlf
}
on *:sockread:yvs.yt: {
  var %yt | sockread %yt
  if (No videos found for isin %yt) {
    sockclose $sockname
    did -ra YVS 4 No Videos Found.
    dialog -s YVS -1 -1 314 96
  }
  elseif ($regex(%yt,/<a class="video-thumb-120" href="(.*?)".*><img title="(.*?)"/i)) {
    did -a YVS 6 $regml(2)
    did -ra YVS 5 Results( $+ $hget(YVS,total) $+ )
    .hinc -m YVS total
    .hadd -m YVS $hget(YVS,total) $+(www.youtube.com,$regml(1),$chr(9),$regml(2))
  }
  elseif ($regex(%yt,/<span id="video-run-time-.*" class="video-time"><span>(.*?)</span></span></a>/i)) {
    .hadd YVS $hget(YVS,total) $instok($hget(YVS,$hget(YVS,total)),$regml(1),1,9)
  }
  elseif (($regex(%yt,/<button class="master-sprite ratingVS ratingVS-.*" title="(.*)"></button>/i) || no rating isin %yt)) {
    .hadd YVS $hget(YVS,total) $addtok($hget(YVS,$hget(YVS,total)),$iif($regml(1),$v1,No Rating),9)
  }
  elseif ($regex(%yt,/<span id="video-num-views-.*" class="video-view-count">(.*?) views</span>/i)) {
    .hadd YVS $hget(YVS,total) $instok($hget(YVS,$hget(YVS,total)),$regml(1),3,9)
  }
  elseif ($regex(%yt,/<span id="video-added-time-.+" class="video-date-added">(.*)</span>/i)) {
    .hadd YVS $hget(YVS,total) $instok($hget(YVS,$hget(YVS,total)),$regml(1),4,9)
  }
  elseif ($regex(%yt,/<span class="video-username"><a id="video-from-username-.* href="/user/.*">(.*)</a></span>/i)) {
    .hadd YVS $hget(YVS,total) $instok($hget(YVS,$hget(YVS,total)),$regml(1),5,9)
  }
}
on *:sockclose:yvs.yt:did -ra YVS 4 Search complete, Double click on a video to download.
dialog -l yt.s {
  title "YouTube Video Download Links"
  size -1 -1 134 29
  option dbu
  text "Links found", 1, 0 4 134 8, center
  link "High Definition", 2, 10 17 38 8
  link "Low Definition", 3, 90 17 40 8
}
alias yt.s {
  if ($sock(yt.s)) .sockclose yt.s
  if ($hget(yts)) .hfree yts
  sockopen yt.s downloader9.com 80
  sockmark yt.s $1
}
on *:sockopen:yt.s: {
  sockwrite -n $sockname GET /?url= $+ $sock($sockname).mark HTTP/1.1
  sockwrite -n $sockname HOST: downloader9.com
  sockwrite -n $sockname $crlf
}
on *:sockread:yt.s: {
  var %yt | sockread %yt
  if ($regex(%yt,/<a href="http://(.*)">Download</a> video.(flv|mp4) - (Low|high) Quality<br />/i)) { hadd -m yts $regml(3) $regml(1) }
}
on *:sockclose:yt.s:dialog -mdo yt.s yt.s
on *:dialog:yt.s:sclick:2,3: {
  var %a = $gettok($did($did),1,32),%b = $iif(%a == High,.mp4,.flv),%c = $hget(YTS,%a)
  .hadd -m YTS ftype %b
  if ($isfile($ytd.loc($hget(YTS,Name)))) {
    .noop $tip(YTS,Video already Exists!,Video already exists!) 
    halt
  }
  elseif (%c) {
    yt.dl %c
    dialog -x $dname
    .noop $tip(YTS.BEGIN,Video Downloading,%a Quality of $qt($hget(YTS,name)) is now being downloaded.)
  }
  else .noop $tip(YTS.err,Video ERROR!,%a Quality version is unavailable.)
}
alias yt.dl {
  if ($sock(yt.dl)) { .noop $input(Video already downloading!,o) | halt }
  .sockopen yt.dl $gettok($1,1,47) 80
  .sockmark yt.dl $+($gettok($1,1,47),|,/,$gettok($1,2,47))
}
alias ytd.loc return $qt($+($mircdirYouTube Videos\,$1,$hget(YTS,ftype)))
on *:sockopen:yt.dl: {
  sockwrite -nt $sockname GET $gettok($sock($sockname).mark,2,124) HTTP/1.0
  sockwrite -nt $sockname HOST: $gettok($sock($sockname).mark,1,124)
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; WindowsNT 6.0; en-US; rv:1.8.1.13)
  sockwrite -n $sockname Connection: Keep-Alive
  sockwrite -n $sockname $crlf
  sockmark $sockname start
}
on *:sockread:yt.dl: {
  var %yt,%hash = YTS. $+ $sockname
  if ($sock($sockname).mark == start) {
    while ($sock($sockname).rq) {
      sockread %yt
      if (!%yt) { 
        sockmark $sockname read
        .hadd -m %hash time $ctime
        break
      }
      elseif ($regex(%yt,/^Content-Type: text/html/i)) { echo $color(info) -a * Error: Bad/broken file | sockclose $sockname }
      elseif ($regex(%yt,/^Content-length: (\d*)/i)) .hadd -m %hash Size $regml(1)
    }
  }
  elseif ($hget(%hash,Size)) {
    var %fname = $YTD.loc($hget(YTS,Name)),%size = $hget(%hash,size),%echo = echo $iif($me ison $active,$active,$lactive)
    var %win = $+(@Youtube,$chr(160),Video,$chr(160),Downloader.,$hget(%hash,Time))
    if (!$window(%win)) window -Cdk0p %win -1 -1 300 60
    while ($sock($sockname).rq) {
      sockread &YTD
      bwrite $mircdir $+ %fname -1 -1 &YTD
      clear %win
      drawtext %win 14 "Times New Roman" 12 5 5 Downloading Video: $+($bytes($file(%fname).size,mb),/,$bytes(%size,mb),MB,$chr(40),$round($calc($file(%fname).size / %size *100),2),%,$chr(41))
    }
    if ($file(%fname).size == %size) {
      window -c %win
      if ($appactive) { %echo Download Complete, Download took: $calc($ctime - $hget(%hash,time)) Seconds. }
      else { .noop $tip(YTD,Youtube Video Downloader,Download Complete. $crlf Download took: $round($calc(($ctime - $hget(%hash,time)) /60),2) minutes.) }
      .hfree %hash
      .hfree YTS
    }
  }
}
menu * {
  Youtube Video Searcher/Downloader
  .Open Dialog:yvs
  .Open Directory:run $qt($mircdirYoutube Videos)
}

Comments

Sign in to comment.
sunslayer   -  Jan 25, 2010

you looked far further into it then i did, lol :p

 Respond  
FordLawnmower   -  Jan 25, 2010

Well the Downloader9 gives you decent speed. I get about 90Kbps from it.
Youtube is constantly changing their pages to break peoples downloaders so I think the 3rd party site is a great idea. When the other Youtube scripts are broken, your's should still work. I'm sure Downloader9 will keep pace with Youtube changes, as their whole site depends on it.

 Respond  
sunslayer   -  Jan 25, 2010

thanks FordLawnmower

Just curious, why did you use the 3rd party site for the download?i had come acrossed Downloader9 and had never actually thought of doing it directly

 Respond  
FordLawnmower   -  Jan 25, 2010

Excellent script sunslayer :) Very few people would even attempt something like this.
Just curious, why did you use the 3rd party site for the download?
I have a couple scripts that download from youtube and they are still able to get the file directly from the site.
I never posted any of them because kirby had so many, but I guess his are all broken now.

Good work man :) It's great to see someone taking on something as challenging as this :)

 Respond  
sunslayer   -  Jan 23, 2010

updated the code
all previous bugs are fixed and stopped the annoying window from reappearing

 Respond  
SnoooP   -  Jan 22, 2010

Ok cool, yeah the one which shows the percentage, slightly annoying but a work in progress, still as i said earlier i really like the idea,

 Respond  
sunslayer   -  Jan 22, 2010

Wouldn't download the video thoughthats because not every video has a high/low quality option.(fixed)
then it wouldn't allow me to close the dialog box.
which one?> and when the box was open i wasn't able to click on anything else what so ever..do you mean after u clicked the download link? if the window showing the download percentage appeared you might not be able to click because the window is constantly being edited causing it to go back on top of mIRC, atm this is the best thing i have to show download progress. im prolly gonna add another dialog that allows multiple downloads at the same time

 Respond  
SnoooP   -  Jan 22, 2010

I loaded it seemed to be ok.
it searchd youtube for the video I wanted fine.

Wouldn't download the video though, then it wouldn't allow me to close the dialog box. and when the box was open i wasn't able to click on anything else what so ever..

I think they may just be errors you could possibly fix if you wanted to, untill then though I wont rate it.

 Respond  
sunslayer   -  Jan 21, 2010

thanks peepz

 Respond  
TheNitelyfe   -  Jan 21, 2010

hot

 Respond  
SnoooP   -  Jan 21, 2010

Nice script, i'll give it a thorough test tomorow, im kinda fucked now lol... I like the downloader though, be interesting to see how that is

 Respond  
jsg55   -  Jan 21, 2010

looks cool sunslayer, i havent tried downloading anything yet but so far its working good, really convenient too :)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.