~ubuntu-branches/ubuntu/precise/kstars/precise-proposed

« back to all changes in this revision

Viewing changes to kstars/data/scripts/supernova_updates_parser.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-16 13:14:42 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111216131442-dfrjlt6pests9qu1
Tags: 4:4.7.90-0ubuntu1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##************************************************************************##
 
2
#            supernova_updates_parser.py  -  K Desktop Planetarium         #
 
3
#                             -------------------                          #
 
4
#    begin                : 2011/18/06                                     #
 
5
#    copyright            : (C) 2011 by Samikshan Bairagya                 #
 
6
#    email                : samikshan@gmail.com                            #
 
7
##************************************************************************##
 
8
 
 
9
##************************************************************************##
 
10
#                                                                          #
 
11
#   This program is free software; you can redistribute it and/or modify   #
 
12
#   it under the terms of the GNU General Public License as published by   #
 
13
#   the Free Software Foundation; either version 2 of the License, or      #
 
14
#   (at your option) any later version.                                    #
 
15
#                                                                          #
 
16
##************************************************************************##
 
17
 
 
18
### Supernova Updates Parser. This program reads data from http://www.cbat.eps.harvard.edu/lists/RecentSupernovae.html 
 
19
### This page gives details on supernovae that have occurred since the start of 2010.
 
20
 
 
21
#!/usr/bin/env python
 
22
import re
 
23
import urllib
 
24
import os
 
25
from PyKDE4.kdecore import KStandardDirs
 
26
 
 
27
def parse( line ) :
 
28
    parsed = toCSV(re.sub('<.*?>','',line))
 
29
    return parsed
 
30
 
 
31
#FIXME: The extracted data is converted to CSV by inserting commas
 
32
       #after definite number of characters. There might be a better
 
33
       #way to do this.
 
34
def toCSV(line):
 
35
    commaInterval= [7,24,36,44,51,63,68,86,110,129,133,143]
 
36
    for i in range(0,len(line)-1):
 
37
        if i in commaInterval:
 
38
            edited = line [ :i ] + "," +line [ i+1: ]
 
39
            line=edited
 
40
    return line
 
41
 
 
42
sock = urllib.urlopen("http://www.cbat.eps.harvard.edu/lists/RecentSupernovae.html")
 
43
pageLines=sock.readlines()
 
44
sock.close()
 
45
found = False
 
46
firstLine=True
 
47
 
 
48
output = open(KStandardDirs().locateLocal('data','kstars/supernovae.dat'),'w')
 
49
#print KStandardDirs().locateLocal('data','kstars/supernovae.dat')
 
50
for i in pageLines:
 
51
    if(found):
 
52
        p = re.compile("</pre>")
 
53
        m = p.search(i)
 
54
        if m:
 
55
            found = False
 
56
            break
 
57
        if (firstLine):
 
58
            parsedLine = "#" + parse(i)
 
59
            firstLine = False
 
60
        else:
 
61
            parsedLine = parse(i)
 
62
        #print count
 
63
        output.write(parsedLine)
 
64
        continue;
 
65
    p = re.compile("<pre>")
 
66
    m = p.search(i)
 
67
    if m:
 
68
        print "found!!"+i
 
69
        firstLine=True
 
70
        found = True
 
71
output.close()