~dpm/+junk/langpacks-by-inst

« back to all changes in this revision

Viewing changes to get_data.py

  • Committer: David Planella
  • Date: 2010-06-06 00:59:13 UTC
  • Revision ID: dpm@lillypilly-20100606005913-145mwdgcpg38yz6n
Ported all code to Python, moved modules to a local library location

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
### BEGIN LICENSE
 
4
# Copyright (C) 2009 <David Planella> <david.planella@ubuntu.com>
 
5
#This program is free software: you can redistribute it and/or modify it
 
6
#under the terms of the GNU General Public License version 3, as published
 
7
#by the Free Software Foundation.
 
8
#
 
9
#This program is distributed in the hope that it will be useful, but
 
10
#WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
#PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
#You should have received a copy of the GNU General Public License along
 
15
#with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
### END LICENSE
 
17
 
 
18
import datetime
 
19
import urllib2
 
20
import urllib
 
21
import time
 
22
import os
 
23
import gzip
 
24
import csv
 
25
import re
 
26
from LocaleInfo import LocaleInfo
 
27
import simplejson as json
 
28
 
 
29
def get_http_timestamp(url):
 
30
    try:
 
31
        sock = urllib2.urlopen(url)
 
32
    except:
 
33
        return None
 
34
 
 
35
    timestamp_lm = None
 
36
    if sock.info()["Last-Modified"]:
 
37
        lm_string = sock.info()["Last-Modified"]
 
38
        sock.close()
 
39
        if lm_string:
 
40
            timestamp_lm = time.mktime(time.strptime(lm_string, "%a, %d %b %Y %H:%M:%S GMT"))
 
41
            datetime_lm = datetime.datetime.fromtimestamp(timestamp_lm)
 
42
            return datetime_lm
 
43
    return None
 
44
 
 
45
def get_file_timestamp(filename):
 
46
    try:
 
47
        mtime = os.path.getmtime(filename)
 
48
    except:
 
49
        return None
 
50
 
 
51
    return datetime.datetime.utcfromtimestamp(mtime)
 
52
 
 
53
def get_langpack_code(langpackname):
 
54
    langpack_bits = langpackname.split('-')
 
55
 
 
56
    if len(langpack_bits) == 5:
 
57
        langpack_code = langpack_bits[2] + '-' + langpack_bits[3]
 
58
    else:
 
59
        langpack_code = langpack_bits[2]
 
60
 
 
61
    return langpack_code
 
62
 
 
63
def get_language_name(langcode, li):
 
64
    try:
 
65
        lang_name = li.translate(langcode, True)
 
66
        lname = lang_name.split(';')[0]
 
67
    except:
 
68
        lname = langcode
 
69
 
 
70
    return lname
 
71
 
 
72
DATA_DIR='/home/dpm/langpacks-by-inst/data'
 
73
PUBLIC_DIR='/home/dpm/public_html'
 
74
URL = "http://popcon.ubuntu.com/by_inst.gz"
 
75
REGEXP = re.compile("^language-pack-[a-z]{2,3}(-han[st])?-base")
 
76
data_file = os.path.join(DATA_DIR, os.path.basename(URL))
 
77
CSV_FILE = os.path.join(DATA_DIR, 'langpacks_by_inst.csv')
 
78
JSON_FILE = os.path.join(DATA_DIR, 'langpacks_by_inst.json')
 
79
 
 
80
http_timestamp = get_http_timestamp(URL)
 
81
if http_timestamp != get_file_timestamp(data_file):
 
82
    urllib.urlretrieve(URL, data_file)
 
83
 
 
84
    # Extract and put the language pack data in a CSV file
 
85
 
 
86
    data_csv = csv.reader(gzip.open(data_file), delimiter = ' ', skipinitialspace = True)
 
87
 
 
88
writer = csv.writer(open(CSV_FILE, "wb"))
 
89
writer.writerow(['# Language', 'Installations', 'Timestamp'])
 
90
 
 
91
li = LocaleInfo.LocaleInfo()
 
92
 
 
93
data_json = []
 
94
try:
 
95
    for row in data_csv:
 
96
        #if not (row[0].startswith('#') or row[0].startswith('-') or (row[1] == 'Total')) :
 
97
        try:
 
98
            if REGEXP.search(row[1]):
 
99
                lang_code = get_langpack_code(row[1])
 
100
                installs = row[2]
 
101
                lang_name = get_language_name(lang_code, li)
 
102
                writer.writerow([lang_code, installs, lang_name, http_timestamp])
 
103
                data_json.append({'installs': installs, 'langpack': lang_name})
 
104
        except:
 
105
            pass
 
106
except csv.Error, e:
 
107
    pass
 
108
    #print 'file %s, line %d: %s' % (data_file, data_csv.line_num, e)
 
109
 
 
110
f = open(JSON_FILE, mode='w')
 
111
json.dump(data_json, f)