|
| 1 | +import os |
| 2 | +from requests import get, post |
| 3 | + |
| 4 | +SPEAKERS_CORNER_SEMINAR_SERIES = {"series_id": "speakerscorner", |
| 5 | + "name": "Speakers\' Corner", |
| 6 | + "is_conference": False, |
| 7 | + "topics": [""], # TODO: Get a list of topics |
| 8 | + "language": "en", |
| 9 | + "institutions": [""], |
| 10 | + "timezone": "UTC", |
| 11 | + "homepage": "https://virtualscienceforum.org/speakerscorner.md", |
| 12 | + "visibility": 1, # 0=private, 1=unlisted, 2=public |
| 13 | + "access_control": 0, # 0=open, see schema for more |
| 14 | + "slots": [""], |
| 15 | + "organizers": [{"name": "Virtual Science Forum", |
| 16 | + "email": "vsf@virtualscienceforum.org", |
| 17 | + "homepage": "https://virtualscienceforum.org", |
| 18 | + "organizer": True, |
| 19 | + "order": 0, |
| 20 | + "display": True}]} |
| 21 | + |
| 22 | + |
| 23 | +def find_seminar_series(series_id): |
| 24 | + url = f"https://researchseminars.org/api/0/search/series?series_id={series_id}" |
| 25 | + r = get(url) |
| 26 | + if r.status_code == 200: |
| 27 | + J = r.json() |
| 28 | + results = J["properties"]["results"] |
| 29 | + return (len(results) != 0) |
| 30 | + |
| 31 | + |
| 32 | +def create_seminar_series(payload, authorization): |
| 33 | + url = "https://researchseminars.org/api/0/save/series/" |
| 34 | + r = post(url, json=payload, headers={"authorization":authorization}) |
| 35 | + J = r.json() |
| 36 | + code = J.get("code") |
| 37 | + |
| 38 | + if r.status_code == 200: |
| 39 | + if code == "warning": |
| 40 | + return True, J["warnings"] |
| 41 | + else: |
| 42 | + return True, "" |
| 43 | + else: |
| 44 | + return False, "" |
| 45 | + |
| 46 | + |
| 47 | +def edit_seminar_series(name, payload, authorization): |
| 48 | + url = "https://researchseminars.org/api/0/save/series/" |
| 49 | + r = post(url, json=payload, headers={"authorization":authorization}) |
| 50 | + J = r.json() |
| 51 | + code = J.get("code") |
| 52 | + |
| 53 | + if r.status_code == 200: |
| 54 | + if code == "warning": |
| 55 | + return True, J["warnings"] |
| 56 | + else: |
| 57 | + return True, "" |
| 58 | + else: |
| 59 | + return False, "" |
| 60 | + |
| 61 | + |
| 62 | +def add_talk_to_series(series_id, payload, authorization): |
| 63 | + url = "https://researchseminars.org/api/0/save/talk/" |
| 64 | + r = post(url, json=payload, headers={"authorization":authorization}) |
| 65 | + J = r.json() |
| 66 | + code = J.get("code") |
| 67 | + if r.status_code == 200: |
| 68 | + if code == "warning": |
| 69 | + return J["series_ctr"], J["warnings"] |
| 70 | + else: |
| 71 | + return J["series_ctr"] |
| 72 | + else: |
| 73 | + return "", r.status_code |
| 74 | + |
| 75 | + |
| 76 | +def publish_to_researchseminars(talk): |
| 77 | + # talk should be provided in yaml format |
| 78 | + api_token = os.getenv("RESEARCHSEMINARS_API_TOKEN") |
| 79 | + authorization = "vsf@virtualscienceforum.org %s" % api_token |
| 80 | + |
| 81 | + # Find speakers' corner series, and create it if it doesn't exist |
| 82 | + if not find_seminar_series("speakerscorner"): |
| 83 | + print("[ResearchSeminars.org]: The speakerscorner seminar series "\ |
| 84 | + "does not yet exist. Creating it now") |
| 85 | + create_seminar_series(SPEAKERS_CORNER_SEMINAR_SERIES) |
| 86 | + |
| 87 | + # TODO: Figure out if we need to edit the series; |
| 88 | + # Would be annoying since edits have to be approved |
| 89 | + |
| 90 | + # Set up payload for talk creation |
| 91 | + talk_payload = {"title":talk.get('title'), |
| 92 | + "speaker":talk.get('author'), # TODO: will be 'speakerS' |
| 93 | + "online": True, |
| 94 | + "start_time":talk["time"], |
| 95 | + "timezone":"UTC" |
| 96 | + } |
| 97 | + |
| 98 | + # Make request to remote API |
| 99 | + series_ctr, warnings = add_talk_to_series("speakerscorner", talk_payload, authorization) |
| 100 | + |
| 101 | + if series_ctr != "": |
| 102 | + print("Talk with id {0} successfully added".format(series_ctr)) |
| 103 | + if warnings != "": |
| 104 | + print("Warnings: {0}".format(warnings)) |
| 105 | + return True |
| 106 | + else: |
| 107 | + print("-- ERROR -- ") |
| 108 | + print("Could not add talk to series, status code {0}".format(warnings)) |
| 109 | + return False |
0 commit comments