opentechcalendartools 0.0.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: opentechcalendartools
3
- Version: 0.0.0
3
+ Version: 0.1.1
4
4
  Home-page: https://github.com/TeacakeTech/opentechcalendar-tools
5
5
  Author: Teacake Tech
6
6
  Author-email: hello@teacaketech.scot
@@ -0,0 +1,165 @@
1
+ import datetime
2
+ import os
3
+ import sqlite3
4
+ import tempfile
5
+ import zoneinfo
6
+
7
+ import icalendar
8
+ import requests
9
+ import yaml
10
+
11
+
12
+ class Worker:
13
+
14
+ def __init__(self, sqlite_database_filename):
15
+ self._sqlite_database_filename = sqlite_database_filename
16
+ self._data_dir = os.getcwd()
17
+
18
+ def get_group_ids_to_import(self) -> list:
19
+ with sqlite3.connect(self._sqlite_database_filename) as connection:
20
+ res = connection.cursor().execute(
21
+ "SELECT id FROM record_group WHERE field_import_type != '' AND field_import_type IS NOT NULL ORDER BY id ASC"
22
+ )
23
+ return [i[0] for i in res.fetchall()]
24
+
25
+ def import_group(self, group_id):
26
+ with sqlite3.connect(self._sqlite_database_filename) as connection:
27
+ connection.row_factory = sqlite3.Row
28
+ cursor = connection.cursor()
29
+ res = cursor.execute("SELECT * FROM record_group WHERE id=?", [group_id])
30
+ group = res.fetchone()
31
+ if group["field_import_type"] == "ical":
32
+ self._import_group_type_ical(
33
+ group,
34
+ self._download_file_to_temp(group["field_import_url"]),
35
+ )
36
+
37
+ def _download_file_to_temp(self, url) -> str:
38
+ r = requests.get(url)
39
+ r.raise_for_status()
40
+ new_filename_dets = tempfile.mkstemp(
41
+ suffix="opentechcalendartools_",
42
+ )
43
+ os.write(new_filename_dets[0], r.content)
44
+ os.close(new_filename_dets[0])
45
+ return new_filename_dets[1]
46
+
47
+ def _import_group_type_ical(self, group, ical_filename):
48
+ os.makedirs(os.path.join(self._data_dir, "event", group["id"]), exist_ok=True)
49
+ with open(ical_filename) as fp:
50
+ calendar = icalendar.Calendar.from_ical(fp.read())
51
+ for event in calendar.events:
52
+ timezone_name = group["field_timezone"] or "UTC"
53
+ start_datetime = event.get("DTSTART").dt
54
+ end_datetime = event.get("DTEND").dt
55
+ if isinstance(start_datetime, datetime.datetime):
56
+ pass
57
+ elif isinstance(start_datetime, datetime.date):
58
+ start_datetime = datetime.datetime(
59
+ start_datetime.year,
60
+ start_datetime.month,
61
+ start_datetime.day,
62
+ 0,
63
+ 0,
64
+ 0,
65
+ tzinfo=zoneinfo.ZoneInfo(timezone_name),
66
+ )
67
+ else:
68
+ raise Exception(
69
+ "Start not in the format we expect {}".format(start_datetime)
70
+ )
71
+ if isinstance(end_datetime, datetime.datetime):
72
+ pass
73
+ elif isinstance(end_datetime, datetime.date):
74
+ end_datetime = datetime.datetime(
75
+ end_datetime.year,
76
+ end_datetime.month,
77
+ end_datetime.day,
78
+ 23,
79
+ 59,
80
+ 59,
81
+ tzinfo=zoneinfo.ZoneInfo(timezone_name),
82
+ )
83
+ else:
84
+ raise Exception(
85
+ "End not in the format we expect {}".format(end_datetime)
86
+ )
87
+ if end_datetime.timestamp() > datetime.datetime.now().timestamp():
88
+ # Create event data with various fields
89
+ event_data = {
90
+ "title": str(event.get("SUMMARY")),
91
+ "group": group["id"],
92
+ "timezone": timezone_name,
93
+ "start_at": str(start_datetime),
94
+ "end_at": str(end_datetime),
95
+ "url": str(event.get("URL")),
96
+ "cancelled": (event.get("STATUS") == "CANCELLED"),
97
+ "imported": True,
98
+ "community_participation": {
99
+ "at_event": "unknown",
100
+ "at_event_audience_text": "unknown",
101
+ "at_event_audience_audio": "unknown",
102
+ },
103
+ "in_person": "unknown",
104
+ }
105
+ if group["field_country"]:
106
+ event_data["country"] = group["field_country"]
107
+ if group["field_place"]:
108
+ event_data["place"] = group["field_place"]
109
+ if group["field_code_of_conduct_url"]:
110
+ event_data["code_of_conduct_url"] = group[
111
+ "field_code_of_conduct_url"
112
+ ]
113
+ # In-person events
114
+ if group["field_in_person"] == "all":
115
+ event_data["in_person"] = "yes"
116
+ elif group["field_in_person"] == "none":
117
+ event_data["in_person"] = "no"
118
+ # Community Participation: Interact with event?
119
+ if group["field_community_participation_at_event"] == "all":
120
+ event_data["community_participation"]["at_event"] = "yes"
121
+ elif group["field_community_participation_at_event"] == "none":
122
+ event_data["community_participation"]["at_event"] = "no"
123
+ # Community Participation: Interact with other audience members at the event via text?
124
+ if (
125
+ group["field_community_participation_at_event_audience_text"]
126
+ == "all"
127
+ ):
128
+ event_data["community_participation"][
129
+ "at_event_audience_text"
130
+ ] = "yes"
131
+ elif (
132
+ group["field_community_participation_at_event_audience_text"]
133
+ == "none"
134
+ ):
135
+ event_data["community_participation"][
136
+ "at_event_audience_text"
137
+ ] = "no"
138
+ # Community Participation: Interact with other audience members at the event via audio?
139
+ if (
140
+ group["field_community_participation_at_event_audience_audio"]
141
+ == "all"
142
+ ):
143
+ event_data["community_participation"][
144
+ "at_event_audience_audio"
145
+ ] = "yes"
146
+ elif (
147
+ group["field_community_participation_at_event_audience_audio"]
148
+ == "none"
149
+ ):
150
+ event_data["community_participation"][
151
+ "at_event_audience_audio"
152
+ ] = "no"
153
+ # Id
154
+ id = event.get("UID").split("@").pop(0)
155
+ # filename
156
+ filename = os.path.join(
157
+ self._data_dir, "event", group["id"], id + ".md"
158
+ )
159
+ # Finally write data
160
+ with open(filename, "w") as fp:
161
+ fp.write("---\n")
162
+ fp.write(yaml.dump(event_data))
163
+ fp.write("---\n\n\n")
164
+ fp.write(event.get("DESCRIPTION"))
165
+ fp.write("\n")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: opentechcalendartools
3
- Version: 0.0.0
3
+ Version: 0.1.1
4
4
  Home-page: https://github.com/TeacakeTech/opentechcalendar-tools
