You are not logged in.

#1 2009-09-04 22:06:56

majiq
Member
Registered: 2009-03-06
Posts: 259

Mirror check

This is a script that checks http://users.archlinux.de/~gerbra/mirrorcheck.html for you and only prints the relevant sections. Be warned - it uses python3.

#!/usr/bin/python3

import subprocess
import http.client
from html.parser import HTMLParser

servers_to_check = []

#-------------------------------------------------------------------------------
#Define the parser
class server_checker (HTMLParser):
    check_servers = []
    current_repo = ''
    inA = 0
    inCaption = 0
    foundServer = 0
    printMode = 0
    blockMode = 0

    def handle_starttag (self, tag, attrs):
        if tag == 'caption':
            self.inCaption = 1
        if tag == 'a':
            self.inA = 1
    
    def handle_endtag (self, tag):
        if tag == 'caption':
            self.inCaption = 0
        if tag == 'a':
            self.inA = 0
        if tag == 'td':
            if self.blockMode > 0:
                self.blockMode -= 1

    def handle_data (self, text):
        if self.inA == 1:
            self.printMode = -1
            for i in range(0,len(self.check_servers)):
                if text.find(self.check_servers[i].split(' ')[0]) != -1:
                    self.printMode = 1
                    print("Server: ", self.check_servers[i])
                    self.current_repo = self.check_servers[i].split(' ')[1]
                    break
            if self.printMode == -1:
                self.printMode = 0
        elif self.printMode == 1:
            if text.find(self.current_repo) > 0 and self.blockMode == 0:
                self.blockMode = 4
            if len(text.strip('\n ')) > 0 and self.blockMode > 0:
                if self.blockMode == 4:
                    print('\t' , text.strip('\n '), end = '\t')
                elif self.blockMode == 2:
                    print(text.strip('\n '))
                else:
                    print(text.strip('\n '), end='\t')

#-------------------------------------------------------------------------------
#Set up the parser

myServerChecker = server_checker()

i = 1
for line in subprocess.getoutput("grep -- '^[^#]' /etc/pacman.d/mirrorlist | sed -e 's|^.*://||;s|/.*/| |'").split('\n'):
    myServerChecker.check_servers.append(line)
    print ("Server",i,": ",line.split(' ')[0])
    i+=1

connection = http.client.HTTPConnection('users.archlinux.de')
connection.request('GET','/~gerbra/mirrorcheck.html')
response = connection.getresponse()

print ("Connection response is" , response.status)

if response.status != 200:
    print ("Connection error")
    exit

#-------------------------------------------------------------------------------
#Run the parser
myServerChecker.feed(response.read().decode().strip('\n'))
myServerChecker.close()

Let me know what you guys think. I was planning on aliasing pacman to 'echo "Should you check your mirrors?" && pacman'. I'd just run the script, but sometimes I just want a -Qi and checking the mirrors then would be annoying.

Offline

#2 2009-09-05 11:40:21

djszapi
Member
From: Cambridge, United Kingdom
Registered: 2009-06-14
Posts: 1,439
Website

Re: Mirror check

Hello majiq!

"Let me know what you guys think. I was planning on aliasing pacman to 'echo "Should you check your mirrors?" && pacman'. I'd just run the script, but sometimes I just want a -Qi and checking the mirrors then would be annoying." <- in this case you can use function in .bashrc e.g. instead of alias.

Offline

Board footer

Powered by FluxBB