-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyaafeed.py
222 lines (174 loc) · 7.34 KB
/
nyaafeed.py
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
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
222
import json
import os
import re
import sys
import requests
import time
from datetime import datetime
def load_config():
with open('nyaaconfig.json', 'r') as f:
return json.load(f)
def fetch_data(api_link, retries=2, delay=3):
"""Fetch data from the API link with retries."""
for attempt in range(retries + 1): # Add 1 to include the initial attempt
try:
response = requests.get(api_link, timeout=10)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
print(f"Failed to fetch data from {api_link}, attempt {attempt + 1}/{retries + 1}, error: {e}")
if attempt < retries:
print(f"Retrying in {delay} seconds...")
time.sleep(delay)
print(f"Skipping {api_link} after {retries + 1} failed attempts.")
return None
def extract_text(pattern, text):
match = re.search(pattern, text)
return match.group(1) if match else None
def process_item(item_text):
# Define patterns to extract elements
patterns = {
'title': r'<title>(.*?)</title>',
'link': r'<link>(.*?)</link>',
'guid': r'<guid[^>]*>(.*?)</guid>',
'pubDate': r'<pubDate>(.*?)</pubDate>',
'seeders': r'<(?:ns0:|nyaa:)?seeders>(.*?)</(?:ns0:|nyaa:)?seeders>',
'leechers': r'<(?:ns0:|nyaa:)?leechers>(.*?)</(?:ns0:|nyaa:)?leechers>',
'downloads': r'<(?:ns0:|nyaa:)?downloads>(.*?)</(?:ns0:|nyaa:)?downloads>',
'size': r'<(?:ns0:|nyaa:)?size>(.*?)</(?:ns0:|nyaa:)?size>',
'description': r'<description>(.*?)</description>',
'trusted': r'<(?:ns0:|nyaa:)?trusted>(.*?)</(?:ns0:|nyaa:)?trusted>',
'remake': r'<(?:ns0:|nyaa:)?remake>(.*?)</(?:ns0:|nyaa:)?remake>'
}
item_data = {}
for key, pattern in patterns.items():
item_data[key] = extract_text(pattern, item_text)
return item_data
def parse_xml(xml_content):
items = re.findall(r'<item.*?>(.*?)</item>', xml_content, re.DOTALL)
entries = []
for item_text in items:
item_data = process_item(item_text)
if (item_data['title'] and item_data['link'] and item_data['guid'] and
item_data['pubDate'] and item_data['size'] and item_data['seeders'] and
item_data['leechers'] and item_data['downloads']):
entries.append(item_data)
else:
print(f"Skipping item due to missing elements: {item_text[:100]}...") # Print a snippet for debugging
return entries
def filter_entries(entries, include_regex, exclude_regex):
filtered_entries = []
include_pattern = re.compile(include_regex, re.IGNORECASE) if include_regex else None
exclude_pattern = re.compile(exclude_regex, re.IGNORECASE) if exclude_regex else None
for entry in entries:
title = entry.get("title", "")
if include_pattern and not include_pattern.search(title):
continue
if exclude_pattern and exclude_pattern.search(title):
continue
filtered_entries.append(entry)
return filtered_entries
def load_existing_xml(xml_file_name):
file_path = os.path.join('rssfeed', xml_file_name)
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
return None
def extract_existing_ids(existing_xml):
if not existing_xml:
return set()
id_pattern = re.compile(r'<guid isPermaLink="true">https://nyaa\.si/view/(\d+)</guid>')
return set(id_pattern.findall(existing_xml))
def create_xml_entries(feed_name, feed_link, entries, existing_ids, domain):
new_entries = []
for entry in entries:
entry_id = str(entry['guid'].split('/')[-1])
if entry_id in existing_ids:
continue
title = entry['title']
torrent_url = entry['link']
pub_date = entry['pubDate']
size_str = entry['size']
size_value, size_unit = re.match(r'([0-9.]+)\s*(MiB|GiB)', size_str).groups()
size_mb = float(size_value) * (1024 if size_unit == 'GiB' else 1) # Convert GiB to MiB
seeders = entry.get('seeders', 0)
leechers = entry.get('leechers', 0)
downloads = entry.get('downloads', 0)
trusted = entry.get('trusted', 'Unknown')
remake = entry.get('remake', 'Unknown')
# Replace the domain in the torrent URL
torrent_url = re.sub(r'https://nyaa\.si/', f'{domain}/', torrent_url)
hyperlink = f'<a href="{entry["guid"]}">{title}</a>'
description_content = (
f"{size_mb:.2f} MiB | Seeders: {seeders} | Leechers: {leechers} | "
f"Downloads: {downloads} | Nyaa: {entry_id} | Trusted: {trusted} | Remake: {remake} | {hyperlink}"
)
new_entry = f"""
<item>
<guid isPermaLink="true">{entry['guid']}</guid>
<title><![CDATA[{title}]]></title>
<link>{torrent_url}</link>
<pubDate>{pub_date}</pubDate>
<description><![CDATA[{description_content}]]></description>
</item>"""
new_entries.append(new_entry.strip())
return new_entries
def merge_xml_data(existing_xml, new_entries, feed_name, feed_link):
if existing_xml is None:
existing_xml = f"""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>{feed_name}</title>
<link>{feed_link}</link>
</channel>
</rss>"""
channel_start = existing_xml.find("<channel>")
channel_end = existing_xml.find("</channel>")
if channel_start == -1 or channel_end == -1:
raise ValueError("Invalid XML structure: Missing <channel> tags.")
merged_xml = (
existing_xml[:channel_end] +
"\n".join(new_entries) +
"\n" +
existing_xml[channel_end:]
)
return merged_xml.strip()
def update_feed(feed):
existing_xml = load_existing_xml(feed['xml_file_name'])
existing_ids = extract_existing_ids(existing_xml)
all_new_entries = []
print(f"Processing feed '{feed['name']}'...")
xml_data = fetch_data(feed['api_link'])
if xml_data:
entries = parse_xml(xml_data)
filtered_entries = filter_entries(
entries,
feed['include_regex'],
feed['exclude_regex']
)
domain = feed['link'].rstrip('/') # Ensure no trailing slash
new_entries = create_xml_entries(feed['name'], feed['link'], filtered_entries, existing_ids, domain)
existing_ids.update(entry['guid'].split('/')[-1] for entry in filtered_entries)
all_new_entries.extend(new_entries)
print(f"Processed {len(new_entries)} new entries.")
if all_new_entries:
merged_xml = merge_xml_data(existing_xml, all_new_entries, feed['name'], feed['link'])
file_path = os.path.join('rssfeed', feed['xml_file_name'])
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(merged_xml)
print(f"Total new entries merged: {len(all_new_entries)}")
else:
print("No new entries to merge. The XML file was not modified.")
def main():
config = load_config()
feeds = config['feeds']
if not feeds:
print("No feeds available in the configuration.")
sys.exit(1)
for feed in feeds:
update_feed(feed)
time.sleep(3) # Add a 3-second delay between processing each feed
print("All feeds updated successfully.")
if __name__ == "__main__":
main()