Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

changing version

  • Loading branch information...
commit 99773c89d156c9344ebd6caefed0a6a74367c5a0 1 parent d316bd8
@gcmalloc authored
Showing with 23 additions and 9 deletions.
  1. +3 −3 bin/moodle
  2. +19 −5 epfl/moodle.py
  3. +1 −1  setup.py
View
6 bin/moodle
@@ -8,7 +8,7 @@ import getpass
CONF_FILE = ".moodle"
-SELECT_ALL = 'a'
+SELECT_ALL_KEY = 'a'
def init_moodle_directory(moodle):
"""This method will create a .moodle file. This contains the list of all the courses
to be downloaded
@@ -16,13 +16,13 @@ def init_moodle_directory(moodle):
print("Downloading a list of courses")
courses = list(moodle.get_courses())
print("Select the courses you want to download separated"
- "by a comma, '{}' to select all courses.").format(SELECT_ALL)
+ "by a comma, '{}' to select all courses.").format(SELECT_ALL_KEY)
for index, course in enumerate(courses):
print(u"\t{})) {}".format(index, course.name))
valid = False
while not valid:
input = raw_input(">>")
- if input == SELECT_ALL:
+ if input == SELECT_ALL_KEY:
selection = courses
valid = True
print("All the course are selected")
View
24 epfl/moodle.py
@@ -5,6 +5,7 @@
try:
from bs4 import BeautifulSoup
except ImportError:
+ #fallback from bs4
from BeautifulSoup import BeautifulSoup
import os
import socket
@@ -27,6 +28,10 @@ class Moodle(object):
CHUNCK = 1024 * 1024
+ TEQUILA_LOGIN = "http://moodle.epfl.ch/login/index.php"
+
+ MAIN_PAGE = "http://moodle.epfl.ch/my"
+
def __init__(self, username, password, caching=False):
"""This will create a new moodle handshake.
"""
@@ -42,9 +47,11 @@ def __init__(self, username, password, caching=False):
def login(self, username, password):
"""Explicitly login into the moodle service, this will create
a new moodle session as self.session
+
+ :raise TequilaError:
"""
with requests.session() as self.session:
- resp = self.session.get("http://moodle.epfl.ch/login/index.php")
+ resp = self.session.get(Moodle.TEQUILA_LOGIN)
if resp.status_code != 200:
raise ConnexionIssue()
parsed_url = urlparse.urlsplit(resp.url)
@@ -54,6 +61,7 @@ def login(self, username, password):
resp = self.session.post("https://tequila.epfl.ch/cgi-bin/tequila/login", verify=True, data=payload)
error = BeautifulSoup(resp.text).find('font', color='red', size='+1')
if error:
+ #grab the tequila error if any
raise TequilaError(error.string)
if resp.status_code != 200:
raise ConnexionIssue()
@@ -66,7 +74,9 @@ def __exit__(self):
def get_courses(self):
"""return a dict with course id as key and course name as value
"""
- main_page = self.session.get("http://moodle.epfl.ch/my/")
+ main_page = self.session.get(Moodle.MAIN_PAGE)
+ if main_page.status_code != 200:
+ raise ConnexionIssue()
soup = BeautifulSoup(main_page.text)
for course_head in soup('h3', 'main'):
course_link = course_head.find('a')
@@ -78,6 +88,8 @@ def get_documents(self, course):
document in the course and in the section.
"""
course_page = self.session.get(course.link)
+ if course_page.status_code != 200:
+ raise ConnexionIssue()
soup = BeautifulSoup(course_page.text)
content = soup.find('div', {'class':'course-content'})
#Week separation
@@ -95,9 +107,11 @@ def get_documents(self, course):
return divisions
def fetch_document(self, document, directory=""):
- """Download document `document`
+ """Download document `document` into `directory`
"""
content_page = self.session.get(document.link)
+ if content_page.status_code != 200:
+ raise ConnexionIssue()
if content_page.url != document.link:
#we have a redirection
content_url = content_page.url
@@ -111,9 +125,9 @@ def fetch_document(self, document, directory=""):
parent_content_tag = soup.find('div', 'resourceworkaround')
content_url = parent_content_tag.find('a')['href']
+ file_in = self.session.get(content_url)
file_name = os.path.basename(urlparse.urlparse(content_url)[2])
file_path = os.path.join(directory, file_name)
- file_in = self.session.get(content_url)
if os.path.exists(file_path):
logging.error(u"File {} already exist".format(file_path))
@@ -122,4 +136,4 @@ def fetch_document(self, document, directory=""):
with open(file_path, 'wb') as file_out:
file_out.write(file_in.content)
- return
+
View
2  setup.py
@@ -1,7 +1,7 @@
from setuptools import setup
setup(
name='epfl-moodle',
- version = '0.1.0',
+ version = '0.1.1',
description = 'A simple interface to moodle',
author = 'gcmalloc',
url = 'http://github.com/gcmalloc/epfl-moodle',
Please sign in to comment.
Something went wrong with that request. Please try again.