File size: 3,633 Bytes
cc93546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import requests
import json
import os
from typing import List

class DiscussionEntry:
    def __init__(self, id: int, parent_id: int, name: str, message: str, replies: List):
        self.id = id
        self.parent_id = parent_id
        self.name = name
        self.message = message
        self.replies = replies

    def to_json(self):
        return {
            'id': self.id,
            'parent_id': self.parent_id,
            'name': self.name,
            'message': self.message,
            'replies': [reply.to_json() for reply in self.replies]
        }

def extract_entries(entries, participants):
    result = []
    for entry in entries:
        if 'message' in entry and 'deleted' not in entry:
            id = entry['id']
            parent_id = entry['parent_id']
            user_id = entry['user_id']
            name = next((p['display_name'] for p in participants if p['id'] == user_id), None)
            message = entry['message']
        replies = []
        if 'replies' in entry:
            replies = extract_entries(entry['replies'], participants)
        result.append(DiscussionEntry(id, parent_id, name, message, replies))
    return result

def save_messages(entries):

    for entry in entries:
        # Save the message as an HTML file
        filename = f'docs/{entry.name}.html'

        # Open file in write/append mode
        with open(filename, 'a+') as f:
            if  entry.parent_id == None:
                f.write(f'<p><b>Student Post: {entry.name}</b></p>')
                f.write(entry.message)
                f.write('<hr>')
            else:
                f.write(f'<p><b>Reply to: {entry.parent_id}</b></p>')
                f.write(entry.message)
                f.write('<hr>')


        # Save the messages of the replies
    for entry in entries:

        save_messages(entry.replies)

# Replace these variables with your own information
access_token = ''
course_id = '36263'
discussion_topic_id = '421517'
base_url = 'https://canvas.illinois.edu'

headers = {
    'Authorization': f'Bearer {access_token}'
}

# Retrieve the full discussion topic data
discussion_url = f'{base_url}/api/v1/courses/{course_id}/discussion_topics/{discussion_topic_id}/view'
discussion_response = requests.get(discussion_url, headers=headers)

if discussion_response.ok:
    discussion_data = discussion_response.json()
    with open('discussion_data.json', 'w') as f:
        json.dump(discussion_data, f)

    # Extract the desired fields from the replies and responses
    entries = extract_entries(discussion_data['view'], discussion_data['participants'])

    # Save the extracted data to a file
    with open('discussion_entries.json', 'w') as f:
        json.dump([entry.to_json() for entry in entries], f)

    # Create the /docs directory if it does not exist
    os.makedirs('docs', exist_ok=True)

    # Save the messages as HTML files under the /docs directory
    save_messages(entries)

    # Extract the rubric and save it to a file
    if 'rubric' in discussion_data:
        rubric = discussion_data['rubric']
        with open('rubric.json', 'w') as f:
            json.dump(rubric, f)
else:
    print(f'Error: {discussion_response.text}')

rubric_url = f'{base_url}/api/v1/courses/{course_id}/discussion_topics/{discussion_topic_id}'
rubric_response = requests.get(rubric_url, headers=headers)

if rubric_response.ok:
    rubric_data = rubric_response.json()
    # print(rubric_data)
    if 'rubric' in rubric_data['assignment']:
        rubric = rubric_data['assignment']['rubric']
        with open('rubric_data.json', 'w') as f:
            json.dump(rubric, f)