Pythontr

husonet | Tarih: 15.09.2012

python havadurumu meteoroloji


#!/usr/bin/python
#-*- coding:utf-8 -*-

###############################################################################
# Husonet
# 14.09.2012
# husonet
# DMI havadurumularini parse eder
###############################################################################

import re
import os
import urllib2
import MySQLdb
import pywapi
import time
from os.path import exists

# Database ayarlari
os.environ['TZ'] = 'Turkey'

DB_SUNUCU = 'localhost'
DB_KULLANICI = 'xxxxx'
DB_PAROLA = 'xxxxxx'
DB_VERITABANI= 'xxxxx'

ADRES = "http://www.dmi.gov.tr/FTPDATA/wap/%s.wml"

#------------------------------------------------------------------------------
RE_HAVADURUMU_DERECE = '<b>SONDURUM</b>.*?<br/>(.*?)<br/>(.*?).C<br/>'
RE_HAVADURUMU_DERECE += '(.*?)<br/>(.*?)<br/>(.*?)<br/>(.*?)<br/>'
RE_HAVADURUMU_DERECE = re.compile(RE_HAVADURUMU_DERECE, re.DOTALL)

DEBUG = 1

#------------------------------------------------------------------------------
# databaseden ÅŸehirleri getirir
def get_sehirler():
try:
db = MySQLdb.connect( host=DB_SUNUCU, user=DB_KULLANICI,
passwd=DB_PAROLA, db=DB_VERITABANI)
db.set_character_set('utf8')
db.autocommit(False)
crs = db.cursor()
sql = """ SELECT IL_ADI FROM havadurumu """
sql = sql.decode("UTF-8")
crs.execute(sql)
db.commit()
result = crs.fetchall()
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = -1
finally:
try:
crs.close()
db.close()
except:
result = None
return result

# -----------------------------------------------------------------------------
# sehir isimlerini ingilizce karakterler cevir
def sehir_ingilizce_karakter(sVal):
str_val = sVal
dizi = [
('İ°','i'), ("Ü",'u'), ('Ç','c'), ('О','g'), ('ޞ','s'),
('Ö','o'), ('ı','i'), ('ü','u'), ('ç','c'), ('ğ','g'),
('ş','s'), ('ö','o')
]
aranan = ''
for aranan, karakter in dizi:
str_val = str_val.replace(aranan, karakter)
str_val = str_val.lower()
return str_val

# -----------------------------------------------------------------------------
# guncel havadurumlari
def get_hava_durumu(sSehir):
try:
result = ''
yanit = urllib2.urlopen(ADRES % (sehir_ingilizce_karakter(sSehir)))
result = yanit.read()
yanit.close()
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
return result

# -----------------------------------------------------------------------------
# parse havadurumu
def havadurumu_icerik(sInfo):
try:
print sInfo
except Exception, err:
if DEBUG:
raise
else:
print(str(err))

#------------------------------------------------------------------------------
# Havadurumu Derece Çıkar
def havadurumu_derece(sBody):
try:
result = []
stil = RE_HAVADURUMU_DERECE.search(sBody)
result.append(stil.group(1).strip())
result.append(stil.group(2).strip())
result.append(stil.group(3).strip())
result.append(stil.group(4).strip())
result.append(stil.group(5).strip())
result.append(stil.group(6).strip())
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = None
return result

# -----------------------------------------------------------------------------
# process isle
def ana_blok():
try:
sehirler = get_sehirler()
for sehir in sehirler:
info = get_hava_durumu(sehir[0])
print havadurumu_derece(info)
break;
except Exception, err:
if DEBUG:
raise
else:
print(str(err))

# -----------------------------------------------------------------------------
# Program ana bolumu
if __name__ == '__main__':
try:
ana_blok()
except Exception, err:
print(str(err))