-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (59 loc) · 2.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
import streamlit as st
import json
from bs4 import BeautifulSoup
#set page title
st.title('Stack Overflow Profile Summary')
#load local css file for styling
def local_css(file_name):
with open(file_name) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
local_css("style.css")
# Code for the sidebar text input
with st.sidebar:
st.subheader('Enter Stack Overflow User ID:')
userId = st.text_input('')
if userId and (not userId.isdigit()):
st.error("User ID must be digits only!")
# Code for the profile info display page
if userId and userId.isdigit():
BASEURL = "https://api.stackexchange.com/2.2/users/"+userId
params = {
"site" : "stackoverflow"
}
#send request and save response in JSON
resp = requests.get(BASEURL, params=params)
user_details = resp.json()
if len(user_details['items']) > 0:
#Scrape user's rating directly
rank_link = 'https://stackoverflow.com/users/rank?userId='+userId
profile_page = requests.get(rank_link)
soup = BeautifulSoup(profile_page.content, 'html.parser')
#assign user details to variables
display_name = user_details['items'][0]['display_name']
reputation = user_details['items'][0]['reputation']
image_url = user_details['items'][0]['profile_image']
badges = user_details['items'][0]['badge_counts']
gold = badges['gold']
silver = badges['silver']
bronze = badges['bronze']
#check if user is ranked
if soup.find('b'):
percentile = soup.find('b').get_text()
else :
percentile = '0%'
#Display user details in main page
st.image(image_url)
st.markdown(f'<p class="big-font"> Name: {display_name}</p>', unsafe_allow_html=True)
st.markdown(f'<p class="big-font"> Reputation: {reputation}</p>', unsafe_allow_html=True)
st.markdown(f'<p class="big-font"> Badges: </p>', unsafe_allow_html=True)
st.markdown(f'''
* <b id="gold">Gold: {gold}</b>
* <b id="silver">Silver: {silver}</b>
* <b id="bronze">Bronze: {bronze}</b>
''', unsafe_allow_html=True)
st.markdown(f'<p class="big-font" id="top"> Top: {percentile} Overall</p>', unsafe_allow_html=True)
else:
st.info("A profile with that User ID is not found!")
else :
st.info("Please enter Stack Overflow UserId (digit only) in the left input box")