-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGA_ondn.py
More file actions
80 lines (58 loc) · 2.77 KB
/
GA_ondn.py
File metadata and controls
80 lines (58 loc) · 2.77 KB
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
73
74
75
76
77
78
79
80
#Program for sorting Google Analytics pageview report.
#IMPORTANT - Precondition: Report must be exported from GA Pages (english) in .csv format
import re
import sys
from datetime import datetime
#Open file and read it as a string.
def file_to_string(filename):
file = open(filename, 'r')
text = file.read()
file.close()
return text
#Catch name, date, title of article and number of pageviews per article with regular expressions.
def catch_data(text):
site_name = re.search('www.\w+.\w+', text)
report_date = re.search('\w+-\w+', text)
title = site_name.group()
date_range = report_date.group()
articles = re.findall('(/\D+/[^,]+),"(\w+,\w+)"', text)
return title, date_range, articles
#Remove listed subcategories from the articles list in the tuple.
def clear_subcategories(articles):
clear_articles = [x for x in articles if x[0] not in ('/slovenija/v-ospredju', '/sport/nogomet',
'/poslovni/novice', '/sport/hokej-na-ledu,') and '/tag/' not in x[0]]
return clear_articles
#Save "n" most viewed pages in a new file.
#Precondition: Your original report must contain a minimum of n articles.
def write_file(title, date_range, articles, filewrite, n):
range_start, range_end = date_range[:8], date_range[9:]
date_start = datetime.strptime(range_start, '%Y%m%d')
date_end = datetime.strptime(range_end, '%Y%m%d')
top_articles = articles[:n]
# file = open(filewrite, 'w')
# file.write(title + '\n\n')
# file.write('From: ' + str(date_start) + ' To: ' + str(date_end) + '\n\n')
# for art in top_articles:
# file.write(art[0] + ',' + ' ' + art[1] + '\n\n')
# file.close()
with open(filewrite, 'w') as file:
file.write(title + '\n\n')
file.write('From: ' + str(date_start) + ' To: ' + str(date_end) + '\n\n')
for art in top_articles:
file.write(art[0] + ',' + ' ' + art[1] + '\n\n')
if __name__ == "__main__":
if len(sys.argv) < 4:
print ("Please provide input file, output file and the number of articles you want to have in your output file.")
sys.exit(0)
# filename = input("Please provide the path to the Google Analytics file (without the quotes):")
filename = sys.argv[1]
# filewrite = input("Please provide the path to the write-to file (without the quotes):")
filewrite = sys.argv[2]
# n = input("Please select the number of articles with the most pageviews you would like to have in the new report:")
n = sys.argv[3]
# text = file_to_string(filename)
text = open(filename, 'r').read()
title, date_range, articles = catch_data(text)
articles = clear_subcategories(articles)
write_file(title, date_range, articles, filewrite, int(n))
print('Done!')