Created
December 27, 2013 03:16
-
-
Save Zagrebelin/8142153 to your computer and use it in GitHub Desktop.
Помогаем делать первые шаги в питоне. Правим вот этот скрипт http://clubs.ya.ru/python/replies.xml?item_no=2698
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# abook.py | |
# | |
import string | |
import datetime | |
import os | |
def convert(birthday_name): # Функция конвертирования имен | |
#!!!!!!! | |
if birthday_name.count('$D') == 0: | |
return birthday_name | |
name_to_list = "" | |
convert_name = birthday_name.split('$D') | |
list_convert = { '0$90' : ' А', '0$B0' : 'а', '0$91' : ' Б', '0$B1' : 'б', | |
'0$92' : ' В', '0$B2' : 'в', '0$93' : ' Г', '0$B3' : 'г', | |
'0$94' : ' Д', '0$B4' : 'д', '0$95' : ' Е', '0$B5' : 'е', | |
'0$96' : ' Ж', '0$B6' : 'ж', '0$97' : ' З', '0$B7' : 'з', | |
'0$98' : ' И', '0$B8' : 'и', '0$99' : ' Й', '0$B9' : 'й', | |
'0$9A' : ' К', '0$BA' : 'к', '0$9B' : ' Л', '0$BB' : 'л', | |
'0$9C' : ' М', '0$BC' : 'м', '0$9D' : ' Н', '0$BD' : 'н', | |
'0$9E' : ' О', '0$BE' : 'о', '0$9F' : ' П', '0$BF' : 'п', | |
'0$A0' : ' Р', '1$80' : 'р', '0$A1' : ' С', '1$81' : 'с', | |
'0$A2' : ' Т', '1$82' : 'т', '0$A3' : ' У', '1$83' : 'у', | |
'0$A4' : ' Ф', '1$84' : 'ф', '0$A5' : ' Х', '1$85' : 'х', | |
'0$A6' : ' Ц', '1$86' : 'ц', '0$A7' : ' Ч', '1$87' : 'ч', | |
'0$A8' : ' Ш', '1$88' : 'ш', '0$A9' : ' Щ', '1$89' : 'щ', | |
'0$AA' : ' Ъ', '1$8A' : 'ъ', '0$AB' : ' Ы', '1$8B' : 'ы', | |
'0$AC' : ' Ь', '1$8C' : 'ь', '0$AD' : ' Э', '1$8D' : 'э', | |
'0$AE' : ' Ю', '1$8E' : 'ю', '0$AF' : ' Я', '1$8F' : 'я'} | |
for l in range(1, len(convert_name)): | |
if len(convert_name[l]) > 3: | |
lll = convert_name[l][:4] | |
l_c = list_convert[lll] | |
name_to_list = name_to_list + l_c | |
return(name_to_list) | |
def main(): | |
# Определяем пользователя и пути к директориям | |
username = os.getlogin() | |
#!!!!!!! | |
home = os.path.expanduser('~') | |
cache_dir = os.path.join(home, '.cache', 'birthdays') | |
if not os.path.exists(cache_dir): | |
os.mkdir(cache_dir) | |
#!!!!!!! | |
directory = os.listdir(os.path.join(home, '.thunderbird')) | |
for d in directory: | |
if d.find('.default') != -1: | |
thun_dir = os.path.join(home, '.thunderbird', d) | |
# Первичная обработка файла адресной книги (почему-то через strip не получилось) | |
new_file = open(thun_dir + 'abook.mab').read().replace('\n', '').replace('\\', '').replace(' ', '') | |
new_file = new_file[1425:len(new_file) - 1] | |
# Создание списка с данными пользователей | |
data = '' | |
for i in range(0, len(new_file) - 1): | |
if new_file[i] == '<': | |
ind = 1 | |
if new_file[i] == '>': | |
ind = 0 | |
if ind == 1: | |
data = data + new_file[i] | |
open(cache_dir + 'data_file', 'w').write(data.replace('<', '')) | |
# Создание списка пользователей с индексами | |
users = '' | |
for i in range(0, len(new_file) - 1): | |
if new_file[i] == '[': | |
ind = 2 | |
users = users + '' | |
if new_file[i] == ']': | |
ind = 3 | |
users = users + '\n' | |
if ind == 2: | |
users = users + new_file[i] | |
open(cache_dir + 'users_file', 'w').write(users) | |
#Поиск пользователей с днями рождений в сегодняшний день | |
birthdays = 'Сегодня именинники:\n' | |
res_line = '' | |
m = 'B1=' + str(datetime.datetime.today().strftime('%m')) | |
d = 'B2=' + str(datetime.datetime.today().strftime('%d')) | |
# Тестовые переменные m - месяц рождения, d - день рождения | |
#m = 'B1=02' | |
#d = 'B2=21' | |
bd_count = 0 | |
for line in open(cache_dir + 'users_file'): | |
# Совпадение дня и месяца рождения | |
#!!!!!!! | |
if line.find(m) == -1 or line.find(b) == -1: | |
continue | |
bd_count = bd_count + 1 | |
line = line.split(')(') | |
# Ищем и отправляем на конвертирование отображаемое имя | |
for b in range(0, len(line)): | |
#!!!!!!! | |
if line[b].find('^87^') == -1: | |
continue | |
new_line = line[b].replace('^87^', '') | |
res_line.find(new_line) | |
#!!!!!!! | |
if res_line.find(new_line) != -1: | |
continue | |
res_line = res_line + new_line + '\n' | |
name = open(cache_dir + 'data_file').read().split(')(') | |
for a in range(0, len(name)): | |
#!!!!!!! | |
if name[a].find(new_line + '=') != 0: | |
continue | |
birthday_name = name[a][len(new_line) + 1:] | |
# Проверяем на символы - кириллица или латиница | |
#!!!!!!! | |
name_to_list = convert(birthday_name) | |
birthdays = birthdays + name_to_list + '\n' | |
if bd_count == 0: | |
birthdays = 'Сегодня именинников нет' | |
# Вывод уведомления | |
start = 'notify-send -t 20000 "' + birthdays + '"' | |
os.popen(start) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment