cms.py 8.7 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : cms.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/8/25
H
hjdhnx 已提交
6 7
import requests

H
hjdhnx 已提交
8
from utils.web import *
H
hjdhnx 已提交
9 10
from utils.config import config
from utils.htmlParser import jsoup
H
hjdhnx 已提交
11
from urllib.parse import urljoin
H
hjdhnx 已提交
12 13 14

class CMS:
    def __init__(self,rule):
H
hjdhnx 已提交
15
        self.url = rule.get('url','').rstrip('/')
H
hjdhnx 已提交
16
        self.detailUrl = rule.get('detailUrl','').rstrip('/')
H
hjdhnx 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
        self.searchUrl = rule.get('searchUrl','')
        ua = rule.get('ua','')
        if ua == 'MOBILE_UA':
            self.ua = MOBILE_UA
        elif ua == 'PC_UA':
            self.ua = PC_UA
        else:
            self.ua = UA
        self.searchUrl = rule.get('searchUrl','')
        self.class_name = rule.get('class_name','')
        self.class_url = rule.get('class_url','')
        self.一级 = rule.get('一级','')
        self.二级 = rule.get('二级','')
        self.搜索 = rule.get('搜索','')
        self.title = rule.get('title','')
H
hjdhnx 已提交
32 33
        self.filter = rule.get('filter',[])
        self.extend = rule.get('extend',[])
H
hjdhnx 已提交
34 35 36 37

    def getName(self):
        return self.title

H
hjdhnx 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    def homeContent(self):
        # yanaifei
        # https://yanetflix.com/vodtype/dianying.html
        result = {}
        class_names = self.class_name.split('&')
        class_urls = self.class_url.split('&')
        cnt = min(len(class_urls),len(class_names))
        classes = []
        for i in range(cnt):
            classes.append({
                'type_name': class_names[i],
                'type_id': class_urls[i]
            })
        result['class'] = classes
        if self.filter:
            result['filters'] = config['filter']
        return result

    def homeVideoContent(self):
        rsp = self.fetch("https://www.genmov.com/", headers=self.header)
        root = self.html(rsp.text)
        aList = root.xpath("//div[@class='module module-wrapper']//div[@class='module-item']")
        videos = []
        for a in aList:
            name = a.xpath(".//div[@class='module-item-pic']/a/@title")[0]
            pic = a.xpath(".//div[@class='module-item-pic']/img/@data-src")[0]
            mark = a.xpath("./div[@class='module-item-text']/text()")[0]
            sid = a.xpath(".//div[@class='module-item-pic']/a/@href")[0]
            sid = self.regStr(sid, "/video/(\\S+).html")
            videos.append({
                "vod_id": sid,
                "vod_name": name,
                "vod_pic": pic,
                "vod_remarks": mark
            })
        result = {
            'list': videos
        }
        return result

    def categoryContent(self, fyclass, fypage):
        """
        一级带分类的数据返回
        :param fyclass: 分类标识
        :param fypage: 页码
        :return: cms一级数据
        """

        result = {}
        # urlParams = ["", "", "", "", "", "", "", "", "", "", "", ""]
        # urlParams = [""] * 12
        # urlParams[0] = tid
        # urlParams[8] = str(pg)
        # for key in self.extend:
        #     urlParams[int(key)] = self.extend[key]
        # params = '-'.join(urlParams)
        # print(params)
        # url = self.url + '/{0}.html'.format(params)
H
hjdhnx 已提交
96 97
        pg = str(fypage)
        url = self.url.replace('fyclass',fyclass).replace('fypage',pg)
H
hjdhnx 已提交
98 99 100 101 102 103 104 105
        print(url)
        headers = {'user-agent': self.ua}
        r = requests.get(url, headers=headers)
        p = self.一级.split(';')  # 解析
        jsp = jsoup(self.url)
        pdfh = jsp.pdfh
        pdfa = jsp.pdfa
        pd = jsp.pd
H
hjdhnx 已提交
106 107 108
        # print(pdfh(r.text,'body a.module-poster-item.module-item:eq(1)&&Text'))
        # print(pdfh(r.text,'body a.module-poster-item.module-item:eq(0)'))
        # print(pdfh(r.text,'body a.module-poster-item.module-item:first'))
H
hjdhnx 已提交
109 110 111 112 113 114 115 116
        items = pdfa(r.text, p[0])
        videos = []
        for item in items:
            # print(item)
            title = pdfh(item, p[1])
            img = pd(item, p[2])
            desc = pdfh(item, p[3])
            link = pd(item, p[4])
H
hjdhnx 已提交
117
            content = '' if len(p) < 6 else pdfh(item, p[5])
H
hjdhnx 已提交
118 119 120 121 122 123 124 125 126
            # sid = self.regStr(sid, "/video/(\\S+).html")
            videos.append({
                "vod_id": link,
                "vod_name": title,
                "vod_pic": img,
                "vod_remarks": desc,
                "vod_content": content,
            })
        result['list'] = videos
