diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-10-11 11:30:34 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-10-11 11:30:34 +0200 |
| commit | 93123614dd0fb9ffedc43b71c897b4fe64cc96bc (patch) | |
| tree | e1943f89bc561e824dd74a45075504bacedbc135 | |
| download | moodlescraper-93123614dd0fb9ffedc43b71c897b4fe64cc96bc.tar.gz moodlescraper-93123614dd0fb9ffedc43b71c897b4fe64cc96bc.zip | |
| -rw-r--r-- | .gitignore | 3 | ||||
| -rwxr-xr-x | diagram.py | 68 | ||||
| -rwxr-xr-x | moodler.py | 46 |
3 files changed, 117 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d987b6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +data/ +config.py diff --git a/diagram.py b/diagram.py new file mode 100755 index 0000000..ecef7a6 --- /dev/null +++ b/diagram.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +#-*- coding:utf-8 -*- + +import networkx as nx +from html.parser import HTMLParser +import re, os + +G = nx.Graph() +node_colors = [] + +# gescrapte Daten laden +f = open("data/users.txt", encoding = "utf-8") +addresses = eval(f.read()) +f.close() + +class User(object): + def __init__(self, name, firstname, lastname, mail, nick, provider): + self.name = name + self.firstname = firstname + self.lastname = lastname + self.mail = mail + self.nick = nick + self.provider = provider + +users = [] + +# Adressen durchgehen und Edges / Nodes erstellen +for user in addresses: + if len(addresses[user]) < 1: + continue + + firstname, lastname = re.match("(.+) ([^ ]+)", user).groups() + nick, provider = re.match("(.*?)@([^@]+)", addresses[user]).groups() + + users.append(User(user, firstname, lastname, addresses[user], nick, provider)) + +for user in users: + G.add_node(user.name) + + # nicht weiter verarbeiten + users.remove(user) + + userswithprovider = len([i for i in users if i.provider == user.provider]) + if userswithprovider > 0: + G.add_node(user.provider) + G.add_edge(user.name, user.provider) + + brothersisters = [i for i in users if i.lastname == user.lastname] + if len(brothersisters) > 0: + for brothersister in brothersisters: + G.add_edge(user.name, brothersister.name) + + samenicks = [i for i in users if i.nick == user.nick] + if len(samenicks) > 0: + for samenick in samenicks: + G.add_edge(user.name, samenick.name) + +nx.write_gml(G, 'data/graph.gml') + +# Encoding fixen +p = HTMLParser() +f = open('data/graph.gml', 'r') +s = p.unescape(f.read()) +f.close() +os.remove('data/graph.gml') +f = open('data/graph.gml', 'w') +f.write(s) +f.close() diff --git a/moodler.py b/moodler.py new file mode 100755 index 0000000..5ff9d82 --- /dev/null +++ b/moodler.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +#-*- coding:utf-8 -*- + +import http.cookiejar +import urllib.request +import urllib.parse +import config +from bs4 import BeautifulSoup + +cookiejar = http.cookiejar.CookieJar() + +opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar)) +urllib.request.install_opener(opener) + +data = urllib.parse.urlencode({'username': config.username, 'password': config.password}) +data = data.encode('utf-8') + +request = urllib.request.Request(config.moodleurl + 'login/index.php') + +# Login +f = urllib.request.urlopen(request, data) + +# Scrapen starten +f = urllib.request.urlopen(config.moodleurl + 'user/index.php?perpage=999999&mode=1&id=' + str(config.courseid)) +soup = BeautifulSoup(f.read()) + +# iterieren und speichern +users = dict() +for cell in soup.find_all('td', class_='content'): + thisuser = cell.find_all('div', class_='username') + if thisuser: + thisuser = thisuser[0] + if thisuser: + thisuser = thisuser.string + + thismail = cell.find_all('a') + if thismail: + thismail = thismail[0] + if thismail: + thismail = thismail.string + + users[thisuser] = thismail + +f = open('data/users.txt', mode='w', encoding='utf-8') +f.write(str(users)) +f.close() |
