forked from 8ei/offcloud2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_nyaa.py
More file actions
155 lines (139 loc) · 6.52 KB
/
rss_nyaa.py
File metadata and controls
155 lines (139 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import logging
import urllib
import xml.etree.ElementTree as ET
# third-party
import requests
# sjva 공용
from framework import logger, py_urllib2
# 패키지
from .plugin import logger, package_name
# 로그
#########################################################
class RssUtil(object):
@staticmethod
def get_rss(url):
try:
logger.debug('get_rss : %s', url)
"""
req = py_urllib2.Request(url)
req.add_header('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36')
resp = py_urllib2.urlopen(req)
tree = ET.ElementTree(file=resp)
"""
text = requests.get(url, headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'}).text
#logger.warning(text)
try:
root = ET.fromstring(text)
except ET.ParseError as e:
print(f"XML 파싱 에러: {e}")
else:
namespaces = {'nyaa': 'https://nyaa.si/xmlns/nyaa'} # 'nyaa' 네임스페이스 정의
#logger.warning(tree)
#root = tree.getroot()
item_list = root.findall('.//channel/item')
logger.debug('xml item count:%s', len(item_list))
ret = []
for item in item_list:
try:
info_hash_element = item.find('nyaa:infoHash', namespaces=namespaces)
if info_hash_element is not None and info_hash_element.text:
link = "magnet:?xt=urn:btih:" + info_hash_element.text
else:
link = item.find('link').text.strip()
if link.startswith('magnet'):
try:
link = link[0:60]
except:
logger.error(e)
logger.error(traceback.format_exc())
link = item.find('link').text.strip()
rss = Feed(item.find('title').text.strip(), link)
ret.append(rss)
except Exception as exception:
#logger.debug(item.find('title').text)
#logger.debug(item.find('link').text)
logger.debug(exception)
logger.debug(traceback.format_exc())
return ret
except Exception as exception:
logger.debug(exception)
logger.debug(traceback.format_exc())
logger.debug('url:%s', url)
return None
"""
@staticmethod
def make_rss(title, rss_list, torrent_mode, ddns, is_bot=False):
xml = '<rss xmlns:showrss=\"http://showrss.info/\" version=\"2.0\">\n'
xml += '\t<channel>\n'
xml += '\t\t<title>' + '%s</title>\n' % title
xml += '\t\t<link></link>\n'
xml += '\t\t<description></description>\n'
for bbs in rss_list:
for download in bbs.files:
try:
item_str = '\t\t<item>\n'
if download.filename.lower().endswith('.smi') or download.filename.lower().endswith('.srt') or download.filename.lower().endswith('.ass') or download.filename.lower().endswith('.ssa') or download.filename.lower().endswith('.sub'):
tmp = '\t\t\t<title>[자막]%s</title>\n' % TorrentSite.replace_xml(bbs.title)
else:
tmp = '\t\t\t<title>%s</title>\n' % TorrentSite.replace_xml(bbs.title)
item_str += tmp
if torrent_mode == 'magnet' and download.is_torrent:
item_str += '\t\t\t<link>%s</link>\n' % download.magnet
else:
if is_bot:
item_str += '\t\t\t<link>%s/rss/download_bot/%s</link>\n' % (ddns, download.id)
else:
item_str += '\t\t\t<link>%s/rss/download/%s</link>\n' % (ddns, download.id)
item_str += '\t\t\t<description>%s</description>\n' % TorrentSite.replace_xml(download.filename)
date_str = bbs.created_time.strftime("%a, %d %b %Y %H:%M:%S") + ' +0900'
item_str += '\t\t\t<pubDate>%s</pubDate>\n' % date_str
item_str += '\t\t</item>\n'
xml += item_str
except Exception as exception:
logger.debug('Exception:%s', exception)
logger.debug(traceback.format_exc())
xml += '\t</channel>\n'
xml += '</rss>'
return xml
"""
@staticmethod
def replace_xml(xml):
tmp = [['&', '&'], ['<', '<'], ['>', '>'], ['‘', '''], ['"', '"']]
for t in tmp:
xml = xml.replace(t[0], t[1])
return xml
@staticmethod
def make_rss(package_name, rss_list):
#from system.model import ModelSetting as SystemModelSetting
#ddns = SystemModelSetting.get('ddns')
xml = '<rss xmlns:showrss=\"http://showrss.info/\" version=\"2.0\">\n'
xml += '\t<channel>\n'
xml += '\t\t<title>' + '%s</title>\n' % package_name
xml += '\t\t<link></link>\n'
xml += '\t\t<description></description>\n'
for bbs in rss_list:
try:
item_str = '\t\t<item>\n'
tmp = '\t\t\t<title>%s</title>\n' % RssUtil.replace_xml(bbs['title'])
item_str += tmp
#item_str += '\t\t\t<link>%s</link>\n' % bbs['link']
item_str += '\t\t\t<link>%s</link>\n' % RssUtil.replace_xml(bbs['link'])
date_str = bbs['created_time'].strftime("%a, %d %b %Y %H:%M:%S") + ' +0900'
item_str += '\t\t\t<pubDate>%s</pubDate>\n' % date_str
item_str += '\t\t</item>\n'
xml += item_str
except Exception as exception:
logger.debug('Exception:%s', exception)
logger.debug(traceback.format_exc())
xml += '\t</channel>\n'
xml += '</rss>'
return xml
class Feed(object):
def __init__(self, title, link):
self.title = title
self.link = link