# Path: search/data/category_mappings.py
# def mit_to_merlot_category(cat, subcat=None, spec=None):
#     # print cat, subcat, spec
#     if cat and subcat and spec:
#         cat = cat[0]
#         subcat = subcat[0]
#         spec = spec[0]
#         return MIT_MAPPING[cat][subcat][spec]['_cat']
# 
#     if cat and subcat:
#         cat = cat[0]
#         subcat = subcat[0]
#         return MIT_MAPPING[cat][subcat]['_cat']
# 
#     if cat:
#         cat = cat[0]
#         return MIT_MAPPING[cat]['_cat']


import xlwt
import datetime
import requests
import requests_cache
import xml.etree.ElementTree as ET
import lxml.html
from lxml import etree
from lxml.cssselect import CSSSelector
from optparse import make_option
from collections import OrderedDict
from urlparse import urlsplit, parse_qs
from django.core.management.base import BaseCommand
from django.conf import settings
from ...category_mappings import mit_to_merlot_category
from data.models import Source, Course, MerlotCategory, MerlotLanguage, SearchQuery
            'url': url
        }

        while True:
            r = requests.get(settings.MERLOT_API_URL + '/materialsAdvanced.rest', params=params)

            tree = ET.fromstring(r.content)
            num_results = int(tree.find('nummaterialstotal').text)

            if num_results > 0:
                for material in tree.findall('material'):
                    url = material.find('URL').text

                    if print_all_urls:
                        print(url)
                    else:
                        self._locate_local_url(url)

                if params['page'] * 10 > num_results:
                    break

                params['page'] += 1
            else:
                break

    def local_subdomain_search(self, url):
        def _map_mit_categories(mit_categories):
            cats = []
            for url in mit_categories:
                qs = parse_qs(url.split('#')[1])

# Path: search/data/category_mappings.py
# def mit_to_merlot_category(cat, subcat=None, spec=None):
#     # print cat, subcat, spec
#     if cat and subcat and spec:
#         cat = cat[0]
#         subcat = subcat[0]
#         spec = spec[0]
#         return MIT_MAPPING[cat][subcat][spec]['_cat']
# 
#     if cat and subcat:
#         cat = cat[0]
#         subcat = subcat[0]
#         return MIT_MAPPING[cat][subcat]['_cat']
# 
#     if cat:
#         cat = cat[0]
#         return MIT_MAPPING[cat]['_cat']


# Path: search/data/management/commands/merlot.py
import xlwt
import datetime
import requests
import requests_cache
import xml.etree.ElementTree as ET
import lxml.html
from lxml import etree
from lxml.cssselect import CSSSelector
from optparse import make_option
from collections import OrderedDict
from urlparse import urlsplit, parse_qs
from django.core.management.base import BaseCommand
from django.conf import settings
from ...category_mappings import mit_to_merlot_category
from data.models import Source, Course, MerlotCategory, MerlotLanguage, SearchQuery

            'url': url
        }

        while True:
            r = requests.get(settings.MERLOT_API_URL + '/materialsAdvanced.rest', params=params)

            tree = ET.fromstring(r.content)
            num_results = int(tree.find('nummaterialstotal').text)

            if num_results > 0:
                for material in tree.findall('material'):
                    url = material.find('URL').text

                    if print_all_urls:
                        print(url)
                    else:
                        self._locate_local_url(url)

                if params['page'] * 10 > num_results:
                    break

                params['page'] += 1
            else:
                break

    def local_subdomain_search(self, url):
        def _map_mit_categories(mit_categories):
            cats = []
            for url in mit_categories:
                qs = parse_qs(url.split('#')[1])

                cat = mit_to_merlot_category(cat=qs.get('cat'), subcat=qs.get('subcat'), spec=qs.get('spec'))