Last active
November 7, 2016 00:04
-
-
Save queeup/16d8a347b14cf7ccbfb5d03acea8d2dc to your computer and use it in GitHub Desktop.
MagPi issue downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import shutil | |
import requests | |
import subprocess | |
import re | |
from bs4 import BeautifulSoup as BS4 | |
HOME_FOLDER = os.path.expanduser('~') | |
DOWNLOAD_FOLDER = HOME_FOLDER + 'Please put your download folder here (ex: /Documents/MagPi)' | |
URL = 'http://www.raspberrypi.org/magpi/issues/' | |
response = requests.get(URL).content | |
DOWNLOADING = False | |
def notification(summary, body): | |
subprocess.Popen(['notify-send', summary, body]) | |
return | |
def just_pdf(href): | |
return href and re.compile("pdf").search(href) | |
soup4_pdf = BS4(response, 'html.parser').find_all(href=just_pdf) | |
soup4_title = BS4(response, 'html.parser').find_all('div', re.compile('col-sm-4.*')) | |
# create empty lists | |
pdfs = [] | |
titles = [] | |
# put download url to list | |
for pdf in soup4_pdf: | |
pdfs.append(pdf['href']) | |
for title in soup4_title: | |
correct_title = 'MagPI %s' % title('p')[0].string.replace('MagPi', 'Issue') | |
try: | |
titles.append('%s - %s' % (correct_title, title('p')[1].string)) | |
except IndexError: | |
titles.append(correct_title) | |
# combine 2 list in dict | |
dictionary = dict(zip(titles, pdfs)) | |
for title, download_url in dictionary.iteritems(): | |
# Check if file downloaded before | |
if not os.path.isfile('%s/%s.pdf' % (DOWNLOAD_FOLDER, title)): | |
DOWNLOADING = True | |
# Check if download directory exist | |
if not os.path.isdir(DOWNLOAD_FOLDER): | |
os.mkdir(DOWNLOAD_FOLDER) | |
# Download | |
print 'Downloading: %s.pdf' % title | |
r = requests.get(download_url, stream=True) | |
if r.status_code == 200 and r.headers['content-type'] == 'application/pdf': | |
with open('%s/%s.pdf' % (DOWNLOAD_FOLDER, title), 'wb') as fd: | |
shutil.copyfileobj(r.raw, fd) | |
del r | |
notification('Download Completed!', title) | |
if not DOWNLOADING: | |
print 'There is no new issue!!!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment