Created
February 18, 2016 18:15
-
-
Save jarifibrahim/c754a87a4acf3e4dd3e6 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# Script to automatically populate top-starred-devs-and-repos Readme file | |
import re | |
import urllib2 | |
from bs4 import BeautifulSoup | |
f = open("README.md", 'r') | |
new_file = open("test.md", 'w') | |
for line in f: | |
#find line starting with a number | |
if not re.match("\d+. ", line) is None: | |
# extract url from the line | |
url = re.search("(https?://[^\s]+)", line).group(0)[:-1] | |
print ("Fetching page from URL") | |
page_html = urllib2.urlopen(url).read() | |
soup = BeautifulSoup(page_html, 'html.parser') | |
# find a list of all span elements | |
try: | |
repo = soup.find_all('span', {'class' : 'repo'})[0].get_text().strip() | |
stars = soup.find_all('span', {'class': 'stars'})[0].get_text().strip() | |
except Exception as e: | |
try: | |
repo = soup.find('h3', {'class' : 'repo-list-name'}).find('a').get_text().strip() | |
stars = soup.find('a', {'class' : 'repo-list-stat-item tooltipped tooltipped-s'}).get_text().strip() | |
except: | |
# The rest of the README file contains only top-repo. No more user/org found | |
new_file.write(line) | |
for line in f: | |
new_file.write(line) | |
new_file.close() | |
f.close() | |
exit() | |
repo_link = url + "/" + repo | |
add_line = " ([%s](%s) %s)\n" % (repo, repo_link, stars) | |
line = line.rstrip("\n") + add_line | |
print("Added %s" % add_line) | |
new_file.write(line) | |
f.close() | |
new_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment