-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (39 loc) · 1.34 KB
/
utils.py
File metadata and controls
50 lines (39 loc) · 1.34 KB
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 os
import time
import csv
def getdate():
'''
yyyy-mm-dd format date text
'''
gmt = time.gmtime()
return '-'.join(list(map(str, [gmt.tm_year, gmt.tm_mon, gmt.tm_mday])))
def esc(text):
'''
Escape unicode text
'''
return str(text).encode('unicode_escape')
def writelog(task, tags):
'''
Save log of task run
'''
with open('runs.log', 'a+', newline='\n', encoding='utf-8') as f:
f.write('{} {} {}\n'.format(time.ctime(), task, str(tags)))
def fopen(tag, **kwargs):
'''
Open a file for the tag
'''
cwd = os.getcwd()
datefmt = getdate()
fn = '{}{}{}'.format(str(int(time.time())), tag, '.csv') # filename
filepath = os.path.join('data', datefmt, fn)
# get file and path names
dn = os.path.join(cwd, os.path.dirname(filepath))
if not os.path.exists(dn):
os.makedirs(dn)
# write header for new file
if not os.path.exists(filepath):
with open(filepath, 'w+', **kwargs) as f:
writer = csv.writer(f)
writer.writerow(['tweet_text', 'tweet_id', 'user_name', 'user_screen_name', 'user_location', 'user_description', 'user_follower_count', 'user_friends_count', 'user_listed_count', 'user_statuses_count', 'user_favourites_count', 'user_verified', 'user_default_profile_image', 'user_default_profile', 'user_protected', 'user_created_at'])
# return the file
return open(filepath, 'a+', **kwargs)