-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.py
More file actions
49 lines (46 loc) · 1006 Bytes
/
fetch.py
File metadata and controls
49 lines (46 loc) · 1006 Bytes
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
import time
import urllib.request
import sys
def getpath():
conf=open('today.config','r')
st=conf.read()
key,pt=st.split('=')
if key=='path':
return pt
def usestd(b):
if b==False:
lout=open('log','w+')
sys.stdout=lout
def logger(*dt):
'''Logging Fuction'''
ts=time.asctime()
print('[',ts,']',end='')
sys.stdout.writelines(dt)
print()
sys.stdout.flush()
if __name__ == '__main__':
usestd(False)
while True:
try:
flag=True
rem=urllib.request.urlopen('http://www.ipip5.com/today/api.php?type=txt')
if rem.getcode()!=200:
flag=False
except Exception as e:
logger('Error:',e)
flag=False
if flag==True:
logger('TXT fetch success')
fout=open(getpath(),'w+')
try:
text=rem.read()
text=text.decode('UTF-8')
text=text.split('\n')
del text[0],text[-1]
text=[line+'\n' for line in text]
fout.writelines(text)
except Exception as e:
logger('Error:',e)
fout.close()
logger('TXT update done. Now sleep.')
time.sleep(1800)