| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | """Tokyo Toshokan (A BitTorrent Library for Japanese Media)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import re | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2024-06-12 22:35:13 +02:00
										 |  |  | from searx.utils import extract_text, int_or_zero | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.tokyotosho.info/', | 
					
						
							|  |  |  |     "wikidata_id": None, | 
					
						
							|  |  |  |     "official_api_documentation": None, | 
					
						
							|  |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'HTML', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2021-05-29 16:14:19 +02:00
										 |  |  | categories = ['files'] | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | paging = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # search-url | 
					
						
							|  |  |  | base_url = 'https://www.tokyotosho.info/' | 
					
						
							|  |  |  | search_url = base_url + 'search.php?{query}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # do search-request | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |     query = urlencode({'page': params['pageno'], 'terms': query}) | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  |     params['url'] = search_url.format(query=query) | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get response from search-request | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dom = html.fromstring(resp.text) | 
					
						
							|  |  |  |     rows = dom.xpath('//table[@class="listing"]//tr[contains(@class, "category_0")]') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # check if there are no results or page layout was changed so we cannot parse it | 
					
						
							|  |  |  |     # currently there are two rows for each result, so total count must be even | 
					
						
							|  |  |  |     if len(rows) == 0 or len(rows) % 2 != 0: | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # regular expression for parsing torrent size strings | 
					
						
							| 
									
										
										
										
											2024-06-12 22:35:13 +02:00
										 |  |  |     size_re = re.compile(r'[\d.]+(T|G|M)?B', re.IGNORECASE) | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # processing the results, two rows at a time | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |     for i in range(0, len(rows), 2): | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  |         # parse the first row | 
					
						
							|  |  |  |         name_row = rows[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         links = name_row.xpath('./td[@class="desc-top"]/a') | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         params = {'template': 'torrent.html', 'url': links[-1].attrib.get('href'), 'title': extract_text(links[-1])} | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  |         # I have not yet seen any torrents without magnet links, but | 
					
						
							|  |  |  |         # it's better to be prepared to stumble upon one some day | 
					
						
							|  |  |  |         if len(links) == 2: | 
					
						
							|  |  |  |             magnet = links[0].attrib.get('href') | 
					
						
							|  |  |  |             if magnet.startswith('magnet'): | 
					
						
							|  |  |  |                 # okay, we have a valid magnet link, let's add it to the result | 
					
						
							|  |  |  |                 params['magnetlink'] = magnet | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # no more info in the first row, start parsing the second one | 
					
						
							|  |  |  |         info_row = rows[i + 1] | 
					
						
							|  |  |  |         desc = extract_text(info_row.xpath('./td[@class="desc-bot"]')[0]) | 
					
						
							|  |  |  |         for item in desc.split('|'): | 
					
						
							|  |  |  |             item = item.strip() | 
					
						
							|  |  |  |             if item.startswith('Size:'): | 
					
						
							|  |  |  |                 try: | 
					
						
							| 
									
										
										
										
											2024-06-12 22:35:13 +02:00
										 |  |  |                     params['filesize'] = size_re.search(item).group() | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |                 except:  # pylint: disable=bare-except | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  |                     pass | 
					
						
							|  |  |  |             elif item.startswith('Date:'): | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     # Date: 2016-02-21 21:44 UTC | 
					
						
							|  |  |  |                     date = datetime.strptime(item, 'Date: %Y-%m-%d %H:%M UTC') | 
					
						
							|  |  |  |                     params['publishedDate'] = date | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |                 except:  # pylint: disable=bare-except | 
					
						
							| 
									
										
										
										
											2016-03-26 19:49:57 +01:00
										 |  |  |                     pass | 
					
						
							|  |  |  |             elif item.startswith('Comment:'): | 
					
						
							|  |  |  |                 params['content'] = item | 
					
						
							|  |  |  |         stats = info_row.xpath('./td[@class="stats"]/span') | 
					
						
							|  |  |  |         # has the layout not changed yet? | 
					
						
							|  |  |  |         if len(stats) == 3: | 
					
						
							|  |  |  |             params['seed'] = int_or_zero(extract_text(stats[0])) | 
					
						
							|  |  |  |             params['leech'] = int_or_zero(extract_text(stats[1])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(params) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |