Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the broken code + refactoring #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pdfs/
32 changes: 16 additions & 16 deletions download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import time
import requests

# encoding=utf8
import sys
# encoding=utf8
import sys
try:
reload(sys)
except NameError:
Expand All @@ -28,15 +28,15 @@ def download_pdf(link, location, name):
f.write(response.content)
f.close()
except HTTPError:
print('>>> Error 404: cannot be downloaded!\n')
raise
print('>>> Error 404: cannot be downloaded!\n')
raise
except socket.timeout:
print(" ".join(("can't download", link, "due to connection timeout!")) )
raise

def clean_pdf_link(link):
if 'arxiv' in link:
link = link.replace('abs', 'pdf')
link = link.replace('abs', 'pdf')
if not(link.endswith('.pdf')):
link = '.'.join((link, 'pdf'))

Expand All @@ -46,35 +46,35 @@ def clean_pdf_link(link):
def clean_text(text, replacements = {':': '_', ' ': '_', '/': '_', '.': '', '"': ''}):
for key, rep in replacements.items():
text = text.replace(key, rep)
return text
return text

def print_title(title, pattern = "-"):
print('\n'.join(("", title, pattern * len(title))))
print('\n'.join(("", title, pattern * len(title))))

def get_extension(link):
extension = os.path.splitext(link)[1][1:]
if extension in ['pdf', 'html']:
return extension
if 'pdf' in extension:
return 'pdf'
return 'pdf'
return 'pdf'
return 'pdf'

def shorten_title(title):
m1 = re.search('[[0-9]*]', title)
m2 = re.search('".*"', title)
if m1:
title = m1.group(0)
if m2:
title = ' '.join((title, m2.group(0)))
return title[:50] + ' [...]'
title = ' '.join((title, m2.group(0)))
return title[:50] + ' [...]'


if __name__ == '__main__':

parser = argparse.ArgumentParser(description = 'Download all the PDF/HTML links into README.md')
parser.add_argument('-d', action="store", dest="directory")
parser.add_argument('--no-html', action="store_true", dest="nohtml", default = False)
parser.add_argument('--overwrite', action="store_true", default = False)
parser.add_argument('--overwrite', action="store_true", default = False)
results = parser.parse_args()

output_directory = 'pdfs' if results.directory is None else results.directory
Expand All @@ -84,7 +84,7 @@ def shorten_title(title):
if results.overwrite and os.path.exists(output_directory):
shutil.rmtree(output_directory)

with open('README.md',encoding='utf8) as readme:
with open('README.md',encoding='utf8') as readme:
readme_html = mistune.markdown(readme.read())
readme_soup = BeautifulSoup.BeautifulSoup(readme_html, "html.parser")

Expand All @@ -98,7 +98,7 @@ def shorten_title(title):
h1_directory = os.path.join(output_directory, clean_text(point.text))
current_directory = h1_directory
elif point.name == 'h2':
current_directory = os.path.join(h1_directory, clean_text(point.text))
current_directory = os.path.join(h1_directory, clean_text(point.text))
if not os.path.exists(current_directory):
os.makedirs(current_directory)
print_title(point.text)
Expand All @@ -125,8 +125,8 @@ def shorten_title(title):
break
except:
failures.append(point.text)
point = point.next_sibling

point = point.next_sibling

print('Done!')
if failures:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mistune>=0.7.2
beautifulsoup4>=4.4.1
six>=1.10.0
requests>=2.25