LoginSignup
0
1

More than 3 years have passed since last update.

サーバレスでRSSを取得しDynamoDBに登録する30行

Posted at

とあるサイトの情報をRSSで取得し、DynamoDBに突っ込むだけの処理。最初、ライブラリとかをアップロードしないとできないかなと思ったが、標準ライブラリだけで大丈夫だった。CloudWatch Eventsから起動して使う。

import urllib.request
import xml.etree.ElementTree as ET

import boto3
import json
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('xxxxxxxx')

def lambda_handler(event, context):
    cnt = 0
    url = 'https://note.mu/usop/m/m79fbb598dd82/rss'
    with urllib.request.urlopen(url) as response:
        html = response.read()
        root = ET.fromstring(html)        
        for item in root.iter('item'):
            link = item.find('link').text
            res = table.scan(
                FilterExpression=Attr('url').eq(link)
            )
            if res["Count"] == 0:
                resall = table.scan()
                cnt = resall["Count"]
                table.put_item(Item={"id":cnt+1,"url":link})
    return {
        'count': cnt
    }
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1