Dump the record activity data to a local human-readable JSON
(profile_name, logger, logfolder)
| 1458 | |
| 1459 | |
| 1460 | def dump_record_activity(profile_name, logger, logfolder): |
| 1461 | """Dump the record activity data to a local human-readable JSON""" |
| 1462 | |
| 1463 | conn = None |
| 1464 | |
| 1465 | try: |
| 1466 | # get a DB and start a connection |
| 1467 | db, id = get_database() |
| 1468 | conn = sqlite3.connect(db) |
| 1469 | |
| 1470 | with conn: |
| 1471 | conn.row_factory = sqlite3.Row |
| 1472 | cur = conn.cursor() |
| 1473 | |
| 1474 | cur.execute( |
| 1475 | "SELECT * FROM recordActivity WHERE profile_id=:var", {"var": id} |
| 1476 | ) |
| 1477 | user_data = cur.fetchall() |
| 1478 | |
| 1479 | if user_data: |
| 1480 | ordered_user_data = {} |
| 1481 | current_data = {} |
| 1482 | |
| 1483 | # get the existing data |
| 1484 | filename = "{}recordActivity.json".format(logfolder) |
| 1485 | if os.path.isfile(filename): |
| 1486 | with open(filename) as recordActFile: |
| 1487 | current_data = json.load(recordActFile) |
| 1488 | |
| 1489 | # re-order live user data in the required structure |
| 1490 | for hourly_data in user_data: |
| 1491 | hourly_data = tuple(hourly_data) |
| 1492 | day = hourly_data[-1][:10] |
| 1493 | hour = hourly_data[-1][-8:-6] |
| 1494 | |
| 1495 | if day not in ordered_user_data.keys(): |
| 1496 | ordered_user_data.update({day: {}}) |
| 1497 | |
| 1498 | ordered_user_data[day].update( |
| 1499 | { |
| 1500 | hour: { |
| 1501 | "likes": hourly_data[1], |
| 1502 | "comments": hourly_data[2], |
| 1503 | "follows": hourly_data[3], |
| 1504 | "unfollows": hourly_data[4], |
| 1505 | "server_calls": hourly_data[5], |
| 1506 | } |
| 1507 | } |
| 1508 | ) |
| 1509 | |
| 1510 | # update user data with live data whilst preserving all other |
| 1511 | # data (keys) |
| 1512 | current_data.update({profile_name: ordered_user_data}) |
| 1513 | |
| 1514 | # dump the fresh record data to a local human readable JSON |
| 1515 | with open(filename, "w") as recordActFile: |
| 1516 | json.dump(current_data, recordActFile) |
| 1517 |