Skip to content

Commit

Permalink
Merged scrap and upload
Browse files Browse the repository at this point in the history
  • Loading branch information
draescherl committed Oct 7, 2022
1 parent 9129409 commit 1eb0ec2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
10 changes: 5 additions & 5 deletions Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@


class Parser:
def __init__(self, _csvContent):
self.csvContent = StringIO(_csvContent)
def __init__(self, _csv_content):
self.csvContent = StringIO(_csv_content)

def parse_week(self, weekLines):
dates, content = weekLines
def parse_week(self, week_lines):
dates, content = week_lines
lessons = []
try:
for i in range(len(dates)):
Expand All @@ -17,7 +17,7 @@ def parse_week(self, weekLines):
new_lesson = self.parse_day(dates[i], content[i])
if new_lesson:
lessons.append(new_lesson)
except:
except Exception:
print("An exception occurred during the parsing process")
return lessons

Expand Down
25 changes: 24 additions & 1 deletion scrap.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import shutil
import os
import shutil
import requests
from os.path import isfile, join
from ics import Calendar
from boto.s3.key import Key
from boto.s3.connection import S3Connection
from boto.s3.connection import OrdinaryCallingFormat

from Parser import Parser
from models.db import Timetables
from utils.base import Base, Session, engine

api_key = os.environ.get('CELLAR_ADDON_KEY_ID')
secret_key = os.environ.get('CELLAR_ADDON_KEY_SECRET')
host = os.environ.get('CELLAR_ADDON_HOST')

Base.metadata.create_all(engine)
session = Session()

Expand All @@ -30,3 +38,18 @@
lines = c.serialize_iter()
lines = lines[:-2] + ["REFRESH-INTERVAL;VALUE=DURATION:P1H\n"] + lines[-1:]
f.writelines(lines)

cf = OrdinaryCallingFormat() # Can't use uppercase name
conn = S3Connection(aws_access_key_id=api_key, aws_secret_access_key=secret_key, host=host, calling_format=cf)

bucket = conn.get_bucket(os.environ.get('BUCKET_NAME'))
bucket.set_acl('public-read')

directory = os.environ.get('ICS_FOLDER_NAME')
calendars = [f for f in os.listdir(directory) if isfile(join(directory, f))]

for calendar in calendars:
key = Key(bucket)
key.key = calendar
key.set_contents_from_filename(f'{directory}/{calendar}')
key.set_acl('public-read')
25 changes: 0 additions & 25 deletions upload.py

This file was deleted.

0 comments on commit 1eb0ec2

Please sign in to comment.