import requests
import json
import re
import time

default_headers = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 ' \
                  'Safari/537.36 '


def http_get(url):
    rsp = requests.get(url, proxies={'http': '', 'https': ''}).text
    json_data = json.loads(rsp)
    return json_data


def http_put(url, data):
    # 必须指定Content-Type为application/json才能成功请求
    headers = {'User-Agent': default_headers,
               "Content-Type": "application/json"}
    rsp = requests.put(url, data=data, headers=headers)
    return rsp


def locationTranslate(locationStr):
    digitLocation = 0
    digits = re.findall(r'\d+', locationStr)

    t = 1
    for d in digits:
        digitLocation += float(d) / t
        t *= 60
    if locationStr.find('S') >= 0 or locationStr.find('W') >= 0:
        return -digitLocation
    else:
        return digitLocation


def dateTranslate(dateStr):
    try:
        t = time.mktime(time.strptime(dateStr, '%B %d, %Y at %H%M UTC'))
        return t + 8 * 3600
    except:
        print('日期转换失败')
        return 0


def dateTranslateCsv(dateStr):
    try:
        t = time.mktime(time.strptime(dateStr, '%m/%d/%Y %H:%M'))
        return t + 8 * 3600
    except:
        print('日期转换失败')
        return 0
