opentechcalendartools 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl
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.
- opentechcalendartools/cli.py +11 -2
- opentechcalendartools/worker.py +74 -14
- {opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/METADATA +2 -1
- opentechcalendartools-0.2.0.dist-info/RECORD +9 -0
- {opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/WHEEL +1 -1
- opentechcalendartools-0.1.0.dist-info/RECORD +0 -9
- {opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/top_level.txt +0 -0
opentechcalendartools/cli.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import argparse
|
|
2
2
|
import os
|
|
3
3
|
|
|
4
|
+
import requests_cache
|
|
5
|
+
|
|
4
6
|
from .worker import Worker
|
|
5
7
|
|
|
6
8
|
|
|
@@ -28,13 +30,20 @@ def main():
|
|
|
28
30
|
|
|
29
31
|
args = parser.parse_args()
|
|
30
32
|
|
|
33
|
+
requests_session = None
|
|
34
|
+
requests_cache_directory = os.getenv(
|
|
35
|
+
"OPEN_TECH_CALENDAR_TOOLS_REQUEST_CACHE_DIRECTORY",
|
|
36
|
+
)
|
|
37
|
+
if requests_cache_directory:
|
|
38
|
+
requests_session = requests_cache.install_cache(requests_cache_directory)
|
|
39
|
+
|
|
40
|
+
worker = Worker(sqlite_database_filename, requests_session=requests_session)
|
|
41
|
+
|
|
31
42
|
if args.subparser_name == "listgroupstoimport":
|
|
32
43
|
# List groups to import
|
|
33
|
-
worker = Worker(sqlite_database_filename)
|
|
34
44
|
for group_id in worker.get_group_ids_to_import():
|
|
35
45
|
print(group_id)
|
|
36
46
|
|
|
37
47
|
elif args.subparser_name == "importgroup":
|
|
38
48
|
# Import Group
|
|
39
|
-
worker = Worker(sqlite_database_filename)
|
|
40
49
|
worker.import_group(args.group_id)
|
opentechcalendartools/worker.py
CHANGED
|
@@ -2,6 +2,7 @@ import datetime
|
|
|
2
2
|
import os
|
|
3
3
|
import sqlite3
|
|
4
4
|
import tempfile
|
|
5
|
+
import zoneinfo
|
|
5
6
|
|
|
6
7
|
import icalendar
|
|
7
8
|
import requests
|
|
@@ -10,9 +11,14 @@ import yaml
|
|
|
10
11
|
|
|
11
12
|
class Worker:
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
WORKER_USER_AGENT = (
|
|
15
|
+
"Open Tech Calendar Tools https://opentechcalendar.co.uk/contact"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def __init__(self, sqlite_database_filename, requests_session=None):
|
|
14
19
|
self._sqlite_database_filename = sqlite_database_filename
|
|
15
20
|
self._data_dir = os.getcwd()
|
|
21
|
+
self._requests_session = requests_session or requests.Session()
|
|
16
22
|
|
|
17
23
|
def get_group_ids_to_import(self) -> list:
|
|
18
24
|
with sqlite3.connect(self._sqlite_database_filename) as connection:
|
|
@@ -34,7 +40,10 @@ class Worker:
|
|
|
34
40
|
)
|
|
35
41
|
|
|
36
42
|
def _download_file_to_temp(self, url) -> str:
|
|
37
|
-
r =
|
|
43
|
+
r = self._requests_session.get(
|
|
44
|
+
url,
|
|
45
|
+
headers={"User-Agent": Worker.WORKER_USER_AGENT},
|
|
46
|
+
)
|
|
38
47
|
r.raise_for_status()
|
|
39
48
|
new_filename_dets = tempfile.mkstemp(
|
|
40
49
|
suffix="opentechcalendartools_",
|
|
@@ -43,31 +52,82 @@ class Worker:
|
|
|
43
52
|
os.close(new_filename_dets[0])
|
|
44
53
|
return new_filename_dets[1]
|
|
45
54
|
|
|
46
|
-
def
|
|
47
|
-
self, group,
|
|
48
|
-
):
|
|
55
|
+
def _should_import_event(
|
|
56
|
+
self, group: dict, title: str, description: str, end: datetime.datetime
|
|
57
|
+
) -> bool:
|
|
58
|
+
if end.timestamp() < datetime.datetime.now().timestamp():
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
if group["field_import_exclude"] and (
|
|
62
|
+
group["field_import_exclude"] in title
|
|
63
|
+
or group["field_import_exclude"] in description
|
|
64
|
+
):
|
|
65
|
+
return False
|
|
66
|
+
|
|
67
|
+
return True
|
|
68
|
+
|
|
69
|
+
def _import_group_type_ical(self, group, ical_filename):
|
|
49
70
|
os.makedirs(os.path.join(self._data_dir, "event", group["id"]), exist_ok=True)
|
|
50
71
|
with open(ical_filename) as fp:
|
|
51
72
|
calendar = icalendar.Calendar.from_ical(fp.read())
|
|
52
73
|
for event in calendar.events:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
timezone_name = group["field_timezone"] or "UTC"
|
|
75
|
+
start_datetime = event.get("DTSTART").dt
|
|
76
|
+
end_datetime = event.get("DTEND").dt
|
|
77
|
+
if isinstance(start_datetime, datetime.datetime):
|
|
78
|
+
pass
|
|
79
|
+
elif isinstance(start_datetime, datetime.date):
|
|
80
|
+
start_datetime = datetime.datetime(
|
|
81
|
+
start_datetime.year,
|
|
82
|
+
start_datetime.month,
|
|
83
|
+
start_datetime.day,
|
|
84
|
+
0,
|
|
85
|
+
0,
|
|
86
|
+
0,
|
|
87
|
+
tzinfo=zoneinfo.ZoneInfo(timezone_name),
|
|
88
|
+
)
|
|
89
|
+
else:
|
|
90
|
+
raise Exception(
|
|
91
|
+
"Start not in the format we expect {}".format(start_datetime)
|
|
92
|
+
)
|
|
93
|
+
if isinstance(end_datetime, datetime.datetime):
|
|
94
|
+
pass
|
|
95
|
+
elif isinstance(end_datetime, datetime.date):
|
|
96
|
+
end_datetime = datetime.datetime(
|
|
97
|
+
end_datetime.year,
|
|
98
|
+
end_datetime.month,
|
|
99
|
+
end_datetime.day,
|
|
100
|
+
23,
|
|
101
|
+
59,
|
|
102
|
+
59,
|
|
103
|
+
tzinfo=zoneinfo.ZoneInfo(timezone_name),
|
|
104
|
+
)
|
|
105
|
+
else:
|
|
106
|
+
raise Exception(
|
|
107
|
+
"End not in the format we expect {}".format(end_datetime)
|
|
108
|
+
)
|
|
109
|
+
if self._should_import_event(
|
|
110
|
+
group,
|
|
111
|
+
str(event.get("SUMMARY")),
|
|
112
|
+
str(event.get("DESCRIPTION")),
|
|
113
|
+
end_datetime,
|
|
114
|
+
):
|
|
56
115
|
# Create event data with various fields
|
|
57
116
|
event_data = {
|
|
58
117
|
"title": str(event.get("SUMMARY")),
|
|
59
118
|
"group": group["id"],
|
|
60
|
-
"timezone":
|
|
61
|
-
"start_at": str(
|
|
62
|
-
"end_at": str(
|
|
119
|
+
"timezone": timezone_name,
|
|
120
|
+
"start_at": str(start_datetime),
|
|
121
|
+
"end_at": str(end_datetime),
|
|
63
122
|
"url": str(event.get("URL")),
|
|
64
123
|
"cancelled": (event.get("STATUS") == "CANCELLED"),
|
|
65
124
|
"imported": True,
|
|
66
125
|
"community_participation": {
|
|
67
|
-
"at_event":
|
|
68
|
-
"at_event_audience_text":
|
|
69
|
-
"at_event_audience_audio":
|
|
126
|
+
"at_event": "unknown",
|
|
127
|
+
"at_event_audience_text": "unknown",
|
|
128
|
+
"at_event_audience_audio": "unknown",
|
|
70
129
|
},
|
|
130
|
+
"in_person": "unknown",
|
|
71
131
|
}
|
|
72
132
|
if group["field_country"]:
|
|
73
133
|
event_data["country"] = group["field_country"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: opentechcalendartools
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Home-page: https://github.com/TeacakeTech/opentechcalendar-tools
|
|
5
5
|
Author: Teacake Tech
|
|
6
6
|
Author-email: hello@teacaketech.scot
|
|
@@ -11,6 +11,7 @@ Requires-Python: >=3.12
|
|
|
11
11
|
License-File: LICENSE
|
|
12
12
|
Requires-Dist: datatig
|
|
13
13
|
Requires-Dist: requests
|
|
14
|
+
Requires-Dist: requests-cache
|
|
14
15
|
Requires-Dist: icalendar
|
|
15
16
|
Requires-Dist: pyyaml
|
|
16
17
|
Provides-Extra: dev
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
opentechcalendartools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
opentechcalendartools/__main__.py,sha256=k62u01s2PYzYpErY7unEc49JsxnOohcSOG_Gu9pFuYA,98
|
|
3
|
+
opentechcalendartools/cli.py,sha256=s35zGpAwqOVOsIAplNuGIGjXmc0GtBPJUbmhkuwHfz0,1458
|
|
4
|
+
opentechcalendartools/worker.py,sha256=Y8Pey6wvT1GGsBXT2uUOdlwz1HszTQ6AJsKOGfoXDLs,8358
|
|
5
|
+
opentechcalendartools-0.2.0.dist-info/licenses/LICENSE,sha256=t3-svmFJHEV_slEWg4lRY-LK523IOA56WXz62uOzGFI,1499
|
|
6
|
+
opentechcalendartools-0.2.0.dist-info/METADATA,sha256=S4URJ-wicKwsuh7oABCm7Ysi1VTzG0X5C5RCdGZUhQE,911
|
|
7
|
+
opentechcalendartools-0.2.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
8
|
+
opentechcalendartools-0.2.0.dist-info/top_level.txt,sha256=cXAUphHY7mCrtEnq3V6i_QyeWcbBEM75SJYjrUtDZ8E,22
|
|
9
|
+
opentechcalendartools-0.2.0.dist-info/RECORD,,
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
opentechcalendartools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
opentechcalendartools/__main__.py,sha256=k62u01s2PYzYpErY7unEc49JsxnOohcSOG_Gu9pFuYA,98
|
|
3
|
-
opentechcalendartools/cli.py,sha256=fR5XhuniYb15hOGzYmCHc2ORRxbhiB9KOEUZNtkiIx8,1201
|
|
4
|
-
opentechcalendartools/worker.py,sha256=hxn_gYiswPE9jwj0WEfE2wKuIeHdpr_bbB-KA9wQ5JM,6019
|
|
5
|
-
opentechcalendartools-0.1.0.dist-info/licenses/LICENSE,sha256=t3-svmFJHEV_slEWg4lRY-LK523IOA56WXz62uOzGFI,1499
|
|
6
|
-
opentechcalendartools-0.1.0.dist-info/METADATA,sha256=cXQwxc4QiEuS3Wq68jeUBpDT6pjFBasVuEHgh6QCcC4,881
|
|
7
|
-
opentechcalendartools-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
8
|
-
opentechcalendartools-0.1.0.dist-info/top_level.txt,sha256=cXAUphHY7mCrtEnq3V6i_QyeWcbBEM75SJYjrUtDZ8E,22
|
|
9
|
-
opentechcalendartools-0.1.0.dist-info/RECORD,,
|
{opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{opentechcalendartools-0.1.0.dist-info → opentechcalendartools-0.2.0.dist-info}/top_level.txt
RENAMED
|
File without changes
|