吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10466|回复: 26
收起左侧

[Python 转载] 微信公众号导购服务端

[复制链接]
y1ca 发表于 2017-9-12 01:28
本帖最后由 y1ca 于 2017-9-12 01:29 编辑

WeChatShoppin#微信公众号导购服务端

前几天赶时间 临时写的 没有进行优化 感觉写得很渣 懒得改了 写完直接丢客户了 使用还是可以的 但是真正商用我觉得还有一定差距 我使用简单的文件读写来记录各种数据 有兴趣的朋友可以自己去改一下
使用该源码需要注意以下几点微信公众 令牌 (Token)微信公众 消息加解密密钥(EncodingAESKey)微信公众 APPID一个你自己的CMS 可以使用大淘客 需要用到其中的一个Appkey然后把你的服务器地址设置到公众号上面依赖库web框架BeautifulSoupWXBizMsgCrypt
[Python] 纯文本查看 复制代码
# encoding: utf-8
import web
import urllib2
import hashlib
import json
from bs4 import BeautifulSoup
from WXBizMsgCrypt import WXBizMsgCrypt
import xml.dom.minidom
import sys
import time
import os

reload(sys)
sys.setdefaultencoding('utf-8')
H = {
    "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6"
}
# 路由信息
urls = (
    '/', 'index',
    '/findProduct.action', 'findProduct',
    '/checkSignature', 'checkSignature',
    '/coupon', 'coupon'
)

encodingAESKey = '自己写'
token = "自己写"
appid = "自己写"
daTaoKeAppKey = "自己写"

# 格式化模板信息
def fromatXml(FromUserName, ToUserName, CreateTime, Content):
    return "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + str(
        CreateTime) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + Content + "]]></Content><FuncFlag>0</FuncFlag></xml>"


# 查找商品
def queryProduct(text):
    url = "自己的大淘客地址?r=l&kw=" + text
    req = urllib2.Request(url, headers=H)
    text = urllib2.urlopen(req).read()
    soup = BeautifulSoup(text)
    list = soup.findAll("li", {"class": "theme-hover-border-color-1  g_over"})
    count = 0
    dict = {}
    for item in list:
        info = {}
        info["name"] = item.div.a.text
        info["image"] = item.img.get("src")
        info["url"] = item.a.get("href")

        data = {}
        data["pid"] = item.a.get("data-gid")
        data["info"] = info

        dict[count] = data
        count = count + 1
    return json.dumps(dict)


def getProduct(id):
    url = "http://api.dataoke.com/index.php?r=port/index&id=" + id + "&appkey="+daTaoKeAppKey+"&v=2"
    req = urllib2.Request(url, headers=H)
    text = urllib2.urlopen(req).read()
    return text


class index:
    def GET(self):
        return "What are you going to do?"


class findProduct:
    def GET(self):
        return queryProduct(web.input().query)


# 微信接口
class checkSignature:
    def __init__(self):
        self.app_root = os.path.dirname(__file__)
        self.templates_root = os.path.join(self.app_root, 'templates')
        self.render = web.template.render(self.templates_root)

    def GET(self):
        data = web.input()
        signature = data.signature
        timestamp = data.timestamp
        nonce = data.nonce
        echostr = data.echostr

        list = [token, timestamp, nonce]
        list.sort()
        sha1 = hashlib.sha1()
        map(sha1.update, list)
        hashcode = sha1.hexdigest()

        if hashcode == signature:
            return echostr

    def POST(self):
        str_xml = web.data()
        decrypt = WXBizMsgCrypt(token, encodingAESKey, appid)
        ret, decryp_xml = decrypt.DecryptMsg(str_xml, web.input().msg_signature, web.input().timestamp,
                                             web.input().nonce)
        dom = xml.dom.minidom.parseString(decryp_xml)
        root = dom.documentElement
        msgType = root.getElementsByTagName('MsgType')[0].firstChild.data
        fromUser = root.getElementsByTagName('FromUserName')[0].firstChild.data
        toUser = root.getElementsByTagName('ToUserName')[0].firstChild.data

        # 判断是否文本信息
        if msgType == 'text':
            content = root.getElementsByTagName('Content')[0].firstChild.data
            pid = None
            try:
                path = './cache/' + fromUser + '.data'
                indexpath = './cache/' + fromUser + '.index'
                # 判断商品信息是否存在
                if os.path.exists(path) and content == '换':
                    # 读取商品信息Json
                    file_object = open(path, 'r')
                    datajson = json.loads(file_object.read())
                    file_object.close()

                    # 读取索引信息
                    file_object = open(indexpath, 'r')
                    index = file_object.read()
                    file_object.close()

                    # 保存索引信息
                    file_object = open(indexpath, 'w')
                    file_object.write(str(int(index) + 1))
                    file_object.close()

                    pid = datajson[index]["pid"]
                else:
                    datajson = queryProduct(content)
                    # 创建文件
                    file_object = open(path, 'w')
                    # 保存商品信息Json
                    file_object.write(datajson)
                    file_object.close()
                    # 创建文件
                    file_object = open(indexpath, 'w')
                    # 保存索引信息
                    file_object.write('1')
                    file_object.close()
                    pid = json.loads(datajson)['0']["pid"]
                hjson = json.loads(getProduct(pid))
                print  hjson["result"]["Title"]
                return self.render.reply_pictext1(fromUser,
                                                  toUser,
                                                  int(time.time()),
                                                  hjson["result"]["Title"],
                                                  '在售价: ' + str(hjson["result"]["Org_Price"]) + '  券后价: ' + str(hjson["result"]["Price"]),
                                                  hjson["result"]["Pic"],
                                                  "http://student.javalt.cn/coupon?pid=" + hjson["result"]["ID"])
            except Exception as e:
                return fromatXml(fromUser, toUser, int(time.time()), "没有查到该商品信息 ")
        else:
            return fromatXml(fromUser, toUser, int(time.time()), "请输入正确的关键字 ")


