ColombianTeamAnalysis
Attachment 'UbuntuEstadisticasMoinMoin.py'
Download 1 #Python Version 2.5
2 #
3 #
4 #we need the following modules
5
6 import urllib2,string,datetime
7 from BeautifulSoup import BeautifulSoup
8
9
10
11 def main():
12 # parameter and constants
13 team_url='https://launchpad.net/~ubuntu-co/+members'
14
15 # open team's web page
16 team_page=urllib2.urlopen(team_url)
17
18 # Use BeautifulSoup to parse html
19 soup0 = BeautifulSoup(team_page)
20
21 #Find active members
22 p=soup0.find("table",{ "id":"activemembers"})
23
24 #Convert element to string and use BeautifulSoup again
25 soup1 = BeautifulSoup(str(p))
26
27 #Prepare output as a table
28 members_table=['= Analisis del equipo Colombiano de Ubuntu =\n\n']
29 today = datetime.date.today()
30 members_table.append("Fecha '''"+str(today)+"'''\n\n")
31 members_table.append('|| Nombre || Ubuntero? || Karma ||\n')
32
33 #Find all the ocurrences of <a href=""> in the member's table
34 member_data=soup1.findAll('a')
35
36 count=0
37 countSi=0
38 countNo=0
39 karmaCero=0
40 totalKarma=0
41
42 for i in member_data:
43
44 # get member's url
45 member_url='https://launchpad.net'+str(i.get('href'))
46 # Open member's web page
47 member_page=urllib2.urlopen(member_url)
48 # Use BeautifulSoup to parse html
49 soup2=BeautifulSoup(member_page)
50 #Check to see if the opened page belongs to a group, if it's not the case, get relevant data
51 q=soup2.find("a",{ "class":"menu-link-members"})
52 if q == None:
53 # add member data to output table
54 members_table.append('|| [https://launchpad.net'+str(i.get('href'))+' '+i.renderContents()+'] || ')
55 #find the word "Ubuntero" and if it says "Yes" or no, add relevant data to output table
56 u=soup2.find(text='Ubuntero:')
57 if u <> None:
58 v=u.findNext(text=True)
59 if v.find("Yes") > 0:
60 members_table.append('Si')
61 countSi += 1
62 else:
63 members_table.append('No')
64 countNo += 1
65 else:
66 members_table.append('No')
67 countNo += 1
68 # Find Member's karma, add it to output table
69 karma=soup2.find("span",{ "id":"karma-total"}).contents[0]
70 members_table.append(' || '+str(karma)+' ||\n')
71 totalKarma+=int(karma)
72 if karma == "0":
73 karmaCero += 1
74 count += 1
75 # Add trailing info for table
76 #len(member_data) no lo utilice por si vienen anchor no validos
77 firmaCodigoSi=str(float(countSi)/float(count))
78 firmaCodigoNo=str(float(countNo)/float(count))
79 karmaTCero=str(float(karmaCero)/float(count))
80 karmaTNoCero=str(1-float(karmaCero)/float(count))
81
82 members_table.append('Total Miembros: '+str(count)+'\n')
83 members_table.append('== Firma del codigo de conducta ==\n')
84 members_table.append('||Si||'+firmaCodigoSi+' %||\n')
85 members_table.append('||No||'+firmaCodigoNo+' %||\n')
86 members_table.append('== Karma ==\n')
87 promedioKarma=str(float(totalKarma)/float(count))
88 members_table.append("Karma promedio: '''"+promedioKarma+"'''\n")
89
90 members_table.append('||Karma > 0||'+karmaTNoCero+' %||\n')
91 members_table.append('||Karma = 0||'+karmaTCero+' %||\n')
92
93 # Convert element to string
94 doc= ''.join(members_table)
95 # Use BeautifulSoup to parse html
96 soup = BeautifulSoup(doc)
97 # Print output table
98 print soup.prettify()
99
100 #call main function
101 main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.