Downloading PDFs From Weblinks

I would like to use Dynamo to download particular PDFs from web urls.

Is this possible with Dynaweb package?

Via Dynaweb i can find the urls of my sheet sin Dynamo, however I cant find a way to download the files at the URL path.

3 Likes

Thanks,

Do i need to import any special Iron Python libraries into Dynamo to loop through a list of urls, or can the Python node work out of the box?

Cant bring the import module “import requests” into the Dynamo’s python node.

Importing requests module does not work in Dynamo python mode.

Throws error

You have to install that library via pip and use Python3 in Dynamo which is only available in higher versions. I don’t know if you copy folder of requests to Iron Python folder>Lib

Rumour has it that Dynamo doesnt support Python3

hello
a solution with module urllib (include in Ironpython 2.7) this example download this pdf in MyDocument directory

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import urllib
import System

myDocDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

urllib.urlretrieve ("https://thebuildingcoder.typepad.com/files/revit_2014_api_developer_guide.pdf", myDocDir + "\\Revit_2014_Platform_API.pdf")
1 Like

I’m using Dynamo 2.04.
When i use this code block in the python script node, i get this error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “C:\Program Files (x86)\IronPython 2.7\Lib\urllib.py”, line 237, in retrieve
File “”, line 13, in
File “C:\Program Files (x86)\IronPython 2.7\Lib\urllib.py”, line 91, in urlretrieve
File “C:\Program Files (x86)\IronPython 2.7\Lib\urllib.py”, line 209, in open
IOError: [Errno socket error] [Errno errors while performing handshake: ] System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. —> System.ComponentModel.Win32Exception: The function requested is not supported
— End of inner exception stack trace —
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at IronPython.Modules.PythonSocket.ssl.do_handshake()

I have IRON python 2.7.3

it looks like the file you want to download requires a authentication
try this

1 Like

The file i tried to download is the buildingcoder pdf…

Why would it require athentication?

@mscottM6C4C Since I don’t have this error I thought you tried with another file.
I did the test with Dynamo 2.04 and 2.3 I don’t have this error…

you can try this variant

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
import System

myDocDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

webClient = System.Net.WebClient()
webClient.DownloadFile("https://thebuildingcoder.typepad.com/files/revit_2014_api_developer_guide.pdf", myDocDir + "\\Revit_2014_Platform_API_v2.pdf")
2 Likes

It worked for me!

The Building Coder PDF landed in my documents folder.

Can the method accept a list of urls?

I have about 100 urls to download.

@mscottM6C4C See if this helps:
You can define where the downloads should land and use excel to supply the list of links.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
import System
	
links = IN[0]
folderPath = IN[1]
out = []

for link in links:
	fileName = link[link.rfind("/")+1:]
	webClient = System.Net.WebClient()
	webClient.DownloadFile(link, folderPath + "\\" + fileName)
	out.append(fileName)
	
OUT = out
2 Likes

My file names and my links are coming from different places in BIM Track so I’m trying to use a nested loop but i may have done something wrong.

You´d want to loop over the links and file names at the same time (assuming you have as many names as links). Here´s a thorough explanation on the zip() function at realpython.com

Nesting the loop would cause downloading the same file several times with a new name. You could end up with several copies of the last pdf file, just named different - if it did not raise an error for overwriting files.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
import System
	
links = IN[0]
filenames = IN[1]
folderPath = IN[2]
out = []

for link, name in zip(links, filenames):
	webClient = System.Net.WebClient()
	webClient.DownloadFile(link, folderPath + "\\" + name)
	out.append(name)
	
OUT = out
3 Likes

Thanks,

Thats exactly what happened to me.
I was downloading the same file several times.

I didnt know how to loop over the links and file names at the same time.

Thanks very much!

1 Like

You are partially right and incorrect, the latest version of dynamo does include Cpython3 which is based of Python3 ;). Though you have to use dynamo 2.7/2.8 to have access to this.

1 Like