<commit_before>import os
import requests
import xml.etree.ElementTree as ET
from string import index

def get_filetypes(file_id):
	RESULTS = []

	base_path = os.path.dirname(os.path.abspath(__file__))
	files_path = os.path.join(base_path, 'static/files')
	files = os.listdir(files_path)

	for file in files:
		i = file.index('.')
		file_name = file[:i]
		file_type = file[i+1:]
		
		if file_name == file_id:
			RESULTS.append(file_type)

	return RESULTS

def get_metadata(file_id):
	RESULTS = {}

	r = requests.get("http://walter.mit.edu/rest-dlf/record/mit01" + file_id + "?view=full")
	metadata = r.content

	record = ET.fromstring(metadata).find('record')
	title = record.find("./datafield[@tag='245']/*[@code='a']").text
	author = record.find("./datafield[@tag='245']/*[@code='c']").text	

	title = title.rstrip('/ ')
	author = author.rstrip('. ')

	RESULTS['title'] = title
	RESULTS['author'] = author

	return RESULTS<commit_msg>Add alternative XML parsing code that works in Python 2.6<commit_after>import os
import requests
import xml.etree.ElementTree as ET
from string import index

def get_filetypes(file_id):
	RESULTS = []

	base_path = os.path.dirname(os.path.abspath(__file__))
	files_path = os.path.join(base_path, 'static/files')
	files = os.listdir(files_path)

	for file in files:
		i = file.index('.')
		file_name = file[:i]
		file_type = file[i+1:]
		
		if file_name == file_id:
			RESULTS.append(file_type)

	return RESULTS

def get_metadata(file_id):
	RESULTS = {}

	r = requests.get("http://walter.mit.edu/rest-dlf/record/mit01" + file_id + "?view=full")
	metadata = r.content

	record = ET.fromstring(metadata).find('record')
	fields = record.findall("./datafield")
	for field in fields:
		if field.attrib.get('tag') == '245':
			titleElements = field.getchildren()
			for item in titleElements:
				if item.attrib.get('code') == 'a':
					title = item.text
				if item.attrib.get('code') == 'c':
					author = item.text

	## Much simpler code for lines 30-38, can only use in Python 2.7+:
	# title = record.find("./datafield[@tag='245']/*[@code='a']").text
	# author = record.find("./datafield[@tag='245']/*[@code='c']").text	

	title = title.rstrip('/ ')
	author = author.rstrip('. ')

	RESULTS['title'] = title
	RESULTS['author'] = author

	return RESULTS

if __name__ == '__main__':
	print get_metadata('001387502')
{'fraction_non_alphanumeric': 0.16986769099445156, 'fraction_numerical': 0.01621852326077678, 'mean_word_length': 4.2792792792792795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1136625', 'n_tokens_mistral': 919, 'n_tokens_neox': 826, 'n_words': 176}