Created
March 24, 2014 08:11
-
-
Save moqada/9736144 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| import argparse | |
| import csv | |
| import json | |
| import httpagentparser | |
| import os | |
| oss = {} | |
| browsers = {} | |
| def calc(filename): | |
| fh = open(filename) | |
| rows = [] | |
| for line in fh.readlines(): | |
| info = httpagentparser.detect(json.loads(line.split('\t')[-1])['ua']) | |
| if 'browser' not in info: | |
| print('invalid agent: {}'.format(info)) | |
| continue | |
| versions = browsers.setdefault(info['browser']['name'], {}) | |
| versions.setdefault(info['browser']['version'], 0) | |
| browsers[info['browser']['name']][info['browser']['version']] += 1 | |
| try: | |
| oss.setdefault(info['os']['name'], 0) | |
| oss[info['os']['name']] += 1 | |
| except KeyError: | |
| pass | |
| rows.append([ | |
| info.get('browser', '') and info.get('browser').get('name', ''), | |
| info.get('browser', '') and info.get('browser').get('version', ''), | |
| info.get('os', '') and info.get('os').get('name', ''), | |
| info.get('dist', '') and info.get('dist').get('name', ''), | |
| info.get('dist', '') and info.get('dist').get('version', ''), | |
| ]) | |
| return rows | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('path') | |
| parser.add_argument('export') | |
| args = parser.parse_args() | |
| with open(args.export, 'w') as fh: | |
| writer = csv.writer(fh) | |
| writer.writerow([ | |
| 'browserName', 'browserVersion', 'OS', 'distName', 'distVersion' | |
| ]) | |
| for filename in os.listdir(args.path): | |
| print('parsing {}...'.format(filename)) | |
| with open(args.export, 'a+') as fh: | |
| writer = csv.writer(fh) | |
| rows = calc(os.path.join(args.path, filename)) | |
| writer.writerows(rows) | |
| print('=' * 20) | |
| print('Browsers') | |
| print('=' * 20) | |
| for name, versions in browsers.items(): | |
| print('## {}'.format(name)) | |
| for v, count in versions.items(): | |
| print(v, count) | |
| print('=' * 20) | |
| print('OSs') | |
| print('=' * 20) | |
| for name, count in oss.items(): | |
| print('{}: {}'.format(name, count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment