Download YouTube subtitles using excel vba - excel

I have the following code that download xml file with the subtitle of a video from YouTube
Sub Test()
Dim http As Object
Dim oStream As Object
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "http://video.google.com/timedtext?lang=en&v=qANA6POtuFo", False
http.send
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write http.responseBody
oStream.SaveToFile ThisWorkbook.Path & "\Sample.xml", 2
oStream.Close
End Sub
But it doesn't work for other videos
for example I tried this link
v=4Z3EJrh7_5k
Any idea how to do the download with any video with a subtitle?

As far as I researched, you cannot download from 4Z3EJrh7_5k because it's subtitle is not a file someone uploaded, but it is generated automatically.
To see if a video has any subtitle file, you can use http://video.google.com/timedtext?type=list&v=qANA6POtuFo, and it will list every file with languages inserted in the video. You can note that it shows only one, even if you go to the video and click, there are two (English and English (Automatically generated))

Related

VBA Downloaded Zip file from OneDrive corrupted/cannot be opened

I want to download a zip file from OneDrive using VBA.(UserData folder zipped as UserData.zip and uploaded to the OneDrive location; size: 200KB) For that, I used the following VBA script.(sensitive data altered in the code)
Sub DownloadFile()
'Declare the Object and URL
Dim myURL As String
Dim WinHttpReq As Object
'Assign the URL and Object to Variables
myURL = "https://MYCOMPANYNAME-my.sharepoint.com/personal/MYUSERNAME_MYCOMPANYNAME_com/Documents/FOLDERNAME/UsersData.zip"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
'Provide Access Token and PWD to the URL for getting the service from API
WinHttpReq.Open "GET", myURL, False, "abcdef", "12345"
WinHttpReq.send
Debug.Print WinHttpReq.Status
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "C:\Users\MYUSERNAME\Downloads\UsersData.zip", 2
oStream.Close
MsgBox "File downloaded"
Else
MsgBox "Download failed"
End If
End Sub
When I run it, it downloads the zip file and gives me the "File downloaded" message as well. But when I try to open the downloaded file, unzipping application fails to do that and it seems like the file is corrupted.
This works fine for pdf, jpg, txt and other similar files. Why this happens only in zip files?
What is the issue in my code and is there any other effective ways in VBA to download zip files from OneDrive?

Downloaded file with Excel VBA is corrupted

My Excel VBA macro sends a POST multipart/form-data request to an API and receives a application/pdf file as a response.
This is the relevant part:
Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.Open "POST", Uri, False
xhr.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & strBoundary
xhr.setRequestHeader "X-Api-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
xhr.send (strBody)
If xhr.Status Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write xhr.responseBody
oStream.SaveToFile outputFileName, 2
oStream.Close
End If
Set xhr = Nothing
The downloaded PDF document is unreadable by any PDF reader. I checked its contents and it is almost the same as the downloaded document from other clients (eg Postman, cURL), which are valid PDF documents (ie can be open in PDF readers) [Edited].
For example, when opening the PDF documents in VS Code:
The VBA macro downloaded file:
Compare it to the PDF downloaded with Postman (Check the last line in the picture):
If it was a text file, it would seem as an encoding problem, but as I am using the ADODB.Stream with Type = 1 that should handle binary files well.
What am I missing?

Not able to open downloaded Excel file from Sharepoint 2013 using Excel VBA

