from django.http import HttpResponse, HttpResponseBadRequest

from hashlib import sha1
from lxml import etree

import urllib.request
import json

def _make_get_request(url):
    req = urllib.request.Request(url)
    resp_json = urllib.request.urlopen(req).read().decode('utf-8')
    resp = json.loads(resp_json)
    return resp


def _make_post_request(url, post_data):
    post_encoded = urllib.parse.urlencode(post_data).encode('utf-8')
    req = urllib.request.Request(url, data=post_encoded, method='POST')
    resp_json = urllib.request.urlopen(req).read().decode('utf-8')
    resp = json.loads(resp_json)
    return resp


def _check_token(request):
    get_dict = request.GET.dict()
    needed_token = ['signature', 'timestamp', 'nonce', 'echostr']
    for token in needed_token:
        if token not in get_dict:
            return HttpResponseBadRequest('invalid tokens')

    my_token = 'uvacsssvoice'
    arr = [my_token, get_dict['timestamp'], get_dict['nonce']]
    arr.sort()
    before_hash = arr[0] + arr[1] + arr[2]
    after_hash = sha1(before_hash.encode('utf-8')).hexdigest()

    if after_hash == get_dict['signature']:
        return HttpResponse(get_dict['echostr'])
    else:
        return HttpResponseBadRequest('something went wrong')


def _reply(from_name, to_name, create_time, msg_type, content):
    ret = etree.Element('xml')
    etree.SubElement(ret, 'FromUserName').text = from_name
    etree.SubElement(ret, 'ToUserName').text = to_name
    etree.SubElement(ret, 'CreateTime').text = create_time
    etree.SubElement(ret, 'MsgType').text = msg_type
    etree.SubElement(ret, 'Content').text = content

    ret_str = etree.tostring(ret, pretty_print=True, encoding='unicode')
    return HttpResponse(ret_str, content_type='application/xml')


def _handle_reply(request):
    tree = etree.fromstring(request.body.decode('utf-8'))
    try:
        from_name = tree.xpath('/xml/FromUserName')[0].text
        to_name = tree.xpath('/xml/ToUserName')[0].text
        create_time = tree.xpath('/xml/CreateTime')[0].text
        msg_type = tree.xpath('/xml/MsgType')[0].text
    except:
        return HttpResponseBadRequest('cannot parse correct xml')

    if msg_type != 'text':
        txt = '弹幕仅支持文本信息，请按\"弹幕 <想发送的内容>\"格式发弹幕'
        return _reply(to_name, from_name, create_time, msg_type, txt)
    else:
        txt = '弹幕仅支持文本信息，请按\"弹幕 <想发送的内容>\"格式发弹幕'
        return _reply(to_name, from_name, create_time, msg_type, txt)


def index_page(request):
    if request.method == 'GET': return _check_token(request)
    if request.method == 'POST': return _handle_reply(request)
    return HttpResponseBadRequest('bad request type')

@@ -2222,32 +2222,34 @@
 = '%E5%BC%B9%E5%B9%95%E4%BB%85%E6%94%AF%E6%8C%81%E6%96%87%E6%9C%AC%E4%BF%A1%E6%81%AF%EF%BC%8C%E8%AF%B7%E6%8C%89%5C
+n%5C
 %22%E5%BC%B9%E5%B9%95 %3C%E6%83%B3%E5%8F%91%E9%80%81%E7%9A%84%E5%86%85%E5%AE%B9%3E%5C%22%E6%A0%BC%E5%BC%8F
@@ -2238,32 +2238,34 @@
 n%5C%22%E5%BC%B9%E5%B9%95 %3C%E6%83%B3%E5%8F%91%E9%80%81%E7%9A%84%E5%86%85%E5%AE%B9%3E%5C%22
+%5Cn
 %E6%A0%BC%E5%BC%8F%E5%8F%91%E5%BC%B9%E5%B9%95'%0A        r
@@ -2363,16 +2363,18 @@
 %E6%96%87%E6%9C%AC%E4%BF%A1%E6%81%AF%EF%BC%8C%E8%AF%B7%E6%8C%89%5C
+n%5C
 %22%E5%BC%B9%E5%B9%95 %3C%E6%83%B3%E5%8F%91%E9%80%81
@@ -2379,16 +2379,18 @@
 %E5%8F%91%E9%80%81%E7%9A%84%E5%86%85%E5%AE%B9%3E%5C%22
+%5Cn
 %E6%A0%BC%E5%BC%8F%E5%8F%91%E5%BC%B9%E5%B9%95'%0A 

test #4