<commit_before>#-*- coding:utf-8 -*-
"""
@file
@brief The file contains a class which collects data coming from Velib.

"""

import os, os.path, sys, datetime, urllib, json, time

class DataVelibCollect :
    """
    This class automates data collecting from Velib website.
    The service is provided at `JCDecaux developper <https://developer.jcdecaux.com/#/home>`_.
    
    see `notebook on Velib <http://nbviewer.ipython.org/5520933>`_
    
    The list of contracts for JCDecaux can obtained at:
    `Données statiques <https://developer.jcdecaux.com/#/opendata/vls?page=static>`_.
    
    The API provided by JCDecaux is described `here <https://developer.jcdecaux.com/#/opendata/vls?page=dynamic>`_.
    """

    #: list of available cities = contract
    _contracts_static = { k:1 for k in [ 'Arcueil',
                       'Besancon',
                       'Lyon',
                       'Paris',
                        ] }
                       
    # api: two substring to replace (contract, apiKey)
    _url_api  = "https://api.jcdecaux.com/vls/v1/stations?contract=%s&apiKey=%s"
    _url_apic = "https://api.jcdecaux.com/vls/v1/contracts?apiKey=%s"

    
    def __init__ (self, apiKey, fetch_contracts = None) :
        """
        constructor
        
        @param          apiKey              api key
        @param          fetch_contracts     if True, it uses a short list of known contracts,
                                            otherwise, it will updated through the website API
        """
        self.apiKey = apiKey
        self.contracts = DataVelibCollect._contracts_static if not fetch_contracts else self.get_contracts() 
        
        # sometimes, lng and lat are null, check if some past retreiving returned non null coordinates
        self.memoGeoStation = { }

    def get_contracts(self):
        """
        returns the list of contracts 
        
        @return     dictionary    { 'station':1 }
        """
        url = DataVelibCollect._url_apic % (self.apiKey)
        try :
            with urllib.request.urlopen(url) as u :
                js = u.read()
        except (urllib.error.HTTPError, urllib.error.URLError) as exc :
            # there was probably a mistake
            # We try again after a given amount of time
            time.sleep(0.5)
            try :
                with urllib.request.urlopen(url) as u :
                    js = u.read()
            except (urllib.error.HTTPError, urllib.error.URLError) as exc :<commit_msg>change a default value type<commit_after>#-*- coding:utf-8 -*-
"""
@file
@brief The file contains a class which collects data coming from Velib.

"""

import os, os.path, sys, datetime, urllib, json, time

class DataVelibCollect :
    """
    This class automates data collecting from Velib website.
    The service is provided at `JCDecaux developper <https://developer.jcdecaux.com/#/home>`_.
    
    see `notebook on Velib <http://nbviewer.ipython.org/5520933>`_
    
    The list of contracts for JCDecaux can obtained at:
    `Données statiques <https://developer.jcdecaux.com/#/opendata/vls?page=static>`_.
    
    The API provided by JCDecaux is described `here <https://developer.jcdecaux.com/#/opendata/vls?page=dynamic>`_.
    """

    #: list of available cities = contract
    _contracts_static = { k:1 for k in [ 'Arcueil',
                       'Besancon',
                       'Lyon',
                       'Paris',
                        ] }
                       
    # api: two substring to replace (contract, apiKey)
    _url_api  = "https://api.jcdecaux.com/vls/v1/stations?contract=%s&apiKey=%s"
    _url_apic = "https://api.jcdecaux.com/vls/v1/contracts?apiKey=%s"

    
    def __init__ (self, apiKey, fetch_contracts = False) :
        """
        constructor
        
        @param          apiKey              api key
        @param          fetch_contracts     if True, it uses a short list of known contracts,
                                            otherwise, it will updated through the website API
        """
        self.apiKey = apiKey
        self.contracts = DataVelibCollect._contracts_static if not fetch_contracts else self.get_contracts() 
        
        # sometimes, lng and lat are null, check if some past retreiving returned non null coordinates
        self.memoGeoStation = { }

    def get_contracts(self):
        """
        returns the list of contracts 
        
        @return     dictionary    { 'station':1 }
        """
        url = DataVelibCollect._url_apic % (self.apiKey)
        try :
            with urllib.request.urlopen(url) as u :
                js = u.read()
        except (urllib.error.HTTPError, urllib.error.URLError) as exc :
            # there was probably a mistake
            # We try again after a given amount of time
            time.sleep(0.5)
            try :
                with urllib.request.urlopen(url) as u :
                    js = u.read()
            except (urllib.error.HTTPError, urllib.error.URLError) as exc :
{'fraction_non_alphanumeric': 0.09688306531665675, 'fraction_numerical': 0.005558864403414731, 'mean_word_length': 1.8383098591549296, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4995063', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1464, 'n_words': 404}