# Path: wikidat/utils/ipresolver.py
# def getCountryCode(ip_str):
#     '''
#     For a given ip address (as string), locate its country code.
# 
#     ip_str        Target IP address (as string)
#     lowerAdd      List of lower ranges
#     upperAdd      List of upper ranges
#     countryCodes  Range country codes
# 
#     Return the target countr code (or NaN if not know.)
#     '''
#     try:
#         ip = ipaddress.ip_address(str(ip_str))
#     except:
#         # Invalid IP
#         return None
#     ip_int = int(ip)
# 
#     idx = bisect.bisect_left(__UPPER_ADD__, ip_int)
# 
#     if __LOWER__ADD__[idx] <= ip_int and ip_int <= __UPPER_ADD__[idx]:
#         return __COUNTRY__[idx]
#     else:
#         ip_t = str(ip_int)
#         ip_l = str(__LOWER__ADD__[idx])
#         ip_h = str(__UPPER_ADD__[idx])
#         print('Address ', ip_t, ' is not in range [', ip_l, ', ', ip_h, ']')
#         return None


import sys
import MySQLdb as db
from pandas import DataFrame
from pandas.io.sql import read_sql
from wikidat.utils.ipresolver import getCountryCode 
'''
Create table of country contributions for each wiki page.

@author: carlosm
'''


if __name__ == '__main__':
    db_name = sys.argv[1]
    db_user = sys.argv[2]
    db_pass = sys.argv[3]

    # Load data for each page
    con = db.connect('localhost',db_user,db_pass,db_name)
    query = 'SELECT rev_user, rev_ip, page_id, page_namespace, name FROM revision ' + \
        'INNER JOIN page ON page.page_id=revision.rev_page ' + \
        'INNER JOIN namespaces ON page.page_namespace=namespaces.code'
    data     = read_sql(query, con)
    con.close()

    # Map IP addresses to country codes

# Path: wikidat/utils/ipresolver.py
# def getCountryCode(ip_str):
#     '''
#     For a given ip address (as string), locate its country code.
# 
#     ip_str        Target IP address (as string)
#     lowerAdd      List of lower ranges
#     upperAdd      List of upper ranges
#     countryCodes  Range country codes
# 
#     Return the target countr code (or NaN if not know.)
#     '''
#     try:
#         ip = ipaddress.ip_address(str(ip_str))
#     except:
#         # Invalid IP
#         return None
#     ip_int = int(ip)
# 
#     idx = bisect.bisect_left(__UPPER_ADD__, ip_int)
# 
#     if __LOWER__ADD__[idx] <= ip_int and ip_int <= __UPPER_ADD__[idx]:
#         return __COUNTRY__[idx]
#     else:
#         ip_t = str(ip_int)
#         ip_l = str(__LOWER__ADD__[idx])
#         ip_h = str(__UPPER_ADD__[idx])
#         print('Address ', ip_t, ' is not in range [', ip_l, ', ', ip_h, ']')
#         return None


# Path: wikidat/analytics/ip_countries/country_contributions.py
import sys
import MySQLdb as db
from pandas import DataFrame
from pandas.io.sql import read_sql
from wikidat.utils.ipresolver import getCountryCode 

'''
Create table of country contributions for each wiki page.

@author: carlosm
'''


if __name__ == '__main__':
    db_name = sys.argv[1]
    db_user = sys.argv[2]
    db_pass = sys.argv[3]

    # Load data for each page
    con = db.connect('localhost',db_user,db_pass,db_name)
    query = 'SELECT rev_user, rev_ip, page_id, page_namespace, name FROM revision ' + \
        'INNER JOIN page ON page.page_id=revision.rev_page ' + \
        'INNER JOIN namespaces ON page.page_namespace=namespaces.code'
    data     = read_sql(query, con)
    con.close()

    # Map IP addresses to country codes

    data['country'] = data['rev_ip'].apply(lambda ip: getCountryCode(ip))