Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Google Group Members Evolution using Gource
'''
Python Google Group Members Evolution Analysis
Gource:
http://code.google.com/p/gource
'''
import csv
import re
import time
import datetime
FILE_CSV = 'YOUR_INPUT.csv'
OUTPUT_FILE = 'OUTPUT_FILE.txt'
def create_customlog():
group_history = []
f = open(FILE_CSV, 'r')
r = csv.reader(f)
for line,row in enumerate(r):
try:
if row[2] != 'banned':
timestamp = datetime.datetime(year=int(row[6]),month=int(row[7]),day=int(row[8]),
hour=int(row[9]),minute=int(row[10]),second=int(row[11]))
user = row[0]
group_history.append((user,timestamp))
line+=1
except:
print 'Error reading line %d. Skipped.' % line
f.close()
#Sort by the timestamp.
group_history = sorted(group_history,key= lambda sh: sh[1])
fhandle = open(OUTPUT_FILE, 'a')
for sh in group_history:
str_log = '%s|%s|%s|%s'
#you can easily change the users in this section.
if sh[1].year == 2010 and sh[1].month in [2]:
user = 'USER_A'
elif sh[1].year == 2010 and sh[1].month in [3]:
user = 'USER_B'
elif sh[1].year == 2010 and sh[1].month in [4]:
user = 'USER_C'
elif sh[1].year == 2010 and sh[1].month in [5]:
user = 'USER_D'
else:
user = 'USER_E'
#Extract the logins of the user's emails.
usr = re.match(r'(.*)@(.*)',sh[0]).group(1)
#Creating the formatted string.
str_log = str_log % (int(time.mktime(sh[1].timetuple())),user,'A',user+ '/'+ usr)
#Write at the log.
fhandle.write(str_log+'\n')
fhandle.close()
if __name__ == '__main__':
create_customlog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment