插入或更新用户csv。不存在则插入,最新抓取微博id不填,存在则先不动,返回已抓取最新微博id和日期
(logger, headers, result_data, file_path)
| 4 | |
| 5 | |
| 6 | def insert_or_update_user(logger, headers, result_data, file_path): |
| 7 | """插入或更新用户csv。不存在则插入,最新抓取微博id不填,存在则先不动,返回已抓取最新微博id和日期""" |
| 8 | first_write = True if not os.path.isfile(file_path) else False |
| 9 | if os.path.isfile(file_path): |
| 10 | # 文件已存在,直接查看有没有,有就直接return了 |
| 11 | with open(file_path, 'r', encoding='utf-8-sig') as f: |
| 12 | for line in f: |
| 13 | if line.split(',')[0] == result_data[0][0]: |
| 14 | return line.split(',')[len(line.split(',')) - 1].replace('\n', '') |
| 15 | |
| 16 | # 没有或者新建 |
| 17 | result_data[0].append('') |
| 18 | with open(file_path, 'a', encoding='utf-8-sig', newline='') as f: |
| 19 | writer = csv.writer(f) |
| 20 | if first_write: |
| 21 | writer.writerows([headers]) |
| 22 | writer.writerows(result_data) |
| 23 | logger.info('{} 信息写入csv文件完毕,保存路径: {}'.format(result_data[0][1], file_path)) |
| 24 | return '' |
| 25 | |
| 26 | |
| 27 | def update_last_weibo_id(userid, new_last_weibo_msg, file_path): |
nothing calls this directly
no outgoing calls
no test coverage detected