5
5
  Author: Teacake Tech
6
6
  Author-email: hello@teacaketech.scot
@@ -2,7 +2,7 @@ import setuptools
2
2
 
3
3
  setuptools.setup(
4
4
  name="opentechcalendartools",
5
- version="0.0.0",
5
+ version="0.1.1",
6
6
  description="",
7
7
  url="https://github.com/TeacakeTech/opentechcalendar-tools",
8
8
  project_urls={
@@ -1,79 +0,0 @@
1
- import datetime
2
- import os
3
- import sqlite3
4
- import tempfile
5
-
6
- import icalendar
7
- import requests
8
- import yaml
9
-
10
-
11
- class Worker:
12
-
13
- def __init__(self, sqlite_database_filename):
14
- self._sqlite_database_filename = sqlite_database_filename
15
- self._data_dir = os.getcwd()
16
-
17
- def get_group_ids_to_import(self) -> list:
18
- with sqlite3.connect(self._sqlite_database_filename) as connection:
19
- res = connection.cursor().execute(
20
- "SELECT id FROM record_group WHERE field_import_type != '' AND field_import_type IS NOT NULL ORDER BY id ASC"
21
- )
22
- return [i[0] for i in res.fetchall()]
23
-
24
- def import_group(self, group_id):
25
- with sqlite3.connect(self._sqlite_database_filename) as connection:
26
- connection.row_factory = sqlite3.Row
27
- cursor = connection.cursor()
28
- res = cursor.execute("SELECT * FROM record_group WHERE id=?", [group_id])
29
- group = res.fetchone()
30
- if group["field_import_type"] == "ical":
31
- self._import_group_type_ical(
32
- group_id,
33
- self._download_file_to_temp(group["field_import_url"]),
34
- group_country=group["field_country"],
35
- group_place=group["field_place"],
36
- )
37
-
38
- def _download_file_to_temp(self, url) -> str:
39
- r = requests.get(url)
40
- r.raise_for_status()
41
- new_filename_dets = tempfile.mkstemp(
42
- suffix="opentechcalendartools_",
43
- )
44
- os.write(new_filename_dets[0], r.content)
45
- os.close(new_filename_dets[0])
46
- return new_filename_dets[1]
47
-
48
- def _import_group_type_ical(
49
- self, group_id, ical_filename, group_country=None, group_place=None
50
- ):
51
- os.makedirs(os.path.join(self._data_dir, "event", group_id), exist_ok=True)
52
- with open(ical_filename) as fp:
53
- calendar = icalendar.Calendar.from_ical(fp.read())
54
- for event in calendar.events:
55
- start = event.get("DTSTART")
56
- end = event.get("DTEND")
57
- if end.dt.timestamp() > datetime.datetime.now().timestamp():
58
- event_data = {
59
- "title": str(event.get("SUMMARY")),
60
- "group": group_id,
61
- "start_at": str(start.dt),
62
- "end_at": str(end.dt),
63
- "url": str(event.get("URL")),
64
- "cancelled": (event.get("STATUS") == "CANCELLED"),
65
- "imported": True,
66
- }
67
- if group_country:
68
- event_data["country"] = group_country
69
- if group_place:
70
- event_data["place"] = group_place
71
- id = event.get("UID").split("@").pop(0)
72
- filename = os.path.join(
73
- self._data_dir, "event", group_id, id + ".md"
74
- )
75
- with open(filename, "w") as fp:
76
- fp.write("---\n")
77
- fp.write(yaml.dump(event_data))
78
- fp.write("---\n\n\n")
79
- fp.write(event.get("DESCRIPTION"))