random

- collection of un-sorted bollocks
git clone git://git.acid.vegas/random.git
Log | Files | Refs | Archive

librescrape.py (2032B)

      1 #/usr/bin/env python
      2 # LibreScrape - developed by acidvegas in python (https://git.acid.vegas/librescrape)
      3 
      4 import urllib.request
      5 import urllib.parse
      6 import json
      7 import random
      8 import ssl
      9 
     10 context = ssl._create_unverified_context()
     11 
     12 def get_instances() -> list:
     13 	'''Get instances from LibreY and Librex'''
     14 	instances = list()
     15 	sources = ['https://raw.githubusercontent.com/Ahwxorg/LibreY/main/instances.json',]
     16 	for item in sources:
     17 		data = json.loads(urllib.request.urlopen(item).read().decode())
     18 		instances += [item['clearnet'] for item in data['instances']]
     19 	return instances
     20 
     21 def search(instance, query, page=1, type=0):
     22 	'''
     23 	Search for a query on a Libre instance
     24 
     25 	:param instance: The instance to search on
     26 	:param query: The query to search for
     27 	:param page: The page number to search on (first page is 0)
     28 	:param type: The type of search to perform (0=text, 1=image, 2=video, 3=torrent, 4=tor)
     29 
     30 	API will return a list of results in the following format:
     31 		[
     32 			{
     33 				"title": "Title of the post",
     34 				"url": "URL of the post",
     35 				"baseurl": "Base URL of the instance",
     36 				"description": "Description of the post"
     37 			},
     38 		]
     39 	'''
     40 	results = urllib.request.urlopen(f'{instance}api.php?q={query}&p={page}&t={type}', context=context, timeout=5).read().decode()
     41 	return json.loads(results)
     42 
     43 if __name__ == '__main__':
     44 	found = list()
     45 	query = input('Search query: ')
     46 	query = urllib.parse.quote(query)
     47 	latest_instances = get_instances()
     48 	with open('output.log', 'w') as out:
     49 		for i in range(100):
     50 			complete = False
     51 			while complete is False:
     52 				instance = random.choice(latest_instances)
     53 				try:
     54 					results = search(instance, query, page=i)
     55 					for result in results:
     56 						title = result['title']
     57 						title = title.replace('\n','')
     58 						while '  ' in title:
     59 							title = title.replace('  ',' ')
     60 						print(f'{title} - {result["url"]}')
     61 						out.write(f'{title} - {result["url"]}\n')
     62 				except Exception as ex:
     63 					print('!!!' + str(ex))
     64 					print(results)
     65 					print('')
     66 				else:
     67 					completed = True