"""
    日期：2018/09/06
    功能：AQI计算
    版本：7.0
    获取所有城市AQI
"""

import requests
from bs4 import BeautifulSoup


def get_city_aqi(city_pinyin):

    url = 'http://pm25.in/'+city_pinyin
    result = requests.get(url, timeout=30)
    soup = BeautifulSoup(result.text, 'lxml')
    value_list = soup.find_all('div', {'class': 'span1'})

    aqi_list = []
    for i in range(8):
        div_content = value_list[i]
        caption = div_content.find('div', {'class': 'caption'}).text.strip()
        value = div_content.find('div', {'class': 'value'}).text.strip()
        aqi_list.append((caption, value))
    return aqi_list


def get_all_cities():
    url = 'http://pm25.in/'
    city_list = []
    r = requests.get(url, timeout=30)
    soup = BeautifulSoup(r.text, 'lxml')

    city_div = soup.find_all('div', {'class': 'bottom'})[1]
    city_link_list = city_div.find_all('a')
    for city_link in city_link_list:
        city_name = city_link.text
        city_pinyin = city_link['href'][1:]
        city_list.append((city_name, city_pinyin))

    return city_list


def main():
    city_list = get_all_cities()
    for city in city_list:
        name = city[0]
        pinyin = city[1]
        aqi = get_city_aqi(pinyin)
        print('{}:{}'.format(name,aqi))


if __name__ == "__main__":
    main()