H
hjdhnx 已提交
127
        result['page'] = fypage
H
hjdhnx 已提交
128 129 130 131 132
        result['pagecount'] = 9999
        result['limit'] = 90
        result['total'] = 999999
        return result

H
hjdhnx 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    def detailContent(self, array):
        """
        cms二级数据
        :param array:
        :return:
        """
        # video-info-header
        fyid = array[0]
        url = self.detailUrl.replace('fyid', fyid)
        print(url)
        headers = {'user-agent': self.ua}
        r = requests.get(url, headers=headers)
        html = r.text
        # print(html)
        p = self.二级  # 解析
        jsp = jsoup(self.url)
        pdfh = jsp.pdfh
        pdfa = jsp.pdfa
        pd = jsp.pd
        pq = jsp.pq
        obj = {}
        vod_name = ''
        if p.get('title'):
            p1 = p['title'].split(';')
            vod_name = pdfh(html,p1[0]).replace('\n',' ')
            title = '\n'.join([pdfh(html,i).replace('\n',' ') for i in p1])
            # print(title)
            obj['title'] = title
        if p.get('desc'):
            p1 = p['desc'].split(';')
            desc = '\n'.join([pdfh(html,i).replace('\n',' ') for i in p1])
            obj['desc'] = desc

        if p.get('content'):
            p1 = p['content'].split(';')
            content = '\n'.join([pdfh(html,i).replace('\n',' ') for i in p1])
            obj['content'] = content

        if p.get('img'):
            p1 = p['img'].split(';')
            img = '\n'.join([pdfh(html,i).replace('\n',' ') for i in p1])
            obj['img'] = img

        vod = {
            "vod_id": fyid,
            "vod_name": vod_name,
            "vod_pic": obj.get('img',''),
            "type_name": obj.get('title',''),
            "vod_year": "",
            "vod_area": "",
            "vod_remarks": obj.get('desc',''),
            "vod_actor": "",
            "vod_director": "",
            "vod_content": obj.get('content','')
        }

        vod_play_from = '$$$'
        playFrom = []
        if p.get('tabs'):
            vodHeader = pdfa(html,p['tabs'])
            vodHeader = [pq(v).text() for v in vodHeader]
        else:
            vodHeader = ['道长在线']

        for v in vodHeader:
            playFrom.append(v)
        vod_play_from = vod_play_from.join(playFrom)

        vod_play_url = '$$$'
        vod_tab_list = []
        if p.get('lists'):
            for i in range(len(vodHeader)):
               p1 = p['lists'].replace('#id',str(i))
               vodList = pdfa(html,p1) # 1条线路的选集列表
               vodList = [pq(i).text()+'$'+pd(i,'a&&href') for i in vodList]  # 拼接成 名称$链接
               vlist = '#'.join(vodList) # 拼多个选集
               vod_tab_list.append(vlist)
            vod_play_url = vod_play_url.join(vod_tab_list)
        # print(vod_play_url)
        vod['vod_play_from'] = vod_play_from
        vod['vod_play_url'] = vod_play_url

        result = {
            'list': [
                vod
            ]
        }
        return result

H
hjdhnx 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    def searchContent(self, key, fypage=1,quick=1):
        pg = str(fypage)
        url = self.searchUrl.replace('**', key).replace('fypage',pg)
        if not str(url).startswith('http'):
            url = urljoin(self.url,url)
        print(url)
        headers = {'user-agent': self.ua}
        r = requests.get(url, headers=headers)
        html = r.text
        p = self.搜索.split(';')  # 解析
        jsp = jsoup(self.url)
        pdfh = jsp.pdfh
        pdfa = jsp.pdfa
        pd = jsp.pd
        pq = jsp.pq
        items = pdfa(html, p[0])
        videos = []
        for item in items:
            # print(item)
            title = pdfh(item, p[1])
            img = pd(item, p[2])
            desc = pdfh(item, p[3])
            link = pd(item, p[4])
            content = '' if len(p) < 6 else pdfh(item, p[5])
            # sid = self.regStr(sid, "/video/(\\S+).html")
            videos.append({
                "vod_id": link,
                "vod_name": title,
                "vod_pic": img,
                "vod_remarks": desc,
                "vod_content": content,
            })
        result = {
            'list': videos
        }
        return result

H
hjdhnx 已提交
259 260 261 262 263 264
if __name__ == '__main__':
    from utils import parser
    js_path = f'js/鸭奈飞.js'
    ctx, js_code = parser.runJs(js_path)
    rule = ctx.eval('rule')
    cms = CMS(rule)
H
hjdhnx 已提交
265
    print(cms.title)
H
hjdhnx 已提交
266 267
    # print(cms.homeContent())
    # cms.categoryContent('dianying',1)
H
hjdhnx 已提交
268 269
    # print(cms.detailContent(['67391']))
    print(cms.searchContent('斗罗大陆'))