-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#568): TWIC scrape permission error
- Loading branch information
1 parent
88636d4
commit fd59b67
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import boto3 | ||
from boto3.dynamodb.conditions import Key | ||
from boto3.dynamodb.types import TypeDeserializer | ||
import traceback | ||
import json | ||
|
||
db = boto3.resource('dynamodb') | ||
table = db.Table('dev-games') | ||
td = TypeDeserializer() | ||
|
||
|
||
def handle_items(items, known_ids, batch): | ||
deleted = 0 | ||
for item in items: | ||
if item['id'] not in known_ids and item['headers'].get('TwicArchive', None) == '1549': | ||
batch.delete_item(Key={ | ||
'cohort': 'masters', | ||
'id': item['id'], | ||
}) | ||
deleted += 1 | ||
return deleted | ||
|
||
|
||
|
||
def main(): | ||
try: | ||
known_ids = set() | ||
|
||
with open('twic_games_1549.json') as file: | ||
for line in file: | ||
game = line.strip() | ||
game = json.loads(game) | ||
game = td.deserialize({'M': game['Item']}) | ||
known_ids.add(game['id']) | ||
|
||
lastKey = None | ||
deleted = 0 | ||
|
||
res = table.query( | ||
KeyConditionExpression='cohort = :masters and id > :id', | ||
ExpressionAttributeValues={ | ||
':masters': 'masters', | ||
':id': '2024.06.25', | ||
} | ||
) | ||
items = res.get('Items', []) | ||
lastKey = res.get('LastEvaluatedKey', None) | ||
|
||
with table.batch_writer() as batch: | ||
deleted += handle_items(items, known_ids, batch) | ||
|
||
while lastKey != None: | ||
res = table.query( | ||
KeyConditionExpression='cohort = :masters and id > :id', | ||
ExpressionAttributeValues={ | ||
':masters': 'masters', | ||
':id': '2024.06.25', | ||
}, | ||
ExclusiveStartKey=lastKey | ||
) | ||
items = res.get('Items', []) | ||
lastKey = res.get('LastEvaluatedKey', None) | ||
|
||
deleted += handle_items(items, known_ids, batch) | ||
|
||
except Exception as e: | ||
print(e) | ||
traceback.print_exc() | ||
|
||
print('Deleted: ', deleted) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |