User:Popcorndude/bacon

import pywikibot as pw
wd = pw.Site('wikidata', 'wikidata').data_repository()
do_list = ['Q3454165']
goal = 'Q720'
def get_file(ip):
    return "scripts/my-scripts/Bacon/%s.txt" % divmod(int(ip.id[1:]), 1000)[0]
def get_dict(ip):
    try:
        print get_file(ip)
        f = open(get_file(ip))
        r = f.read()
        f.close()
        return eval(r)
    except:
        return {}
class trick:
    def __init__(self, s):
        self.id = s
def save_dict(d):
    f = open(get_file(trick(d.keys()[0])), 'w')
    f.write(str(d))
    f.close()
    del d
def update(ip, n, source, p):
    d = get_dict(ip)
    if ip.id not in d or d[ip.id][0] > n:
        d[ip.id] = [n, source, p]
        save_dict(d)
        do_list.append(ip.id)
def do_ip(ip):
    ip.get()
    d = get_dict(ip)
    #print d
    try: n = d[ip.id][0]
    except:
        n = 0
        d[ip.id] = [0, None, None]
    for p in ip.claims.keys():
        for c in ip.claims[p]:
            if type(c.target) == type(ip):
                update(c.target, n + 1, ip.id, p)
def goal_found():
    return goal in get_dict(trick(goal)).keys()
while not goal_found():
    do_ip(pw.ItemPage(wd, do_list.pop(0)))