python-sfacg

 · 2019-5-3 · 次阅读


import re
import urllib.request
import json
import os
from selenium import webdriver
import pytesseract
def login():
    browser = webdriver.Chrome()
    browser.get("https://passport.sfacg.com/Login.aspx")
    w=1
    while w==1:
        cookies = browser.get_cookies()
        for i in cookies:
            if i['name']=='.SFCommunity':
                cookie=i["value"]
                w=0
                break
    browser.quit()

    return cookie
def sfnext(url):
    html=urllib.request.urlopen(url).read().decode("utf-8")
    xyz1 =re.search('class="btn normal">下一章</a>', html, flags=0).span()
    xyz1=xyz1[0]
    xyz=html[:xyz1]
    xyz=xyz[-40:]
    xyz2=re.search('=',xyz,flags=0).span()
    xyz2=xyz2[1]
    xyz=xyz[xyz2:]
    xyz=xyz[1:][:-2]
    xyz="http://book.sfacg.com"+xyz
    return xyz
def sflast(url):
    html=urllib.request.urlopen(urllib.request.Request(url)).read().decode("utf-8")
    syz1 = re.search('class="btn normal">上一章</a>', html, flags=0    ).span()
    syz1=syz1[0]
    syz=html[:syz1]
    syz=syz[-200:]
    syz2=re.search('<a href=',syz,flags=0).span()
    syz2=syz2[1]
    syz=syz[syz2:]
    syz=syz[1:][:-2]
    syz="http://book.sfacg.com"+syz
    return syz
def gettext(url,cookie):
    url="https://api.sfacg.com"+url
    request = urllib.request.Request(url,headers={
    "Accept-Charset": "UTF-8",
    "Authorization": "Basic YW5kcm9pZHVzZXI6MWEjJDUxLXl0Njk7KkFjdkBxeHE=",
    "Accept": "application/vnd.sfacg.api+json;version=1",
    "User-Agent": "boluobao/3.7.04(android;17)/HomePage",
    "SFSecurity": "nonce=B3E9066C-572E-45B9-AD5A-5035DA7790BC&timestamp=1554014488870&devicetoken=752ADE55-8262-3737-8ACD-422CD57E482A&sign=16B0E8214B70FF272A22D761878FAC6B",
    "Host": "api.sfacg.com",
    "Connection": "Keep-Alive",
    "Accept-Encoding": "gzip",
    'Cookie':".SFCommunity="+cookie
    })

    response = urllib.request.urlopen(request)
    text=response.read().decode()
    return(text)
def getid(url):
    url=url.split("/")
    for i in url:
        if i == '':
            url.remove(i)
    url=url[-1:]
    url=url[0]
    return url
def dir(bookid,cookie):
    data=json.loads(gettext("/novels/"+str(bookid)+"/dirs?expand=originNeedFireMoney",cookie))
    data=data['data']['volumeList']
    gn=0
    cl=[]
    print(40*'*'+'选择卷'+40*'*')
    for i in data:
        lt=i['title']
        gn+=1
        print(str(gn),lt)
        cl.append(i["chapterList"])
    i=int(input("输入编号:"))
    o=cl[i-1]
    #print(o)
    id=[]
    ln=0
    print(40*'*'+'选择章'+40*'*')
    for x in o:
        ln+=1
        title=x['title']
        bid=x['chapId']
        id.append(bid)
        print(ln,title)
    iid=int(input("[输入编号]"))
    id=id[iid-1]
    return [id,i,iid,bookid,gn,ln]
    #print(data)
def so_dir(bookid,gid,zid,cookie):
    data=json.loads(gettext("/novels/"+str(bookid)+"/dirs?expand=originNeedFireMoney",cookie))
    data=data['data']['volumeList']
    gn=0
    cl=[]
    for i in data:
        gn+=1
        cl.append(i["chapterList"])
    o=cl[int(gid)-1]
    id=[]
    ln=0
    for x in o:
        ln+=1
        bid=x['chapId']
        id.append(bid)
    id=id[int(zid)-1]
    return id
