Skip to content

Commit 85ee241

Browse files
author
Flood Sung
authored
Merge pull request floodsung#48 from clouds56/master
Add 2&3 compatibility and improve keyboard interrupt behaviour
2 parents 109889d + 134b83a commit 85ee241

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

download.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1+
from __future__ import print_function
12
import os
23
import re
3-
import urllib2
4+
from six.moves.urllib.request import urlopen
5+
from six.moves.urllib.error import HTTPError
46
import shutil
57
import argparse
68
import mistune
79
import bs4 as BeautifulSoup
10+
import socket
11+
import time
812

913
def download_pdf(link, location, name):
1014
try:
11-
response = urllib2.urlopen(link, timeout=500)
15+
response = urlopen(link, timeout=500)
1216
file = open(os.path.join(location, name), 'w')
1317
file.write(response.read())
1418
file.close()
15-
except urllib2.HTTPError:
19+
except HTTPError:
1620
print('>>> Error 404: cannot be downloaded!\n')
1721
raise
1822
except socket.timeout:
1923
print(" ".join(("can't download", link, "due to connection timeout!")) )
24+
raise
2025

2126
def clean_pdf_link(link):
2227
if 'arxiv' in link:
@@ -97,6 +102,13 @@ def shorten_title(title):
97102
fullname = '.'.join((name, ext))
98103
if not os.path.exists('/'.join((current_directory, fullname)) ):
99104
download_pdf(link, current_directory, '.'.join((name, ext)))
105+
except KeyboardInterrupt:
106+
try:
107+
print("Press Ctrl-C in 1 second to quit")
108+
time.sleep(1)
109+
except KeyboardInterrupt:
110+
print("Cancelling..")
111+
break
100112
except:
101113
failures.append(point.text)
102114

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mistune>=0.7.2
2-
beautifulsoup4>=4.4.1
2+
beautifulsoup4>=4.4.1
3+
six>=1.10.0

0 commit comments

Comments
 (0)