forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPomodoro.py
153 lines (119 loc) Β· 4.56 KB
/
Pomodoro.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
143
144
145
146
147
148
149
150
151
152
import time
# path of host file in windows
host_path = r"C:\Windows\System32\drivers\etc\hosts"
# URL of websites to block
with open('block_urls.txt', 'r') as url_file:
block_list = [url.strip() for url in url_file]
# redirecting above URLs to this localhost to ensure blocking
redirect = "127.0.0.1"
def block_websites():
"""
The function will open the host file and add the block-list websites to
the file if it is not already present and redirect it to the localhost
for blocking
"""
try:
# Opening the host file in reading and writing mode
with open(host_path, 'r+') as h_file:
content = h_file.read()
for website in block_list:
# Website is already blocked
if website in content:
pass
# To redirect the website to be blocked
else:
h_file.write(redirect + "\t" + website + "\n")
return 1
except PermissionError:
print('\nTry running the cmd in admin mode and '
'then run this code to enable this functionality!')
return 0
except FileNotFoundError:
print('\nThis functionality is not supported for your OS!')
return 0
def remove_websites():
"""
The function will unblock the block_list websites by opening the file
and removing the changes we made before
"""
try:
# Opening the host file in reading and writing mode
with open(host_path, "r+") as file:
# making each line of file into a list
content = file.readlines()
# sets the file pointer at the beginning of the file
file.seek(0)
# Traversing through each line of the host file and
# checking for the websites to be blocked
for lines in content:
if not any(website in lines for website in block_list):
file.write(lines)
# Truncating the file to its original size
file.truncate()
finally:
pass
def pomodoro():
"""
This function has the implementation of the user-friendly Pomodoro timer
along with the website blocking functionality.
"""
print('\n------------------ POMODORO TIMER ------------------\n')
print('\nPomodoro timer helps to break down your work into 25 minutes of '
'high focus intervals separated by short breaks of 5 minutes.')
print('\nFor extra focus, would you like to enable website blocker? '
'\n**Selecting this option will block all of the popular '
'social media sites throughout the 25 min duration**')
enable = input('\nPress (y) to enable and any other key to skip: ')
cycle = 0
while 1:
# Feature to block social media websites
if enable == 'y':
if block_websites():
pass
else:
enable = ''
input('\nPress any key to start the timer: ')
cycle += 1
# Setting a 25 minute timer
t = 1 * 60
# To display the time left on the screen
while t:
minute_count = t // 60
second_count = t % 60
timer = '{:02d}:{:02d}'.format(minute_count, second_count)
print(f'Time left: {timer}', end="\r")
time.sleep(1)
t -= 1
# A sound to let user know the timer is over
for i in range(2):
for _ in range(4):
time.sleep(0.5)
print("\a")
time.sleep(3)
print('\nPOMODORO FINISHED!')
print("Break Time!! Give yourself a 5 min break!")
input('\nPress any key to start the break timer: ')
# Removing the blocked websites for the break
if enable == 'y':
remove_websites()
t = 1 * 60
# To display the break time left
while t:
minute_count = t // 60
second_count = t % 60
timer = '{:02d}:{:02d}'.format(minute_count, second_count)
print(f'Time left: {timer}', end="\r")
time.sleep(1)
t -= 1
# A sound to let user know the timer is over
for i in range(3):
for _ in range(4):
time.sleep(0.5)
print("\a")
time.sleep(3)
ans = input('\nWould you like to start a new Pomodoro? \n'
'Press (y) to continue or any other key to exit: ').lower()
if ans != 'y':
return cycle
num = pomodoro()
print(f'\nGreat work! You completed {num} Pomodoro cycles! \nGood going!\n')