def sfpockets(cookie):
    ret=json.loads(gettext("/user/Pockets?expand=novels%2Calbums%2Ccomics%2Cdiscount%2CdiscountExpireDate",cookie))
    ret=ret['data']    
    pocket_book_name=[]
    pocket_book_id=[]
    pocket_book_writer=[]
    #book_num=0
    for i in ret:
        if "novels" in i['expand']:
            for x in i['expand']['novels']:
                pocket_book_name.append(x['novelName'])
                pocket_book_id.append(x['novelId'])
                pocket_book_writer.append(x['authorName'])
                #book_num+=1

    ret={
        'pocket_book_id':pocket_book_id,
        'pocket_book_name':pocket_book_name,
        'pocket_book_writer':pocket_book_writer,
        #'book_num':book_num
    }
    return ret
def sftext(id,cookie):
    xx=json.loads(gettext("/Chaps/"+str(id)+'?expand=content%2CneedFireMoney%2CoriginNeedFireMoney%2Ctsukkomi&autoOrder=false',cookie))
    #print(xx)
    title=xx['data']['title']
    text=xx['data']['expand']['content']
    text="*"*50+title+'*'*50+"\r\n\r\n  "+re.sub('\\r\\n',"\r\n   ",text)
    return text
def sf_url_mode(url,cookie):
    return sftext(getid(url),cookie)

def sf_pocket_mode(cookie):
    pocket=sfpockets(cookie)
    #pocketnum=pocket["book_num"]
    pocketname=pocket["pocket_book_name"]
    pocketid=pocket['pocket_book_id']
    num=0
    for i in pocketname:
        num+=1
        print(str(num),":",i)
    innum=int(input("[输入编号]"))
    bookid=pocketid[innum-1]
    ret=dir(bookid,cookie)
    return ret
    '''
def main():
    cookie="A0D82612B107A00D97D3B4186640FB48EC8157F6406B1E6D94C633CC69953C24652F5FD90A6BB16125562344C929590D2996A899F2BB49F47055DDF0F7D1346E118E013243A7023B202D56FE5FCCDAC997CB44189BA4D56A2F7D4F88EBA9ADD6"
    print(sfpocket_mode(cookie))
    '''
def __main__():
    os.system("color F0")
    if os.path.exists('cookie.ini') == True:
        with open("cookie.ini",'r')as f:
            cookie=f.read()
    if os.path.exists('cookie.ini') == False:
        with open('cookie.ini','w')as f:
            cookie=login()
            f.write(cookie)
    mode=input("[请输入运行模式:(p:进入火袋,u:链接模式]")
    if mode == 'p':
        i=sf_pocket_mode(cookie)
        id=i[0]
    elif mode == 'u':
        url = input("[请输入sf链接]")
        id=getid(url)
        print(sftext(id,cookie))
    while True:
        print(sftext(id,cookie))
        tj = input("[是否继续?上一章输入l,下一章输入n 退出输入q,更新cookie输入c,重输链接输入r,, 输入其他刷新]")
        if tj == 'q':
            break
        elif tj =='c':

            with open('cookie.ini','w')as f:
                cookie=login()
                f.write(cookie)
        elif tj =='n':
            if int(i[3])>int(i[5]):
                id=so_dir(i[3],int(i[1])+1,1,cookie)

                i=[id,i[1],int(i[2])+1,1]
            else:
                id=so_dir(i[3],i[1],int(i[2])+1,cookie)
                i=[id,i[1],int(i[2])+1,i[3]]
        elif tj =='l':
            if (int(i[3]) < 1 ): 
                id=so_dir(i[3],i[1],int(i[5],cookie)
                i=[id,i[1],i[5],i[3]]
            #else:
             #   id=so_dir(i[3],i[1],int(i[2])-1,cookie)
              #  i=[id,i[1],int(i[2])-1,i[3]]
        else :
            print("[刷新中]")
if __name__ == "__main__":
    __main__()