The WF Python Functions War Chest

Poor man's MajesticSEO API... No account required apparently

Code:
import requests

def get_url_metrics(url):
    """Get metrics for a single URL"""
    
    data = {
        'getcsv': 'Get backlink counts as .CSV',
        'SortBy': '-1',
        'items': '1',
        'Datasource': 'Fresh',
        'item0': url
    }

    request = requests.post('https://csv.majesticseo.com/getcsvdata/bulkbacklinks',
                            data=data,
                            cookies={'STOK': 'anything'})

    if 'We could not determine your user creditials' not in request.text:
        return dict(zip(request.text.splitlines()[0].split(','),
                    request.text.splitlines()[1].split(',')))
    else:
        raise Exception("Couldn't authenticate without proper session variable")

Returns:

Code:
{u'ACRank': u'8',
 u'CitationFlow': u'54',
 u'ExtBackLinks': u'43649',
 u'ExtBackLinksEDU': u'2',
 u'ExtBackLinksGOV': u'1',
 u'ItemType': u'3',
 u'RefDomains': u'1413',
 u'RefDomainsEDU': u'2',
 u'RefDomainsGOV': u'1',
 u'RefIPs': u'1309',
 u'RefSubNets': u'1152',
 u'ResultCode': u'OK',
 u'Status': u'Found',
 u'TrustFlow': u'35',
 u'\ufeffItem': u'"http://www.wickedfire.com"'}
 


So you obviously always have a few iPython shells open, so you grab one, then use the power of sets and one liners to see how many unique domains are in that huge text file of links you have lying around:

Code:
len(set(urlparse.urlparse(line.strip()).netloc.lower() for line in open('my_links.txt')))

That was easy, huh?