Skip to content

Instantly share code, notes, and snippets.

@willianrschuck
Last active October 6, 2020 00:47
Show Gist options
  • Save willianrschuck/d9c0bb6eefaad54f1e2d7dd6551b085f to your computer and use it in GitHub Desktop.
Save willianrschuck/d9c0bb6eefaad54f1e2d7dd6551b085f to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
import getpass
import requests
import re
import os
urlLogin = 'https://moodle.passofundo.ifsul.edu.br/login/index.php'
def getPageContent(url):
return s.get(url).text
def getLoginToken():
soup = BeautifulSoup(getPageContent('https://moodle.passofundo.ifsul.edu.br/login/index.php'), 'html.parser')
return soup.find(attrs={'name':'logintoken'})['value']
def doLogin():
username = input('Usuário: ')
password = getpass.getpass('Senha: ')
r = s.post(urlLogin, data={'username' : username, 'password' : password, 'logintoken' : getLoginToken()})
def extractLink(linkTag):
return linkTag['href']
def getDocumentLinks():
url = input('Url: ')
soup = BeautifulSoup(getPageContent(url), 'html.parser')
linkTags = soup.find_all(attrs={'href':re.compile("resource")})
return list(map(extractLink, linkTags))
def getFilename(request):
try:
return str(re.findall('filename="(.+)"', request.headers['content-disposition'])[0])
except:
return ''
def downloadFile(url):
print(url)
req = s.get(url)
filename = getFilename(req)
if filename:
outFile = open(filename, 'wb')
outFile.write(req.content)
outFile.close()
s = requests.Session()
doLogin()
for link in getDocumentLinks():
downloadFile(link)
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment