Created
October 13, 2012 12:55
-
-
Save macolyte/3884563 to your computer and use it in GitHub Desktop.
ADN Follower suggestions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, urllib, operator | |
from collections import defaultdict | |
USERNAME = raw_input("Get suggestions for: ") | |
CUTOFF = 3 | |
pattern = re.compile(r'<span class="username"><a href="/\w+">(?P<name>\w+)</a></span>') | |
suggestions = defaultdict(int) | |
def find_followers(user): | |
print "**********" | |
print "Finding followers for %s" % user | |
url = "https://alpha.app.net/%s/following/" % user | |
user_html = urllib.urlopen(url).read() | |
user_following = re.findall(pattern, user_html) | |
print "%s follows %d people" % (user, len(user_following)) | |
return user_following | |
i_follow = find_followers(USERNAME) | |
n_follow = len(i_follow) | |
for user in i_follow: | |
user_following = find_followers(user) | |
for person in user_following: | |
if person in i_follow: | |
continue | |
else: | |
suggestions[person] += 1 | |
sorted_suggestions = sorted(suggestions.iteritems(), key=operator.itemgetter(1)) | |
final_suggestions = (item for item in sorted_suggestions if item[1] > CUTOFF) | |
output = "<html><head></head><body><table border='1'>" | |
for item in final_suggestions: | |
percentage = (float(item[1])/n_follow) * 100 | |
output += "<tr><td><a href='https://alpha.app.net/%s/'>%s</td><td>%.1f%%</td></tr>\n" % (item[0], item[0], percentage) | |
output += "</table></body></html>" | |
with open("adn_suggestions.html", "w") as f: | |
f.write(output) | |
print "\nAll done. Your suggestions are in adn_suggestions.html" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment