Dump follow restriction data to a local human-readable JSON
(profile_name, logger, logfolder)
| 1171 | |
| 1172 | |
| 1173 | def dump_follow_restriction(profile_name, logger, logfolder): |
| 1174 | """Dump follow restriction data to a local human-readable JSON""" |
| 1175 | |
| 1176 | conn = None |
| 1177 | |
| 1178 | try: |
| 1179 | # get a DB and start a connection |
| 1180 | db, id = get_database() |
| 1181 | conn = sqlite3.connect(db) |
| 1182 | |
| 1183 | with conn: |
| 1184 | conn.row_factory = sqlite3.Row |
| 1185 | cur = conn.cursor() |
| 1186 | |
| 1187 | cur.execute( |
| 1188 | "SELECT * FROM followRestriction WHERE profile_id=:var", {"var": id} |
| 1189 | ) |
| 1190 | data = cur.fetchall() |
| 1191 | |
| 1192 | if data: |
| 1193 | # get the existing data |
| 1194 | filename = "{}followRestriction.json".format(logfolder) |
| 1195 | if os.path.isfile(filename): |
| 1196 | with open(filename) as followResFile: |
| 1197 | current_data = json.load(followResFile) |
| 1198 | else: |
| 1199 | current_data = {} |
| 1200 | |
| 1201 | # pack the new data |
| 1202 | follow_data = {user_data[1]: user_data[2] for user_data in data or []} |
| 1203 | current_data[profile_name] = follow_data |
| 1204 | |
| 1205 | # dump the fresh follow data to a local human readable JSON |
| 1206 | with open(filename, "w") as followResFile: |
| 1207 | json.dump(current_data, followResFile) |
| 1208 | |
| 1209 | except Exception as exc: |
| 1210 | logger.error( |
| 1211 | "Pow! Error occurred while dumping follow restriction data to a " |
| 1212 | "local JSON:\n\t{}".format(str(exc).encode("utf-8")) |
| 1213 | ) |
| 1214 | |
| 1215 | finally: |
| 1216 | if conn: |
| 1217 | # close the open connection |
| 1218 | conn.close() |
| 1219 | |
| 1220 | |
| 1221 | def follow_restriction(operation, username, limit, logger): |