opentechcalendartools 0.0.0__tar.gz → 0.1.0__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.0
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,132 @@
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,
33
+ self._download_file_to_temp(group["field_import_url"]),
34
+ )
35
+
36
+ def _download_file_to_temp(self, url) -> str:
37
+ r = requests.get(url)
38
+ r.raise_for_status()
39
+ new_filename_dets = tempfile.mkstemp(
40
+ suffix="opentechcalendartools_",
41
+ )
42
+ os.write(new_filename_dets[0], r.content)
43
+ os.close(new_filename_dets[0])
44
+ return new_filename_dets[1]
45
+
46
+ def _import_group_type_ical(
47
+ self, group, ical_filename, group_country=None, group_place=None
48
+ ):
49
+ os.makedirs(os.path.join(self._data_dir, "event", group["id"]), exist_ok=True)
50
+ with open(ical_filename) as fp:
51
+ calendar = icalendar.Calendar.from_ical(fp.read())
52
+ for event in calendar.events:
53
+ start = event.get("DTSTART")
54
+ end = event.get("DTEND")
55
+ if end.dt.timestamp() > datetime.datetime.now().timestamp():
56
+ # Create event data with various fields
57
+ event_data = {
58
+ "title": str(event.get("SUMMARY")),
59
+ "group": group["id"],
60
+ "timezone": group["field_timezone"] or "UTC",
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
+ "community_participation": {
67
+ "at_event": None,
68
+ "at_event_audience_text": None,
69
+ "at_event_audience_audio": None,
70
+ },
71
+ }
72
+ if group["field_country"]:
73
+ event_data["country"] = group["field_country"]
74
+ if group["field_place"]:
75
+ event_data["place"] = group["field_place"]
76
+ if group["field_code_of_conduct_url"]:
77
+ event_data["code_of_conduct_url"] = group[
78
+ "field_code_of_conduct_url"
79
+ ]
80
+ # In-person events
81
+ if group["field_in_person"] == "all":
82
+ event_data["in_person"] = "yes"
83
+ elif group["field_in_person"] == "none":
84
+ event_data["in_person"] = "no"
85
+ # Community Participation: Interact with event?
86
+ if group["field_community_participation_at_event"] == "all":
87
+ event_data["community_participation"]["at_event"] = "yes"
88
+ elif group["field_community_participation_at_event"] == "none":
89
+ event_data["community_participation"]["at_event"] = "no"
90
+ # Community Participation: Interact with other audience members at the event via text?
91
+ if (
92
+ group["field_community_participation_at_event_audience_text"]
93
+ == "all"
94
+ ):
95
+ event_data["community_participation"][
96
+ "at_event_audience_text"
97
+ ] = "yes"
98
+ elif (
99
+ group["field_community_participation_at_event_audience_text"]
100
+ == "none"
101
+ ):
102
+ event_data["community_participation"][
103
+ "at_event_audience_text"
104
+ ] = "no"
105
+ # Community Participation: Interact with other audience members at the event via audio?
106
+ if (
107
+ group["field_community_participation_at_event_audience_audio"]
108
+ == "all"
109
+ ):
110
+ event_data["community_participation"][
111
+ "at_event_audience_audio"
112
+ ] = "yes"
113
+ elif (
114
+ group["field_community_participation_at_event_audience_audio"]
115
+ == "none"
116
+ ):
117
+ event_data["community_participation"][
118
+ "at_event_audience_audio"
119
+ ] = "no"
120
+ # Id
121
+ id = event.get("UID").split("@").pop(0)
122
+ # filename
123
+ filename = os.path.join(
124
+ self._data_dir, "event", group["id"], id + ".md"
125
+ )
126
+ # Finally write data
127
+ with open(filename, "w") as fp:
128
+ fp.write("---\n")
129
+ fp.write(yaml.dump(event_data))
130
+ fp.write("---\n\n\n")
131
+ fp.write(event.get("DESCRIPTION"))
132
+ 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.0
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.0",
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"))