zixing131 / Pys60_Simulator

Pys60_Simulator
25 stars 4 forks source link

it之家的api加密算法 #3

Closed LiuChangFreeman closed 4 years ago

LiuChangFreeman commented 4 years ago

你需要保证pycrypto的des加密能够运行在你的机器上


# -*- coding: utf-8 -*-
from __future__ import print_function
from Crypto.Cipher import DES
import binascii
import requests
import json
import hashlib
import base64

def make_up_data(data,mode):
    pad = 8 - len(data) % 8
    pad_str = ""
    if mode=="PAD_PKCS5":
        for i in range(pad):
            pad_str +=chr(pad)
    elif mode=="PAD_ZERO":
        for i in range(pad):
            pad_str +=chr(0)
    return data + pad_str

def get_sn(article_id):
    secret_key = "(#i@x*l%"
    des = DES.new(secret_key, DES.MODE_ECB)
    data = make_up_data(article_id, "PAD_ZERO")
    data = des.encrypt(data)
    return binascii.b2a_hex(data)

def get_time_hash(time):
    secret_key = "w^s(1#a@"
    des = DES.new(secret_key, DES.MODE_ECB)
    data = make_up_data(time, "PAD_ZERO")
    data = des.encrypt(data)
    return binascii.b2a_hex(data)

def get_next(time_str):
    if "." in time_str:
        time_str=time_str.split(".")[0]
    time_array = time.strptime(time_str, "%Y-%m-%dT%H:%M:%S")
    time_stamp = int(time.mktime(time_array))
    hash=get_time_hash("{}000".format(time_stamp))
    url="https://api.ithome.com/json/listpage/news/{}".format(hash)
    headers={}
    headers["User-Agent"]="Mozilla/5.0 (Linux; Android 7.0; OPPO R9m Build/XQG58I) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.731.88 Mobile Safari/537.36"
    headers["Connection"] = "Keep-Alive"
    headers["Accept-Encoding"] = "gzip"
    response=requests.get(url,headers=headers,timeout=10)
    result=response.json()
    return result

def get_hotcomments(article_id,ua):
    sn=get_sn(article_id)
    url="http://dyn.ithome.com/api/comment/ghcl?sn={}".format(sn)
    headers = {}
    headers["User-Agent"]=ua
    headers["Connection"] = "Keep-Alive"
    headers["Accept-Encoding"] = "gzip"
    response = requests.get(url, headers=headers, timeout=10)
    result = response.json()
    return result

def get_comments(article_id,ua,cid=None):
    sn=get_sn(article_id)
    if cid:
        url = "https://dyn.ithome.com/api/comment/getnewscomment?sn={}&cid={}".format(sn,cid)
    else:
        url = "https://dyn.ithome.com/api/comment/getnewscomment?sn={}".format(sn)
    headers = {}
    headers["User-Agent"]=ua
    headers["Connection"] = "Keep-Alive"
    headers["Accept-Encoding"] = "gzip"
    response = requests.get(url, headers=headers, timeout=10)
    result = response.json()
    return result
zixing131 commented 4 years ago

感谢你的分享,pys60上面有可用的des模块。