Files
scripts/check-ip-seeds.py
Bogomil Vasilev 1ca024226e cleanup
Signed-off-by: Bogomil Vasilev <smirky@smirky.net>
2026-02-13 14:12:17 +02:00

83 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python
import sys
import json
import urllib
import urllib.request as urllib
from html.parser import HTMLParser
from termcolor import colored
class MyHTMLParser(HTMLParser):
tbody = False
div = False
a_href = False
timestamp = False
timestamps = []
category = False
category_value = None
def handle_starttag(self, tag, attrs):
if tag == 'tbody':
self.tbody = True
elif tag == 'div' and self.tbody:
self.div = True
elif tag == 'a' and self.tbody and self.div:
self.a_href = True
if tag == 'td' and self.tbody:
for attr in attrs:
if len(attr) == 2 and attr[0] == 'class':
if attr[1] == 'date-column':
self.timestamp = True
elif attr[1] == 'category-column':
self.category = True
def get_category(self):
colored_category = None
if self.category_value == 'Movies':
colored_category = colored(self.category_value, 'cyan', attrs=['bold'])
elif self.category_value == 'Books':
colored_category = colored(self.category_value, 'green', attrs=['bold'])
elif self.category_value == 'XXX':
colored_category = colored(self.category_value, 'magenta', attrs=['bold'])
else:
colored_category = self.category_value
return f'{colored_category}{(10-len(self.category_value)) * " "}'
def store_category(self, data):
self.category_value = data.strip()
self.category = False
def handle_endtag(self, tag):
if tag == 'tbody':
self.tbody = self.div = self.a_href = False
if tag == 'div':
self.div = self.a_href = False
if tag == 'a_href':
self.a_href = False
def handle_data(self, data):
if self.tbody and self.div and self.a_href:
print(' \t| '.join(self.timestamps) + '\t| ' + self.get_category() + '| ' + data.strip())
self.a_href = False
self.timestamps = []
elif self.timestamp:
self.timestamps.append(data)
self.timestamp = False
elif self.category:
self.store_category(data)
if len(sys.argv) != 2:
print(f'{sys.argv[0]} <ip_addr>')
sys.exit(1)
url = f'https://iknowwhatyoudownload.com/en/peer/?ip={sys.argv[1]}'
req = urllib.Request(url, headers={'User-Agent' : "Magic Browser"})
fp = urllib.urlopen(req)
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()
ip = json.loads(urllib.urlopen(f'http://ipinfo.io/{sys.argv[1]}/json').read().decode('utf8'))
print(f'----- IP: {ip["ip"]} ----- Country: {ip["country"]} ----- City: {ip["city"]} -----')
# instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed(mystr)