# Path: courseraresearchexports/constants/api_constants.py
# EXPORT_TYPE_CLICKSTREAM = 'RESEARCH_EVENTING'
# 
# EXPORT_TYPE_TABLES = 'RESEARCH_WITH_SCHEMAS'
# 
# Path: courseraresearchexports/exports/api.py
# def get(export_job_id):
# def get_all():
# def post(export_request):
# def get_clickstream_download_links(clickstream_download_links_request):
# 
# Path: courseraresearchexports/models/ClickstreamDownloadLinksRequest.py
# class ClickstreamDownloadLinksRequest:
#     """
#     Represents a request for clickstream download links.
#     """
# 
#     def __init__(self, course_id=None, partner_id=None, interval=None,
#                  **kwargs):
#         self.course_id = course_id
#         self.partner_id = partner_id
#         self.interval = interval
# 
#     @staticmethod
#     def from_args(**kwargs):
#         """
#         Create a ClickstreamDownloadLinkRequest from arguments. Performs
#         course_id/partner_id inference.
#         :param kwargs:
#         :return eventing_links_request: ClickstreamDownloadLinksRequest
#         """
#         if kwargs.get('course_slug') and not kwargs.get('course_id'):
#             kwargs['course_id'] = utils.lookup_course_id_by_slug(
#                 kwargs['course_slug'])
#         elif kwargs.get('partner_short_name') and not kwargs.get('partner_id'):
#             kwargs['partner_id'] = \
#                 utils.lookup_partner_id_by_short_name(
#                     kwargs['partner_short_name'])
#         elif kwargs.get('group_id'):
#             logging.error(
#                 'Eventing exports by group is not currently supported. '
#                 'Please see: '
#                 'https://partner.coursera.help/hc/articles/360021121132'
#                 )
#             raise ValueError('Eventing exports by group is not supported.')
# 
#         return ClickstreamDownloadLinksRequest(**kwargs)
# 
#     @property
#     def scope(self):
#         """
#         API specific format for request scope context.
#         :return scope:
#         """
#         if self.course_id:
#             return 'courseContext~{}'.format(self.course_id)
#         elif self.partner_id:
#             return 'partnerContext~{}'.format(self.partner_id)
# 
#     def to_url_params(self):
#         """
#         API specific parameters for POST request.
#         :return:
#         """
#         url_params = {'action': 'generateLinks', 'scope': self.scope}
#         if self.interval:
#             url_params['startDate'] = self.interval[0]
#             url_params['endDate'] = self.interval[1]
# 
#         return url_params


import logging
import os
import requests
from urlparse import urlparse
from tqdm import tqdm
from courseraresearchexports.constants.api_constants import \
    EXPORT_TYPE_CLICKSTREAM, EXPORT_TYPE_TABLES
