-
Notifications
You must be signed in to change notification settings - Fork 0
/
work_with_files.py
142 lines (104 loc) · 4.69 KB
/
work_with_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import ast
from copy import deepcopy
import file_path
import bot
import os
def add_new_user(user_info):
with open(file_path.files_path + 'result_buttons/' + str(user_info.id) + '.txt', 'w') as profile_to_save:
profile_to_save.write('')
is_user_new = True
with open(file_path.files_path + 'users_list.txt', 'r') as users_list_file_to_read:
current_user = str(user_info) + '\n'
for line in users_list_file_to_read:
if line == current_user:
is_user_new = False
if is_user_new:
with open(file_path.files_path + 'users_list.txt', 'a') as users_list_file_add_line:
users_list_file_add_line.write(str(user_info) + '\n')
with open(file_path.files_path + 'total_users_count.txt', 'r') as user_count_to_read:
x = user_count_to_read.readline()
with open(file_path.files_path + 'total_users_count.txt', 'w') as user_count_to_write:
new_q = int(x) + 1
user_count_to_write.write(str(new_q))
def add_search_attempt():
with open(file_path.files_path + 'total_search_count.txt', 'r') as total_attempts_file:
x = total_attempts_file.readline()
new_q = int(x) + 1
with open(file_path.files_path + 'total_search_count.txt', 'w') as total_attempts_file_to_write:
total_attempts_file_to_write.write(str(new_q))
def get_users_count():
with open(file_path.files_path + 'total_users_count.txt', 'r') as users_count_file:
x = users_count_file.readline()
return str(x)
def get_total_search_count():
with open(file_path.files_path + 'total_search_count.txt', 'r') as search_count_file:
x = search_count_file.readline()
return str(x)
def write_up_associated_addresses(some_dict, address, isdone):
if isdone:
with open(file_path.files_path + f'results/{address}.txt', 'w') as file_to_write_addresses:
for tx in some_dict:
file_to_write_addresses.write(str(tx) + '\n')
else:
with open(file_path.files_path + f'results/{address}.txt', 'w') as file_to_write_addresses:
file_to_write_addresses.write('У этого адреса недостаточно совершенных транзакций,'
' как для того что бы получить какую-то информацию =(')
def remove_gived_file(address):
try:
os.remove(file_path.files_path + f'results/{address}.txt')
except:
pass
def get_dict_from_file(user_id):
try:
with open(file_path.files_path + 'result_buttons/' + str(user_id) + '.txt', 'r') as file_to_read:
all_data = ''
for line in file_to_read:
all_data += line
if len(all_data) > 1:
my_opened_dict = ast.literal_eval(all_data)
else:
my_opened_dict = dict()
return my_opened_dict
except:
return dict()
def get_user_buttons(user_id, for_button='buttons'):
temp_dict = deepcopy(get_dict_from_file(user_id))
if for_button in temp_dict.keys():
for buttons in temp_dict[for_button]:
yield {
'text': buttons['text'],
'callback_data': buttons['callback_data']
}
def remove_button(user_id, some_button_name, parent_button='buttons'):
flag = False
buttons_to_remove = []
button_number_counter = -1
temp_dict = deepcopy(get_dict_from_file(user_id))
if parent_button in temp_dict.keys():
for buttons in temp_dict[parent_button]:
for keys, values in buttons.items():
if keys == 'text':
button_number_counter += 1
if values == some_button_name:
buttons_to_remove.append(button_number_counter)
if len(buttons_to_remove) > 0:
for buttons_number in buttons_to_remove[::-1]:
del temp_dict[parent_button][buttons_number]
flag = True
with open((file_path.files_path + 'result_buttons/' + str(user_id) + '.txt'), 'w') as file_to_save:
file_to_save.write(str(temp_dict))
return flag
def add_main_button(user_id, some_button):
temp_dict = deepcopy(get_dict_from_file(user_id))
if 'buttons' in temp_dict.keys():
temp_dict['buttons'].append(some_button.values)
if 'buttons' not in temp_dict.keys():
temp_dict['buttons'] = [some_button.values]
with open(str(file_path.files_path + 'result_buttons/' + str(user_id) + '.txt'), 'w') as file_to_save:
file_to_save.write(str(temp_dict))
def remove_results_file(file_name):
try:
os.chdir(str(file_path.files_path + 'results/'))
os.remove(str(file_name) + '.txt')
except:
pass