i have tried the following code to download a single Excel file from Sharepoint 2013.
Option Explicit
Sub TxtStream()
Dim myURL As String, DestFile As String, myHeader As String
Dim Usr as string, Pwd as string
Dim oStream As Object
Usr="": Pwd=""
myURL = "https://Server.Name/teams/Forms/AllItems.aspx/MasterFile.xlsx"
DestFile = "C:\Test.xlsx"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("MSXML2.XMLHTTP")
'WinHttpReq.Open "HEAD", myURL, False, Usr, Pwd
'WinHttpReq.Send
'myHeader = WinHttpReq.getAllResponseHeaders()
'Debug.Print myHeader
'myHeader = WinHttpReq.getResponseHeader("Content-Disposition")
'Debug.Print myHeader
'myHeader = WinHttpReq.getResponseHeader("Content-Type")
'Debug.Print myHeader
WinHttpReq.Open "GET", myURL, False, Usr, Pwd
WinHttpReq.setRequestHeader "content-type", "application/octet-stream"
WinHttpReq.Send
myURL = WinHttpReq.responsebody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 1
oStream.Open
oStream.Position = 0
oStream.Write WinHttpReq.responsebody
oStream.SaveToFile DestFile, 2
oStream.Close
End If
End Sub
It downloads ok, but when i try to open it in Excel 2010, it shoots an error:
Excel cannot open the file Test.xlsx because the file format or file extension is not valid. Verify the file has not been corrupted and the file extension matches the format of the file.
I have checked the Content-Type and it shows as Text/Html;UTF-8. It doesn't show Content-Disposition.
Can someone help as to why the file is not opening?
This is not a full fledged answer, but more like a comment / suggestion as I do not have enough reputation to post comments.
Setting a content-type header in your GET query does not seems to make any difference. We need to check the WinHttpReq.getResponseHeader("Content-Type") sent by the server which in this case seems to be Text/Html;UTF-8.
If you copy paste the URL used in myURL variable to your browser, do you get the "file save as" dialog box?
Better analysis of your requests and responses can be done if you use Fiddler Web Debugger where you can create requests in teh composer and check the responses to finetune your requirements and to see whether any other headers (like cookies, authentications etc) are also required for the file to be downloaded. In this case, you might be getting back a login page (html) because of some authentication issues and not the excel file.
Open the DestFile saved with Notepad.exe to see whether it is actualy an XLSX file (starting with characters PK) or an HTML or other text file.

How to get file extension using Microsoft XMLHTTP using VBA

I'm using the following simple code to download and save some files from the internet. The server that I'm connecting to sometimes generates excel files and the other times text files, I would like to know if theres any possible way to figure out whats the file extension so it can be downloaded with the proper extension? right now I save all of the files as txt files and if the file returns excel files then the downloaded file will be useless (as it is saved as a txt file and I have to change the extension manually). Thanks guys
Dim myURL As String
Dim WinHttpReq As Object
Dim FP As String
myURL = "SAMPLE URL"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile (FP & "\" & C1 & "-" & C2 & ".txt"), 2
oStream.Close
End If
End Sub
Before WinHttpReq.Open "GET", myUrl, false you can try to open the header and look at the mime type of your file before downloading it:
WinHttpReq.Open "HEAD", myUrl, false
WinHttpReq.Send
MyHeader = WinHttpReq.getAllResponseHeaders()
' Scan MyHeader here to find your file mime type ...
Thanks to kiks73 I find my way to deal with this issue, However I wanted to add a little part to it, using
MyHeader = WinHttpReq.getAllResponseHeaders()
will give you something like
"Server: Microsoft-IIS/5.0
Date: Tue, 29 Jul 2014 18:05:44 GMT
X-Powered-By: ASP.NET
Content-Disposition: FileName=11120166.Txt
Content-Type: application/octet-stream
Content-Length: 152
Content:
"
which might be a bit hard to read and analyze so I found the alternative way of getting only the file name and extension:
MyHeader = WinHttpReq.getResponseHeader("Content-Disposition")
this way you ditch all the junk and get only what youre looking for. HOWEVER: in my case I have to download between 300 to 8000 files at once (all of the same type, but the type may change from one session to another one) and trying to pull extension each time seems absurd, and it make my code crash, so you may wanna pull the extension only once.
Thanks again kiks73 for helping me out

Download google finance file from internet using VBA

I am doing a VBA module in which I want to download the csv file from google finance.
Here is a sample URL
www.google.com/finance/historical?q=LON%3AESSR&ei=dEfGU_ioD4iKwAO3-4HQDg&output=csv
Once I put this URL in browser I get a csv file downloaded. But When I use the "WinHttpReq" object to download the file, I am not getting the csv file. Instead I am getting some HTML.
Is there anyway for me to download this CSV file using the URL in VBA?
Thanks,
Arun.
I am not familiar with WINHTTP but I seem to get this working fine - with XMLHTTP. Just enter your file path & name where commented
Sub getCSV()
Dim xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.Open "GET", "http://www.google.com/finance/historical?q=LON%3AESSR&ei=dEfGU_ioD4iKwAO3-4HQDg&output=csv", False
xhr.send
With xhr
If .Status = 200 And .ReadyState = 4 Then
myfile = FreeFile
'HERE
Open "FILE_PATH_&_NAME.csv" For Output As #myfile
Print #myfile, .responseText
Close #myfile
Set xhr = Nothing
End If
End With
End Sub

Resources