/
shareworks.py
134 lines (115 loc) · 4.64 KB
/
shareworks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
from __future__ import division
import os, itertools
import layout_scanner
from bs4 import BeautifulSoup
from docx import opendocx, getdocumenttext
from pptx import Presentation
def readPortfolios(portfolios_dir, verbose=0):
# this function assumes the indir contains files only
filelist = os.listdir(portfolios_dir)
dictlist = []
for f in filelist:
fextstr = f.split(".")[-1]
print "\n\n", fextstr, f
## PDF
if fextstr == "pdf":
pdf_dict = convPdf(portfolios_dir, f)
dictlist.append(pdf_dict)
if verbose > 0: print "\n", pdf_dict
## PPTX
elif fextstr == "pptx":
pptx_dict = convPptx(portfolios_dir, f)
dictlist.append(pptx_dict)
if verbose > 0: print "\n", pptx_dict
## DOCX
elif fextstr == "docx":
docx_dict = convDocx(portfolios_dir, f)
dictlist.append(docx_dict)
if verbose > 0: print "\n", docx_dict
## HTML
elif fextstr in ("html", "htm"):
html_dict = convHtml(portfolios_dir, f)
dictlist.append(html_dict)
if verbose > 0: print "\n", html_dict
else: raise Exception("Check if this filetype can be read!")
return dictlist
def convPdf(portfolios_dir, f, verbose=0):
pdf_filepath = os.path.join(portfolios_dir, f)
toc=layout_scanner.get_toc(pdf_filepath)
if verbose > 0: print "TOC -- not currently used for dict"
if verbose > 0:
for e in toc: print e
pages=layout_scanner.get_pages(pdf_filepath)
doc_dict = {'doctype': "report", 'origin': "shareworks"}
doc_dict['_id'] = f
doc_dict['student_email'] = email_from_fname(f)
doc_dict['content'] = []
for (count, p) in enumerate(pages):
if len(p) > 5: # when is a page non-empty?
doc_dict['content'].append({'header': "Page %s" % count, 'text': p.decode('utf-8')})
if len(doc_dict['content']) == 0:
doc_dict['content'] = [{'text': 'FrozenCutlery', 'txt_ann': None}]
return doc_dict
def convPptx(portfolios_dir, f):
pptx_filepath = os.path.join(portfolios_dir, f)
prs = Presentation(pptx_filepath)
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_textframe:
continue
for paragraph in shape.textframe.paragraphs:
for run in paragraph.runs:
text_runs.append(run.text)
all_slides = "\n".join(text_runs)
doc_dict = {'doctype': "slides", 'origin': "shareworks"}
doc_dict['_id'] = f
doc_dict['student_email'] = email_from_fname(f)
doc_dict['content'] = [{'header': "All slides", 'text': all_slides}]
if len(doc_dict['content'][0]['text']) == 0:
doc_dict['content'] = [{'text': 'FrozenCutlery', 'txt_ann': None}]
return doc_dict
def convDocx(portfolios_dir, f):
docx_filepath = os.path.join(portfolios_dir, f)
docu = opendocx(docx_filepath)
paratextlist = getdocumenttext(docu)
doc_dict = {'doctype': "report", 'origin': "shareworks"}
doc_dict['_id'] = f
doc_dict['student_email'] = email_from_fname(f)
doc_dict['content'] = []
for pair in pairwise(paratextlist):
if len(pair[0]) / len(pair[1]) < 1.5: #assume header is not much longer than text
doc_dict['content'].append({'header': pair[0], 'text': "\n\n".join(pair)})
if len(doc_dict['content']) == 0:
doc_dict['content'] = [{'text': 'FrozenCutlery', 'txt_ann': None}]
return doc_dict
def convHtml(portfolios_dir, f):
html_filepath = os.path.join(portfolios_dir, f)
html_file = open(html_filepath, 'r')
soup = BeautifulSoup(html_file, "lxml")
html_file.close()
doc_dict = {'doctype': "posts", 'origin': "shareworks"}
doc_dict['_id'] = f
doc_dict['student_email'] = soup.head['id'].split(" ")
doc_dict['title'] = soup.find("h1").text
doc_dict['content'] = []
posts = soup.find_all('div', 'sitepagenote')
for post in posts:
doc_dict['content'].append({'header': post.h3.text, 'text': post.text})
if len(doc_dict['content']) == 0:
doc_dict['content'] = [{'text': 'FrozenCutlery', 'txt_ann': None}]
return doc_dict
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)
def email_from_fname(f):
if len(f.split("--")) == 2:
return [f.split("--")[0]]
else: return f.split("--")[:-1]
if __name__ == '__main__' :
portfolios_path = "../Phase B/sw_portfolios"
websites_path = "../Phase B/websites"
readPortfolios(portfolios_path)