Created
March 14, 2012 19:35
-
-
Save mattdeboard/2038893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from datetime import datetime | |
| from lxml import etree | |
| def time_handler(s): | |
| t = time.strptime(s, "%m/%d/%Y %I:%M:%S %p") | |
| return time.strftime("%Y-%m-%dT%H:%M:%S%Z", t) | |
| def feed_parser(tree): | |
| nodes = [] | |
| for job in tree: | |
| if job[0] == 'start': | |
| node = {"Jobid": job[1].attrib['Jobid']} | |
| for child in job[1].getchildren(): | |
| if child.tag == "JobCategories": | |
| val = child.getchildren()[0].getchildren()[0].text.strip() | |
| elif child.tag in ["JobPostDate", "JobExpireDate"]: | |
| val = time_handler(child.text.strip()) | |
| else: | |
| val = child.text.strip() | |
| node[unicode(child.tag.strip())] = unicode(val) | |
| nodes.append(node) | |
| else: | |
| continue | |
| return nodes | |
| ktype = {"kelly_job": {"properties": | |
| {"DivisionDesc": {"type": "string", "store": "yes"}, | |
| "Jobid": {"type": "string", "store": "yes"}, | |
| "JobTitle": {"type": "string", "store": "yes"}, | |
| "Salary": {"type": "float", "store": "yes"}, | |
| "JobBody": {"type": "string", "store": "yes"}, | |
| "PhyCity": {"type": "string", "store": "yes"}, | |
| "PhyState": {"type": "string", "store": "yes"}, | |
| "PhyCountryCode": {"type": "string", "store": "yes"}, | |
| "PhyPostalCode": {"type": "string", "store": "yes"}, | |
| "JobPostDate": {"type": "date", "store": "yes"}, | |
| "JobExpireDate": {"type": "date", "store": "yes"}, | |
| "ApplyOnlineURL": {"type": "string", "store": "yes"}, | |
| "JobCategories": {"type": "string", "store": "yes"}}}} | |
| if __name__ == "__main__": | |
| from pyelasticsearch import ElasticSearch | |
| kn = "kelly_job" | |
| idx = "kelly" | |
| conn = ElasticSearch("http://localhost:9200/") | |
| conn.create_index(idx) | |
| conn.put_mapping(kn, ktype) | |
| XML = etree.parse("xml2.xml") | |
| walk = etree.iterwalk(XML, events=("start", "end"), tag="Job") | |
| for node in feed_parser(walk): | |
| conn.index(node, idx, kn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment