Author Topic: Programatically download the latest 64 bit version of Firefox for Windows.  (Read 7062 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #30 on: October 30, 2018, 10:53:18 PM »
BASIC doesn't get any thinner.  ;)

Script BASIC

Code: Script BASIC
  1. ' Firefox Download Challenge (Script BASIC Version) by JRS.
  2.  
  3. INCLUDE curl.bas
  4.  
  5. ch = curl::init()
  6.  
  7. curl::option(ch,"URL","https://www.mozilla.org/en-US/firefox/new/")
  8. wp = curl::perform(ch)
  9.  
  10. IF wp LIKE """*data-latest-firefox="*" data-esr-versions*<div id="other-platforms">*<li class="os_win64">*<a href="*"*""" THEN
  11.   PRINT "Downloading Latest 64Bit Firefox (",JOKER(2),") for Windows.\n"
  12.   curl::option(ch,"FOLLOWLOCATION", 1)
  13.   curl::option(ch,"NOPROGRESS",0)
  14.   curl::option(ch,"FILE","Firefox_Setup-" & JOKER(2) & ".exe")
  15.   curl::option(ch,"URL", JOKER(6))
  16.   curl::perform(ch)
  17.   PRINTNL
  18.   PRINT "Firefox_Setup-" & JOKER(2) & ".exe Downloaded ",FORMAT("%~##,###,###~ Bytes",curl::info(ch,"SIZE_DOWNLOAD")), _
  19.   " at ",FORMAT("%~##,###,###~ Bytes/Second",curl::info(ch,"SPEED_DOWNLOAD")),".\n"
  20. ELSE
  21.   PRINT "<< ERROR >>\n"
  22. END IF
  23.  
  24. curl::finish(ch)
  25.  


$ time scriba ffdl.sb
Downloading Latest 64Bit Firefox (63.0) for Windows.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   130  100   130    0     0    663      0 --:--:-- --:--:-- --:--:--   663
100 42.3M  100 42.3M    0     0  22.7M      0  0:00:01  0:00:01 --:--:-- 35.5M

Firefox_Setup-63.0.exe Downloaded 44,400,072 Bytes at 23,883,847 Bytes/Second.

real   0m2.174s
user   0m1.135s
sys   0m0.271s
$
« Last Edit: October 31, 2018, 02:00:11 AM by John »

Offline erosolmi

  • BASIC Developer
  • Posts: 4
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #31 on: October 30, 2018, 11:10:18 PM »
Example in thinBasic.
Attached source code and executable

Ciao
Eros

Code: thinBasic
  1. uses "Console"
  2. uses "iNet"
  3. uses "OS"
  4.  
  5. printl "Connect to Firefox web site, determine latest Firefox version and download it"
  6.  
  7. '---Check if computer is connected to internet
  8. if INET_GetState Then
  9.   '---Get Firefox web site page
  10.   string Firefox_Info_Page = INET_UrlGetString("https://www.mozilla.org/en-US/firefox/new/")
  11.  
  12.   '---Get Firefox available version
  13.   string Firefox_Available_Version = grab$(Firefox_Info_Page, "data-latest-firefox=""", """")
  14.  
  15.   '---Get Win64 direct download url
  16.   string Firefox_DownLoad_Url_Win64 = grab$(Firefox_Info_Page, "<div id=""other-platforms"">", "</div>")
  17.   Firefox_DownLoad_Url_Win64 = grab$(Firefox_DownLoad_Url_Win64, "<li class=""os_win64"">", "</li>")
  18.   Firefox_DownLoad_Url_Win64 = grab$(Firefox_DownLoad_Url_Win64, "<a href=""", """")
  19.  
  20.   printl "Firefox available version...:", Firefox_Available_Version
  21.   printl "Firefox Download url........:", Firefox_DownLoad_Url_Win64
  22.  
  23.   '---Download
  24.   string sLocalFile = APP_SourcePath + "Firefox Setup " + Firefox_Available_Version + ".exe"
  25.   printl "Downlaoding to", sLocalFile, "..."
  26.   INET_UrlDownload(Firefox_DownLoad_Url_Win64, sLocalFile)
  27.   printl "File downlaoded."
  28.   printl
  29.   printl "Press ESC to end or any other key within 10 secs to execute setup"
  30.  
  31.   '---Ask if execute setup
  32.   string sUserReply = WaitKey(10)
  33.   if sUserReply <> "[ESC]" and sUserReply <> "[TIMEOUT]" Then
  34.     printl "Executing " + sLocalFile
  35.     OS_Shell(sLocalFile, %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC)
  36.   Else
  37.     printl "No execution. Execute manually if needed."
  38.   end If
  39.  
  40. Else
  41.   printl "! it seems you are not connected to internet." in %CCOLOR_FLIGHTRED
  42. end If
  43.  
  44. printl "---All done, press a key to end or wait 5 secs---"
  45. WaitKey(5)
  46.  
  47. '[todo] To add async download with any progress info ...
  48. '       Install thinBasic and see example at \thinBasic\SampleScripts\INet\Internet_FileDownload.tbasic
  49.  

« Last Edit: October 31, 2018, 12:33:43 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #32 on: October 30, 2018, 11:24:03 PM »
Quote
BASIC doesn't get any thinner.

It worked and brought the real thinBasic to us.  ;D

Thanks for your contribution!

I changed your code block from. =SB to =thinbasic. I normally don't edit others posts but TB highlighting looks much better. I attached the geshi syntax highlighting file for thinBasic I'm using here on the forum. It might be pretty old. If you want to have a peek and post an updated copy that would be great.
« Last Edit: October 31, 2018, 01:43:17 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #33 on: October 31, 2018, 01:49:30 AM »
BASIC doesn't get any thinner.  ;)

Script BASIC

Pretty Tight, John!

AIR.

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #34 on: October 31, 2018, 01:50:46 AM »
Example in thinBasic.
Attached source code and executable

Ciao
Eros


Very Nice, Eros!!!

AIR.

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #35 on: October 31, 2018, 12:05:14 PM »
My PYTHON version, using only modules that come with a standard installation...

Code: Python
  1. #!/usr/bin/env python
  2.  
  3. import urllib2,re,sys
  4.  
  5. html = urllib2.urlopen('https://mozilla.org/firefox/new').read()
  6.  
  7. version = re.findall('data-latest-firefox="(\d+\.\d+)',html)[0]
  8. download_link = re.findall('.+href="(.+latest-ssl.+os=win64.+US).+',html)[0]
  9.  
  10. sys.stdout.write ("\33[2J\33[HFirefox Download Challenge (Python Version) by AIR.\n\n")
  11. print "Downloading Latest 64Bit Firefox (" + version + ") for Windows.\n"
  12.  
  13. f = urllib2.urlopen(download_link)
  14. with open("Firefox Setup " + version + ".exe", "wb") as dl:
  15.     total_size = int(f.info().getheader('Content-Length').strip())
  16.     downloaded = 0
  17.  
  18.     while True:
  19.         buffer = f.read(8192)
  20.         if not buffer:
  21.             print
  22.             break
  23.         downloaded += len(buffer)
  24.         dl.write(buffer)
  25.         percent = round( (float(downloaded) / total_size)*100,2)
  26.         sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % (downloaded, total_size, percent))
  27.     print "\nDownload Complete.\n"


Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #36 on: October 31, 2018, 01:44:22 PM »
So far SB followed by Bash are my leading challenge contenders.

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #37 on: October 31, 2018, 01:57:37 PM »
That's cool, but remember this is not a competition between languages.


Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #38 on: October 31, 2018, 03:30:34 PM »
I agree and it's a great way to see how different languages approach a common task.



Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #39 on: October 31, 2018, 08:07:51 PM »
OBJECTIVE C

I wanted to show this, because I think most people who don't use ObjC realize how easy it is to incorporate C code.

Code: Objective-C
  1. /*
  2.         Download the latest Firefox 64bit for Windows, Objective C version
  3.        
  4.         Written by AIR.
  5.  
  6.         This shows how Objective C can leverage C and C libraries
  7.         within the same project.
  8.  
  9.         compile with: gcc -framework Foundation getFirefox.m -lcurl -O2 -o getFirefox
  10. */
  11.  
  12. #import <Foundation/Foundation.h>
  13. #import <curl/curl.h>
  14.  
  15. NSString *version;
  16.  
  17. int progress_callback(id clientp, double dltotal, double dlnow, double ultotal, double ulnow ) {
  18.     printf("Downloaded %d of %d bytes\r",(int)dlnow,(int)dltotal);
  19.         return 0;
  20. }
  21.  
  22. void downloadFile(NSString *url, NSString *downloadFileName) {
  23.     id handle;
  24.     int success;
  25.         FILE *fp;
  26.  
  27.         printf("Downloading Latest 64Bit Firefox (%s) for Windows.\n\n",version.UTF8String);
  28.  
  29.     handle = curl_easy_init();    
  30.         if ((fp = fopen(downloadFileName.UTF8String, "wb+"))) {
  31.                 curl_easy_setopt(handle, CURLOPT_URL, url.UTF8String);
  32.                 curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION,1);
  33.                 curl_easy_setopt(handle, CURLOPT_WRITEDATA, fp);
  34.                 curl_easy_setopt(handle, CURLOPT_NOPROGRESS,0);
  35.                 curl_easy_setopt(handle, CURLOPT_PROGRESSDATA,downloadFileName.UTF8String);
  36.                 curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION,progress_callback);
  37.        
  38.                 success = curl_easy_perform(handle);
  39.                 fclose(fp);
  40.         }
  41.     curl_easy_cleanup(handle);
  42.     printf("\n\nDownload of '%s' Complete.\n\n", downloadFileName.UTF8String);
  43. }
  44.  
  45. NSString *regex(NSString *src, NSString *query) {
  46.         NSRegularExpression *regex;
  47.         NSString *result;
  48.         NSArray *matches;
  49.  
  50.         regex = [NSRegularExpression regularExpressionWithPattern:query options:NSRegularExpressionCaseInsensitive error:nil];
  51.         matches = [regex matchesInString:src options:0 range:NSMakeRange(0, [src length])];
  52.         result = [src substringWithRange:[(NSTextCheckingResult*)matches[0] rangeAtIndex:1]];
  53.  
  54.         return result;
  55. }
  56.  
  57.  
  58.  
  59. int main(int argc, char const *argv[]) {
  60.         NSURL *url;
  61.         NSString *htmlContent, *downloadLink;
  62.  
  63.         printf("\33[2J\33[HFirefox Download Challenge (Objective C Version) by AIR.\n\n");
  64.  
  65.         url = [NSURL URLWithString:@"https://mozilla.org/firefox/new"];
  66.  
  67.         htmlContent = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
  68.  
  69.         version = regex(htmlContent,@"data-latest-firefox=.(\\d+\\.\\d+)");
  70.         downloadLink = regex(htmlContent,@".+href=.(.+latest-ssl.+win64.+US).+");
  71.  
  72.         downloadFile(downloadLink,[NSString stringWithFormat:@"Firefox Setup %@.exe",version]);
  73.         return 0;
  74. }
  75.  
  76.  

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #40 on: October 31, 2018, 08:13:16 PM »
These code examples make me appreciate high level languages like BASIC even more.

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #41 on: October 31, 2018, 08:27:53 PM »
What I like about examples like these is that they can lead to asking "Why didn't I think of that?"

Eros' use of "GRAB$" made me take another look at the Bacon manual.  It's called "INBETWEEN$" in Bacon and works the same (I rewrote my submission for Bacon to test that.  Haven't submitted it though).

It also got me thinking about how to implement something like that myself.  After a little research I was able to put something together in C++ that works. Maybe I'll dust off JADE and see what happens.... ;D

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #42 on: October 31, 2018, 08:37:40 PM »
Quote
Maybe I'll dust off JADE and see what happens.... ;D

I would love to see that lost gem resurface.

The SB final version is due to your experimentation with cURL in other languages. Thanks!

40% of the visitors to the forum use Firefox. (dominant browser)
« Last Edit: October 31, 2018, 08:59:21 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #43 on: November 01, 2018, 01:38:07 AM »
Quote
Maybe I'll dust off JADE and see what happens.... ;D

I would love to see that lost gem resurface.

JADE VERSION

Code: C++
  1. #include "jade.h"
  2.  
  3. /* compile with: g++ -std=c++11 getFirefox.cpp -DUSE_CURL -lcurl -O2 -o getFirefox */
  4.  
  5. /* Clone my repository for the JADE system itself: git clone https://bitbucket.org/Airr/jade.git */
  6.  
  7. MAIN
  8.     DEF CSTRING HTML$,VERSION$,DL_LINK$, URL$("https://mozilla.org/firefox/new");
  9.  
  10.     CLS;
  11.     PRINT("Firefox Download Challenge (**JADE** Version) by AIR.");
  12.     PRINTNL;
  13.  
  14.     HTML$ = downloadPage(URL$);
  15.  
  16.     VERSION$ = REGEX(HTML$, "data-latest-firefox=.(\\d+\\.\\d+).");
  17.     DL_LINK$ = REGEX(HTML$, ".+href=.(.+latest-ssl.+win64.+US).+");
  18.  
  19.     PRINT("Downloading Latest 64Bit Firefox ("+VERSION$+") for Windows.");
  20.     PRINTNL;
  21.  
  22.     downloadFile(DL_LINK$,"Firefox Setup " + VERSION$ + ".exe");
  23. ENDMAIN
  24.  

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2741
    • ScriptBasic Open Source Project
Re: Programatically download the latest 64 bit version of Firefox for Windows.
« Reply #44 on: November 01, 2018, 02:13:58 AM »
Nice! (cURL pre-processor defines)

JADE has captured the number 2 spot on my list.

« Last Edit: November 01, 2018, 02:25:29 AM by John »