Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Outputs
*.md
*.png

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ and the format to convert the material to.

## Remaining development

- Only return blocks and lectures which are published, and the latest version (normally only the latest version is published, but not necessarily).
- [ ] Only return blocks and lectures which are published, and the latest version (normally only the latest version is published, but not necessarily).
Currently the relations store all blocks, including earlier versions of the same block, that are included in a lecture.
The same for lectures and courses.
- We need to re-do the embedding of images in the markdown documents. Images are stored in the media library, but are then uploaded to an amazon server, so these can be downloaded from the urls embedded in the document and then redirected to a local location.
- [ ] We need to re-do the embedding of images in the markdown documents. Images are stored in the media library, but are then uploaded to an amazon server, so these can be downloaded from the urls embedded in the document and then redirected to a local location.
14 changes: 10 additions & 4 deletions get_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
>>> python get_block.py

"""
from matplotlib.widgets import Slider, SliderBase
import requests
from typing import Dict, Union, Any
from json import loads, dumps

def get_lecture_block(id: str):
"""Retrieves the contents of a lecture block from the teaching kit website

Arguments
---------
id (str): The id of the lecture block to retrieve
"""
url = f"https://teachingkit.climatecompatiblegrowth.com/api/blocks/{id}?locale=en&populate=*"
response = requests.get(url)
url = f"https://teachingkit.climatecompatiblegrowth.com/api/blocks/{id}"
payload = {'locale': 'en', 'populate': '*'}
response = requests.get(url, params=payload)

return response.json()

Expand All @@ -32,11 +33,16 @@ def print_keys(dict: Union[Dict, Any]):


if __name__ == "__main__":
block = get_lecture_block(4)
block = get_lecture_block(5)
attributes = block['data']['attributes']

print(f"Attributes: {attributes.keys()}")

print(f"Version number: {attributes['versionNumber']}")
print(f"Version: {[x['id'] for x in attributes['versions']['data']]}")

print(f"Published at: {attributes['publishedAt']}")

lectures = [x['attributes'] for x in attributes['Lectures']['data']]
for lecture in lectures:
print(f"This block is part of lecture '{lecture['Title']}'")
Expand Down
20 changes: 4 additions & 16 deletions get_course.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def get_course(id: str):
---------
id (str): The id of the course to retrieve
"""
url = f"https://teachingkit.climatecompatiblegrowth.com/api/courses/{id}?locale=en&populate=*"
response = requests.get(url)
url = f"https://teachingkit.climatecompatiblegrowth.com/api/courses/{id}"
payload = {'locale': 'en', 'populate': '*'}
response = requests.get(url, params=payload)

return response.json()

Expand All @@ -43,6 +44,7 @@ def print_keys(dict: Union[Dict, Any]):

title = attributes['Title']
print(f"Course title: {title}")


outcomes = [x['LearningOutcome'] for x in attributes['LearningOutcomes']]
print(f"Outcomes: {outcomes}")
Expand All @@ -52,17 +54,3 @@ def print_keys(dict: Union[Dict, Any]):
print(f"This course contains lecture: '{lecture['id']}: {lecture['attributes']['Title']}'")


# authors = [x['attributes'] for x in attributes['LectureCreators']['data']]

# print("This lecture lecture was written by:")
# for author in authors:
# print(f"{author['FirstName']} {author['LastName']} {author['Email']} {author['ORCID']}")



# lecture_id = lecture['data']['id']
# document = attributes['Abstract']
# with open(f"lecture_{lecture_id}.md", 'wt') as markdown_file:
# markdown_file.write(f"# {title}\n\n")
# markdown_file.write(document)

50 changes: 50 additions & 0 deletions get_keywrd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.probability import FreqDist
import markdown

from get_lecture import get_lecture

# Download NLTK data if not already downloaded
nltk.download('punkt')
nltk.download('stopwords')

# Function to extract keywords from a text
def extract_keywords(text):
# Tokenize the text
words = word_tokenize(text.lower())

# Remove stopwords and punctuation
stopwords_set = set(stopwords.words('english'))
words = [word for word in words if word.isalnum() and word not in stopwords_set]

# Calculate word frequency
word_freq = FreqDist(words)

# Select the top 10 most frequent words as keywords
keywords = [word for word, _ in word_freq.most_common(10)]

return keywords

#Extract keywords from the lecture note
keywords = extract_keywords('get_lecture')

# Generate Markdown content with keywords
#markdown_content = "\n".join([f"- {keyword}" for keyword in keywords])

# Save the Markdown content to a file
#with open("keywords.md", "w") as file:
#file.write(markdown_content)

#print("Keywords extracted and saved to 'keywords.md'")

# Optional: Print the extracted keywords
#print("Extracted Keywords:")
for keyword in keywords:
print(keyword)





22 changes: 14 additions & 8 deletions get_lecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

>>> python get_lecture.py


"""
import requests
#import keywords
from typing import Dict, Union, Any


Expand All @@ -17,8 +17,9 @@ def get_lecture(id: str):
---------
id (str): The id of the lecture block to retrieve
"""
url = f"https://teachingkit.climatecompatiblegrowth.com/api/lectures/{id}?locale=en&populate=*"
response = requests.get(url)
url = f"https://teachingkit.climatecompatiblegrowth.com/api/lectures/{id}"
payload = {'locale': 'en', 'populate': '*'}
response = requests.get(url, params=payload)

return response.json()

Expand All @@ -39,10 +40,13 @@ def print_keys(dict: Union[Dict, Any]):

print(f"Attributes: {attributes.keys()}")

print(f"Version number: {attributes['versionNumber']}")
print(f"Version: {attributes['versions']}")

blocks = attributes['Blocks']['data']
print(blocks)
# print(blocks)
for block in blocks:
print(f"This lecture contains block {block['id']}: '{block['attributes']['Title']}'")
print(f"This lecture contains block {block['id']}: '{block['attributes']['Title']}'") #'{block['attributes']['keywords']}")

authors = [x['attributes'] for x in attributes['LectureCreators']['data']]

Expand All @@ -56,10 +60,12 @@ def print_keys(dict: Union[Dict, Any]):

title = attributes['Title']
print(title)
keywords = attributes['keywords']
print(keywords)

lecture_id = lecture['data']['id']
document = attributes['Abstract']
with open(f"lecture_{lecture_id}.md", 'wt') as markdown_file:
markdown_file.write(f"# {title}\n\n")
markdown_file.write(document)
# with open(f"lecture_{lecture_id}.md", 'wt') as markdown_file:
# markdown_file.write(f"# {title}\n\n")
# markdown_file.write(document)

8 changes: 8 additions & 0 deletions test_write_out_course.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from write_out_course import extract_urls

def test_extract_urls():

test_string = "![Fig_1.4.2b.png](https://test.com/1.png)![Fig_1.4.2c.png](https://test.com/2.png)"
urls = extract_urls(test_string)
print(urls)
assert len(urls) == 2
Loading