# 跳转到领取优惠卷页
class coupon(object):
    def GET(self):
        return "<script>window.location.href='自己写网站';</script>"


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()


项目地址:https://github.com/ylca/WeChatShoppingGuide.git

免费评分

参与人数 7吾爱币 +5 热心值 +6 收起 理由
馒头稀饭豆浆 + 1 + 1 谢谢@Thanks!
newpowersky + 1 + 1 用了很久后来谢谢
Apollo233 + 1 服务器地址那个还不知道填写
arnoder + 1 + 1 用心讨论,共获提升!
pjie131 + 1 + 1 谢谢@Thanks!
雫Hao洋洋 + 1 热心回复!
qaz003 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| y1ca 发表于 2018-3-22 09:22
Apollo233 发表于 2018-3-19 21:59
然后把你的服务器地址设置到公众号上面依赖库web框架BeautifulSoupWXBizMsgCrypt

这句啥意思啊?我其他 ...

哪个!!!!
Apollo233 发表于 2018-3-22 14:03

使用该源码需要注意以下几点微信公众 令牌 (Token)微信公众 消息加解密密钥(EncodingAESKey)微信公众 APPID一个你自己的CMS 可以使用大淘客 需要用到其中的一个Appkey然后把你的服务器地址设置到公众号上面依赖库web框架BeautifulSoupWXBizMsgCrypt
代码里需要自己写的

encodingAESKey = '自己写'
token = "自己写"
appid = "自己写"
daTaoKeAppKey = "自己写"

# 查找商品
def queryProduct(text):
    url = "自己的大淘客地址?r=l&kw=" + text

是下面这样填吗?

encodingAESKey = '自己写微信公众号后台的'
token = "自己写微信公众号后台的"
appid = "自己写这个也是指微信公众号后台的吗"
daTaoKeAppKey = "自己写大淘客里的"

# 查找商品
def queryProduct(text):
    url = "自己的大淘客地址,指的是自己的域名,是吧?r=l&kw=" + text


微信公众号后台,填写
URL
这个填写自己的域名吗?
还是自己的域名/index.php?checkSignature=自己填的Token



具体怎么填,还望大佬多多指教,还有文件用TXT编辑后改为什么文件名?index1.php可以吗
shenhuawade 发表于 2017-9-12 07:01
头像被屏蔽
大象无形 发表于 2017-9-12 07:04
提示: 作者被禁止或删除 内容自动屏蔽
tufei 发表于 2017-9-12 07:37
学习了····谢谢楼主····
meng-tian 发表于 2017-9-12 08:08
写出来就不错了···嘿嘿
屿圪 发表于 2017-9-12 09:01
学习了 谢谢楼主分享
小样丶站住 发表于 2017-9-12 09:10
python 就是简洁 直接明了
chen180 发表于 2017-9-12 10:24
厉害,,!1
m0216 发表于 2017-9-12 18:09
不错  要是有成品就更好了  嘿嘿
Xinshao 发表于 2017-9-14 21:10 来自手机
群主。我想问问其中的大淘客地址怎么改?又怎么添加在公众号里面。谢谢了。第一次用这个
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-2 01:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表