forked from allanlepp/te_rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_stat.py
50 lines (36 loc) · 1.47 KB
/
rss_stat.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
import fileinput
import sys
import rss_print
def save_string_stat(filename, saveToFile, inpString, found):
if saveToFile is True:
countFound = 0
countNotFound = 0
inpString = inpString.replace("/./", "/")
try:
with open(filename, "r", encoding="utf-8") as file:
for line in file:
if line.startswith(inpString):
splittedLine = line.split(";")
countFound = (int)(splittedLine[1])
countNotFound = (int)(splittedLine[2])
except Exception:
with open(filename, "a", encoding="utf-8") as file:
file.write("")
if found:
countFound += 1
else:
countNotFound += 1
inpStringWithStats = inpString + ";" + str(countFound) + ";" + str(countNotFound) + ";"
inpStringWithStats = inpStringWithStats.replace("/./", "/")
replace_line_in_file(filename, inpString + ";", inpStringWithStats)
def replace_line_in_file(filename, searchExp, replaceExp):
found = False
for line in fileinput.input(filename, inplace=1):
if line.startswith(searchExp):
found = True
line = replaceExp + "\n"
sys.stdout.write(line)
if not found:
rss_print.print_debug(__file__, "lisame faili lõppu: " + replaceExp, 2)
with open(filename, "a", encoding="utf-8") as file:
file.write(replaceExp + "\n")