|
| 1 | +import pymysql |
| 2 | +import urllib3 |
| 3 | +import uuid |
| 4 | + |
| 5 | +from config import MYSQL_CONFIG |
| 6 | +from table import upload_txt |
| 7 | + |
| 8 | + |
| 9 | +def connect_db(): |
| 10 | + urllib3.disable_warnings() |
| 11 | + conn = pymysql.connect(host=MYSQL_CONFIG['host'], |
| 12 | + port=MYSQL_CONFIG['port'], |
| 13 | + user=MYSQL_CONFIG['user'], |
| 14 | + password=MYSQL_CONFIG['password'], |
| 15 | + db=MYSQL_CONFIG['db'], |
| 16 | + charset='utf8') |
| 17 | + return conn |
| 18 | + |
| 19 | + |
| 20 | +def convert_content_to_url(connection): |
| 21 | + cur = connection.cursor() |
| 22 | + batch_size = 500 |
| 23 | + last_id = 0 |
| 24 | + total_articles = 0 |
| 25 | + |
| 26 | + try: |
| 27 | + while True: |
| 28 | + cur.execute(""" |
| 29 | + SELECT `id`, `board_id`, `content` |
| 30 | + FROM `new_articles` |
| 31 | + WHERE `content` IS NOT NULL |
| 32 | + AND `board_id` != 14 |
| 33 | + AND `id` > %s |
| 34 | + ORDER BY `id` ASC |
| 35 | + LIMIT %s |
| 36 | + """, (last_id, batch_size)) |
| 37 | + |
| 38 | + articles = cur.fetchall() |
| 39 | + if not articles: |
| 40 | + break |
| 41 | + |
| 42 | + for article in articles: |
| 43 | + article_id, board_id, content = article |
| 44 | + random_uuid = str(uuid.uuid4().hex) |
| 45 | + file_name = f'articles/content/board_{board_id}/{random_uuid}.txt' |
| 46 | + content_url = upload_txt(file_name=file_name, text_content=content) |
| 47 | + |
| 48 | + update_cur = connection.cursor() |
| 49 | + update_cur.execute(""" |
| 50 | + UPDATE `new_articles` |
| 51 | + SET `content` = %s |
| 52 | + WHERE `id` = %s |
| 53 | + """, (content_url, article_id)) |
| 54 | + print(f"article {article_id} url: {content_url}") |
| 55 | + update_cur.close() |
| 56 | + |
| 57 | + total_articles += 1 |
| 58 | + |
| 59 | + last_id = articles[-1][0] |
| 60 | + connection.commit() |
| 61 | + |
| 62 | + except Exception as error: |
| 63 | + connection.rollback() |
| 64 | + print(error) |
| 65 | + finally: |
| 66 | + cur.close() |
| 67 | + print(f"total articles: {total_articles}") |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + connection = None |
| 72 | + try: |
| 73 | + connection = connect_db() |
| 74 | + convert_content_to_url(connection) |
| 75 | + except Exception as error: |
| 76 | + print(error) |
| 77 | + finally: |
| 78 | + connection.close() |
0 commit comments