#!/usr/bin/env python ###################################### # mbl_depart.py # # Emanuel Miller # emiller@ccorpsoft.com ###################################### import httplib, json, os, time, sys class MBL_Depart: API_DOMAIN = "mybloglog.yahooapis.com" API_KEY = "foo" USER = "" BASE = "" """ init mayne """ def __init__(self, username, key=False): self.USER = username self.API_KEY = key if key else self.API_KEY """ api request """ def api_request(self, request, total=100, start=0): try: conn = httplib.HTTPConnection(self.API_DOMAIN, 80) conn.request("GET", "%s?appid=%s&format=json&count=%d&start=%d" % (request, self.API_KEY, total, start)) request = conn.getresponse() if request.status != 200: return False data = request.read() conn.close() except: return False return data """ translate username -> id """ def id_lookup(self, username): request = "/v1/user/screen_name/%s" % username data = self.api_request(request) if not data: return data = json.loads(data) return data["id"] """ user lookup """ def user_lookup(self, id): request = "/v1/user/%s" % id data = self.api_request(request) if not data: return data = json.loads(data) return data """ return contacts """ def contacts_lookup(self, id): request = "/v1/user/%s/contacts" % id offset = 0 total = 100 contacts = [] while not time.sleep(0.1): data = self.api_request(request, total, offset) if not data: break data = json.loads(data) if int(data["yahoo:returned"]) == 0: break for contact in data["contact"]: contacts.append(contact) offset += 100 return contacts """ return hcard """ def return_hcard(self, username): try: conn = httplib.HTTPConnection("www.mybloglog.com", 80) conn.request("GET", "/buzz/members/%s/hcard" % username) request = conn.getresponse() if request.status != 200: return False data = request.read() conn.close() except: return False return data """ archive a user """ def archive_user(self, contact): user = self.user_lookup(contact["id"]) path = "./%s/%s" % (self.USER, contact["nickname"]) if not user: return if not os.path.exists(path): os.mkdir(path) if user.has_key("sites_authored") and len(user["sites_authored"]) > 0 and user["sites_authored"].has_key("site"): sites_authored = "" for site in user["sites_authored"]["site"]: try: sites_authored += "
  • %s
  • " % (site["url"], str(site["name"])) except: sites_authored += "
  • %s
  • " % (site["url"], str(site["url"])) sites_authored += "" open("%s/authored.html" % path, "w").write(sites_authored) if user.has_key("communities_joined") and len(user["communities_joined"]) > 0 and user["communities_joined"].has_key("community_joined"): communities_joined = "" for site in user["communities_joined"]["community_joined"]: try: communities_joined += "
  • %s
  • " % (site["url"], site["name"]) except: communities_joined += "
  • %s
  • " % (site["url"], site["url"]) communities_joined += "" open("%s/communities.html" % path, "w").write(communities_joined) if user["profile"].has_key("services") and len(user["profile"]["services"]) > 0 and user["profile"]["services"].has_key("service"): services = "" for service in user["profile"]["services"]["service"]: if service.has_key("name") and service.has_key("profile_url") and service.has_key("id"): services += "
  • %s: %s
  • " % (service["name"], service["profile_url"], service["id"]) services += "" open("%s/services.html" % path, "w").write(services) hcard = self.return_hcard(contact["nickname"]) if hcard: open("%s/hcard.html" % path, "w").write(hcard) """ main loop """ def run(self): print "Looking up %s..." % self.USER id = self.id_lookup(self.USER) if not id.isdigit(): print "Could not lookup %s!" % self.USER return self.ME = self.user_lookup(id) if not os.path.exists("./%s" % self.USER): os.mkdir("./%s" % self.USER) self.archive_user({"nickname": self.USER, "id": id}) print "Collecting contacts..." contacts = self.contacts_lookup(id) tc = 0 for contact in contacts: tc += 1 print " - [%d/%d] Archiving %s (%s)..." % (tc, len(contacts), contact["nickname"], contact["id"]) self.archive_user(contact) if __name__ == "__main__": if len(sys.argv) != 2: print "./mbl_depart.py " sys.exit() main = MBL_Depart(sys.argv[1]) main.run()