#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
# -*- coding:utf-8 -*-
import re
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from flatten import flatten


url = "https://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=2ahUKEwjJwOzY5ublAhXRKqYKHRT7AXMQFjAAegQIABAB&url=https%3A%2F%2Fwww.statista.com%2Fstatistics%2F271496%2Fglobal-market-share-held-by-smartphone-vendors-since-4th-quarter-2009%2F&usg=AOvVaw2bfRob7AXtKfBUxrL2HJgj"
client = webdriver.Chrome()
client.get(url)
div = client.find_element_by_css_selector('[data-chart-vars-name="options"]')
vendor_str = div.get_attribute('data-chart-vars')
#print(vendor_str)

pattern = re.compile('"#aaa9d6"],"series":(.*])', re.S)
result = re.search(pattern, vendor_str)
json_str = result.group(1)
print(json_str)
json_data = json.loads(json_str, parse_float=float)
print(json_data)

vendor_list = []
for vendor in json_data:
    if vendor['name'].endswith('*'):
        vendor['name'] = vendor['name'][:-1]
    vendor_list.append(vendor['name'])
print(vendor_list)

quarters = ['2009-Q4']
for year in range(2010, 2020):
    for quarter in range(1, 5):
        if year == 2019 and quarter == 4:
            break;
        q = str(year) + '-Q' + str(quarter)
        quarters.append(q)
print(quarters)

# clean json data
for item in json_data:
    for i in range(len(item['data'])):
        if item['data'][i] != None:
            item['data'][i] = item['data'][i]['y']
print(json_data)

# zip quarter shares
zipped = list(zip(json_data[0]['data'], json_data[1]['data']))
for i in range(2, len(json_data)):
    zipped = list(zip(zipped, json_data[i]['data']))
print(zipped)
    
for shares in zipped:
    print(list(flatten(shares)))

name2cn = {
    'Nokia' : '诺基亚',
    'Samsung': '三星',
    'Apple' : '苹果',
    'Huawei': '华为',
    'OPPO': 'OPPO',
    'HTC': 'HTC',
    'LG': 'LG',
    'vivo': 'vivo',
    'Xiaomi': '小米',
    'ZTE': '中兴',
    'RIM': '黑莓',
    'Sony': '索尼',
    'Lenovo': '联想',
    'Others': '其他',
}
quartly_vender_shares = []
quartly_vender_shares_cn = []
vendor_list_cn = []
for en_name in vendor_list:
    vendor_list_cn.append(name2cn[en_name])


for i in range(len(quarters)):
    q_data = {}
    q_data['period'] = quarters[i]
    q_data['shares'] = []
    q_share = list(flatten(zipped[i])) # share number list of one quarter
    for i in range(len(vendor_list)):
        vendor_share = {}
        vendor_share['name'] = vendor_list[i]
        vendor_share['percentage'] = q_share[i]
        q_data['shares'].append(vendor_share)
    quartly_vender_shares.append(q_data) 
 
for i in range(len(quarters)):
    q_data = {}
    q_data['period'] = quarters[i]
    q_data['shares'] = []
    q_share = list(flatten(zipped[i])) # share number list of one quarter
    for i in range(len(vendor_list_cn)):
        vendor_share = {}
        vendor_share['name'] = vendor_list_cn[i]
        vendor_share['percentage'] = q_share[i]
        q_data['shares'].append(vendor_share)
    quartly_vender_shares_cn.append(q_data) 

for item in quartly_vender_shares:
    print(item)

with open('smartphone-vendors-share-2009Q4-2019Q3.json', 'w') as f:
    json.dump(quartly_vender_shares, f, indent=2)
with open('smartphone-vendors-share-2009Q4-2019Q3-cn.json', 'w') as f:
    json.dump(quartly_vender_shares_cn, f, ensure_ascii=False, indent=2)