from courseraresearchexports.exports import api
from courseraresearchexports.models.ClickstreamDownloadLinksRequest import \
    ClickstreamDownloadLinksRequest
    """
    try:
        is_table_export = export_request.export_type == EXPORT_TYPE_TABLES
        is_clickstream_export = \
            export_request.export_type == EXPORT_TYPE_CLICKSTREAM

        _validate(export_request)

        if not os.path.exists(dest):
            logging.info('Creating destination folder: {}'.format(dest))
            os.makedirs(dest)

        if is_table_export:
            return [download_url(export_request.download_link, dest)]
        elif is_clickstream_export:
            links_request = ClickstreamDownloadLinksRequest.from_args(
                course_id=export_request.course_id,
                partner_id=export_request.partner_id,
                interval=export_request.interval)
            download_links = api.get_clickstream_download_links(links_request)
            if len(download_links) == 0:
                raise ValueError(
                    'Clickstream download links not found. This typically '
                    'means no data was available for the dates in '
                    'the specified interval: {interval}'
                    .format(interval=export_request.interval))
            return [download_url(link, dest) for link in download_links]
        else:
            raise ValueError('Require export_type is one of {} or {}'.format(
                EXPORT_TYPE_TABLES,

# Path: courseraresearchexports/constants/api_constants.py
# EXPORT_TYPE_CLICKSTREAM = 'RESEARCH_EVENTING'
# 
# EXPORT_TYPE_TABLES = 'RESEARCH_WITH_SCHEMAS'
# 
# Path: courseraresearchexports/exports/api.py
# def get(export_job_id):
# def get_all():
# def post(export_request):
# def get_clickstream_download_links(clickstream_download_links_request):
# 
# Path: courseraresearchexports/models/ClickstreamDownloadLinksRequest.py
# class ClickstreamDownloadLinksRequest:
#     """
#     Represents a request for clickstream download links.
#     """
# 
#     def __init__(self, course_id=None, partner_id=None, interval=None,
#                  **kwargs):
#         self.course_id = course_id
#         self.partner_id = partner_id
#         self.interval = interval
# 
#     @staticmethod
#     def from_args(**kwargs):
#         """
#         Create a ClickstreamDownloadLinkRequest from arguments. Performs
#         course_id/partner_id inference.
#         :param kwargs:
#         :return eventing_links_request: ClickstreamDownloadLinksRequest
#         """
#         if kwargs.get('course_slug') and not kwargs.get('course_id'):
#             kwargs['course_id'] = utils.lookup_course_id_by_slug(
#                 kwargs['course_slug'])
#         elif kwargs.get('partner_short_name') and not kwargs.get('partner_id'):
#             kwargs['partner_id'] = \
#                 utils.lookup_partner_id_by_short_name(
#                     kwargs['partner_short_name'])
#         elif kwargs.get('group_id'):
#             logging.error(
#                 'Eventing exports by group is not currently supported. '
#                 'Please see: '
#                 'https://partner.coursera.help/hc/articles/360021121132'
#                 )
#             raise ValueError('Eventing exports by group is not supported.')
# 
#         return ClickstreamDownloadLinksRequest(**kwargs)
# 
#     @property
#     def scope(self):
#         """
#         API specific format for request scope context.
#         :return scope:
#         """
#         if self.course_id:
#             return 'courseContext~{}'.format(self.course_id)
#         elif self.partner_id:
#             return 'partnerContext~{}'.format(self.partner_id)
# 
#     def to_url_params(self):
#         """
#         API specific parameters for POST request.
#         :return:
#         """
#         url_params = {'action': 'generateLinks', 'scope': self.scope}
#         if self.interval:
#             url_params['startDate'] = self.interval[0]
#             url_params['endDate'] = self.interval[1]
# 
#         return url_params


# Path: courseraresearchexports/exports/utils.py
import logging
import os
import requests
from urlparse import urlparse
from tqdm import tqdm
from courseraresearchexports.constants.api_constants import \
    EXPORT_TYPE_CLICKSTREAM, EXPORT_TYPE_TABLES
from courseraresearchexports.exports import api
from courseraresearchexports.models.ClickstreamDownloadLinksRequest import \
    ClickstreamDownloadLinksRequest

    """
    try:
        is_table_export = export_request.export_type == EXPORT_TYPE_TABLES
        is_clickstream_export = \
            export_request.export_type == EXPORT_TYPE_CLICKSTREAM

        _validate(export_request)

        if not os.path.exists(dest):
            logging.info('Creating destination folder: {}'.format(dest))
            os.makedirs(dest)

        if is_table_export:
            return [download_url(export_request.download_link, dest)]
        elif is_clickstream_export:
            links_request = ClickstreamDownloadLinksRequest.from_args(
                course_id=export_request.course_id,
                partner_id=export_request.partner_id,
                interval=export_request.interval)
            download_links = api.get_clickstream_download_links(links_request)
            if len(download_links) == 0:
                raise ValueError(
                    'Clickstream download links not found. This typically '
                    'means no data was available for the dates in '
                    'the specified interval: {interval}'
                    .format(interval=export_request.interval))
            return [download_url(link, dest) for link in download_links]
        else:
            raise ValueError('Require export_type is one of {} or {}'.format(
                EXPORT_TYPE_TABLES,

                EXPORT_TYPE_CLICKSTREAM))