diff --git a/pythorhead/comment.py b/pythorhead/comment.py index efbcf8b..0dce540 100644 --- a/pythorhead/comment.py +++ b/pythorhead/comment.py @@ -1,7 +1,7 @@ -from typing import Any, List, Literal, Optional +from typing import Any, List, Literal, Optional, Union from pythorhead.requestor import Request, Requestor -from pythorhead.types import CommentSortType, ListingType +from pythorhead.types import CommentSortType, ListingType, LanguageType class Comment: @@ -72,7 +72,7 @@ def create( content: str, form_id: Optional[str] = None, parent_id: Optional[int] = None, - language_id: Optional[int] = None, + language_id: Union[int, LanguageType, None] = None, ) -> Optional[dict]: """ Create a comment. @@ -82,7 +82,7 @@ def create( content (str) form_id (Optional[int], optional): Defaults to None. parent_id (Optional[int], optional): Defaults to None. - language_id (Optional[int], optional): Defaults to None. + language_id (Union[int, LanguageType], optional): Defaults to None. Returns: Optional[dict]: created comment data if successful @@ -97,7 +97,10 @@ def create( if parent_id is not None: create_comment["parent_id"] = parent_id if language_id is not None: - create_comment["language_id"] = language_id + if isinstance(language_id, LanguageType): + create_comment["language_id"] = language_id.value + else: + create_comment["language_id"] = language_id return self._requestor.api( Request.POST, @@ -111,7 +114,7 @@ def edit( content: Optional[str] = None, distinguished: Optional[bool] = None, form_id: Optional[str] = None, - language_id: Optional[int] = None, + language_id: Union[int, LanguageType, None] = None, ) -> Optional[dict]: """ Edit a comment. @@ -121,7 +124,7 @@ def edit( content (Optional[str], optional): Defaults to None. distinguished (Optional[bool], optional): Defaults to None. form_id (Optional[str], optional): Defaults to None. - language_id (Optional[int], optional): Defaults to None. + language_id (Union[int, LanguageType], optional): Defaults to None. Returns: Optional[dict]: edited comment data if successful @@ -136,7 +139,10 @@ def edit( if form_id is not None: edit_comment["form_id"] = form_id if language_id is not None: - edit_comment["language_id"] = language_id + if isinstance(language_id, LanguageType): + edit_comment["language_id"] = language_id.value + else: + edit_comment["language_id"] = language_id return self._requestor.api( Request.PUT, diff --git a/pythorhead/community.py b/pythorhead/community.py index fb2c64f..63016a0 100644 --- a/pythorhead/community.py +++ b/pythorhead/community.py @@ -1,7 +1,8 @@ -from typing import Any, List, Optional +from typing import List, Optional, Union + from pythorhead.requestor import Request, Requestor -from pythorhead.types import ListingType, SortType +from pythorhead.types import ListingType, SortType, LanguageType class Community: @@ -16,6 +17,7 @@ def create( icon: Optional[str] = None, nsfw: Optional[bool] = None, posting_restricted_to_mods: Optional[bool] = None, + discussion_languages: Optional[List[Union[int, LanguageType]]] = None, ) -> Optional[dict]: """ Create a community @@ -27,6 +29,7 @@ def create( icon (str, optional): Defaults to None nsfw (bool, optional): Defaults to None posting_restricted_to_mods (bool, optional): Defaults to None + discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None Returns: Optional[dict]: post data if successful @@ -43,6 +46,9 @@ def create( new_community["nsfw"] = nsfw if [posting_restricted_to_mods] is not None: new_community["[posting_restricted_to_mods]"] = [posting_restricted_to_mods] + if discussion_languages is not None: + new_community["discussion_languages"] = [l.value for l in discussion_languages + if isinstance(l, LanguageType)] return self._requestor.api(Request.POST, "/community", json=new_community) diff --git a/pythorhead/post.py b/pythorhead/post.py index 52ecfbb..d507ebb 100644 --- a/pythorhead/post.py +++ b/pythorhead/post.py @@ -1,7 +1,7 @@ -from typing import Any, List, Literal, Optional +from typing import Any, List, Literal, Optional, Union from pythorhead.requestor import Request, Requestor -from pythorhead.types import FeatureType, ListingType, SortType +from pythorhead.types import FeatureType, ListingType, SortType, LanguageType class Post: @@ -86,7 +86,7 @@ def create( body: Optional[str] = None, nsfw: Optional[bool] = None, honeypot: Optional[str] = None, - language_id: Optional[int] = None, + language_id: Union[int, LanguageType, None] = None, ) -> Optional[dict]: """ Create a post @@ -98,7 +98,7 @@ def create( body (str, optional): Defaults to None. nsfw (bool, optional): Defaults to None. honeypot (str, optional): Defaults to None. - language_id (int, optional): Defaults to None. + language_id (Union[int, LanguageType], optional): Defaults to None. Returns: Optional[dict]: post data if successful @@ -117,7 +117,10 @@ def create( if honeypot is not None: new_post["honeypot"] = honeypot if language_id is not None: - new_post["language_id"] = language_id + if isinstance(language_id, LanguageType): + new_post["language_id"] = language_id.value + else: + new_post["language_id"] = language_id return self._requestor.api(Request.POST, "/post", json=new_post) @@ -167,7 +170,7 @@ def edit( url: Optional[str] = None, body: Optional[str] = None, nsfw: Optional[bool] = None, - language_id: Optional[int] = None, + language_id: Union[int, LanguageType, None] = None, ) -> Optional[dict]: """ @@ -179,7 +182,7 @@ def edit( url (str, optional): Defaults to None. body (str, optional): Defaults to None. nsfw (bool, optional): Defaults to None. - language_id (int, optional): Defaults to None. + language_id (Union[int, LanguageType], optional): Defaults to None. Returns: Optional[dict]: post data if successful @@ -196,7 +199,10 @@ def edit( if nsfw is not None: edit_post["nsfw"] = nsfw if language_id is not None: - edit_post["language_id"] = language_id + if isinstance(language_id, LanguageType): + edit_post["language_id"] = language_id.value + else: + edit_post["language_id"] = language_id return self._requestor.api(Request.PUT, "/post", json=edit_post) diff --git a/pythorhead/site.py b/pythorhead/site.py index d48266b..4cb9fd5 100644 --- a/pythorhead/site.py +++ b/pythorhead/site.py @@ -1,7 +1,7 @@ -from typing import Any, List, Optional +from typing import Any, List, Optional, Union from pythorhead.requestor import Request, Requestor -from pythorhead.types import ListingType +from pythorhead.types import ListingType, LanguageType class Site: @@ -37,7 +37,7 @@ def edit( legal_information: Optional[str] = None, application_email_admins: Optional[bool] = None, hide_modlog_mod_names: Optional[bool] = None, - discussion_languages=None, + discussion_languages: Optional[List[Union[int, LanguageType]]] = None, slur_filter_regex: Optional[str] = None, actor_name_max_length: Optional[int] = None, rate_limit_message: Optional[int] = None, @@ -84,7 +84,7 @@ def edit( legal_information (str, optional): Defaults to None. application_email_admins (bool, optional): Defaults to None. hide_modlog_mod_names (bool, optional): Defaults to None. - discussion_languages (LanguageType, optional): Defaults to None. + discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None slur_filter_regex (str, optional): Defaults to None. actor_name_max_length (int, optional): Defaults to None. rate_limit_message (int, optional): Defaults to None. @@ -112,6 +112,7 @@ def edit( Returns: Optional[dict]: post data if successful """ + discussion_languages = [l.value for l in discussion_languages if isinstance(l, LanguageType)] edit_site: dict[str, Any] = { key: value for key, value in locals().items() if value is not None and key != "self" } diff --git a/pythorhead/types/__init__.py b/pythorhead/types/__init__.py index 7a6982e..b1e18ca 100644 --- a/pythorhead/types/__init__.py +++ b/pythorhead/types/__init__.py @@ -1,3 +1,4 @@ from .feature import FeatureType from .listing import ListingType from .sort import CommentSortType, SortType +from .language import LanguageType diff --git a/pythorhead/types/language.py b/pythorhead/types/language.py new file mode 100644 index 0000000..b2413e9 --- /dev/null +++ b/pythorhead/types/language.py @@ -0,0 +1,189 @@ +from enum import Enum + +# from https://github.com/LemmyNet/lemmy/blob/c216153dfb3cb2d08b72fa466fe522b3c7accde7/migrations/2022-06-21-123144_language-tags/up.sql + +class LanguageType(Enum): + UNDETERMINED = 0 + AA = 1 # Afaraf + AB = 2 # аҧсуа бызшәа + AE = 3 # avesta + AF = 4 # Afrikaans + AK = 5 # Akan + AM = 6 # አማርኛ + AN = 7 # aragonés + AR = 8 # اَلْعَرَبِيَّةُ + AS = 9 # অসমীয়া + AV = 10 # авар мацӀ + AY = 11 # aymar aru + AZ = 12 # azərbaycan dili + BA = 13 # башҡорт теле + BE = 14 # беларуская мова + BG = 15 # български език + BI = 16 # Bislama + BM = 17 # bamanankan + BN = 18 # বাংলা + BO = 19 # བོད་ཡིག + BR = 20 # brezhoneg + BS = 21 # bosanski jezik + CA = 22 # Català + CE = 23 # нохчийн мотт + CH = 24 # Chamoru + CO = 25 # corsu + CR = 26 # ᓀᐦᐃᔭᐍᐏᐣ + CS = 27 # čeština + CU = 28 # ѩзыкъ словѣньскъ + CV = 29 # чӑваш чӗлхи + CY = 30 # Cymraeg + DA = 31 # dansk + DE = 32 # Deutsch + DV = 33 # ދިވެހި + DZ = 34 # རྫོང་ཁ + EE = 35 # Eʋegbe + EL = 36 # Ελληνικά + EN = 37 # English + EO = 38 # Esperanto + ES = 39 # Español + ET = 40 # eesti + EU = 41 # euskara + FA = 42 # فارسی + FF = 43 # Fulfulde + FI = 44 # suomi + FJ = 45 # vosa Vakaviti + FO = 46 # føroyskt + FR = 47 # Français + FY = 48 # Frysk + GA = 49 # Gaeilge + GD = 50 # Gàidhlig + GL = 51 # galego + GN = 52 # Avañe\'ẽ + GU = 53 # ગુજરાતી + GV = 54 # Gaelg + HA = 55 # هَوُسَ + HE = 56 # עברית + HI = 57 # हिन्दी + HO = 58 # Hiri Motu + HR = 59 # Hrvatski + HT = 60 # Kreyòl ayisyen + HU = 61 # magyar + HY = 62 # Հայերեն + HZ = 63 # Otjiherero + IA = 64 # Interlingua + ID = 65 # Bahasa Indonesia + IE = 66 # Interlingue + IG = 67 # Asụsụ Igbo + II = 68 # ꆈꌠ꒿ Nuosuhxop + IK = 69 # Iñupiaq + IO = 70 # Ido + IS = 71 # Íslenska + IT = 72 # Italiano + IU = 73 # ᐃᓄᒃᑎᑐᑦ + JA = 74 # 日本語 + JV = 75 # basa Jawa + KA = 76 # ქართული + KG = 77 # Kikongo + KI = 78 # Gĩkũyũ + KJ = 79 # Kuanyama + KK = 80 # қазақ тілі + KL = 81 # kalaallisut + KM = 82 # ខេមរភាសា + KN = 83 # ಕನ್ನಡ + KO = 84 # 한국어 + KR = 85 # Kanuri + KS = 86 # कश्मीरी + KU = 87 # Kurdî + KV = 88 # коми кыв + KW = 89 # Kernewek + KY = 90 # Кыргызча + LA = 91 # latine + LB = 92 # Lëtzebuergesch + LG = 93 # Luganda + LI = 94 # Limburgs + LN = 95 # Lingála + LO = 96 # ພາສາລາວ + LT = 97 # lietuvių kalba + LU = 98 # Kiluba + LV = 99 # latviešu valoda + MG = 100 # fiteny malagasy + MH = 101 # Kajin M̧ajeļ + MI = 102 # te reo Māori + MK = 103 # македонски јазик + ML = 104 # മലയാളം + MN = 105 # Монгол хэл + MR = 106 # मराठी + MS = 107 # Bahasa Melayu + MT = 108 # Malti + MY = 109 # ဗမာစာ + NA = 110 # Dorerin Naoero + NB = 111 # Norsk bokmål + ND = 112 # isiNdebele + NE = 113 # नेपाली + NG = 114 # Owambo + NL = 115 # Nederlands + NN = 116 # Norsk nynorsk + NO = 117 # Norsk + NR = 118 # isiNdebele + NV = 119 # Diné bizaad + NY = 120 # chiCheŵa + OC = 121 # occitan + OJ = 122 # ᐊᓂᔑᓈᐯᒧᐎᓐ + OM = 123 # Afaan Oromoo + OR = 124 # ଓଡ଼ିଆ + OS = 125 # ирон æвзаг + PA = 126 # ਪੰਜਾਬੀ + PI = 127 # पाऴि + PL = 128 # Polski + PS = 129 # پښتو + PT = 130 # Português + QU = 131 # Runa Simi + RM = 132 # rumantsch grischun + RN = 133 # Ikirundi + RO = 134 # Română + RU = 135 # Русский + RW = 136 # Ikinyarwanda + SA = 137 # संस्कृतम् + SC = 138 # sardu + SD = 139 # सिन्धी + SE = 140 # Davvisámegiella + SG = 141 # yângâ tî sängö + SI = 142 # සිංහල + SK = 143 # slovenčina + SL = 144 # slovenščina + SM = 145 # gagana fa\'a Samoa + SN = 146 # chiShona + SO = 147 # Soomaaliga + SQ = 148 # Shqip + SR = 149 # српски језик + SS = 150 # SiSwati + ST = 151 # Sesotho + SU = 152 # Basa Sunda + SV = 153 # Svenska + SW = 154 # Kiswahili + TA = 155 # தமிழ் + TE = 156 # తెలుగు + TG = 157 # тоҷикӣ + TH = 158 # ไทย + TI = 159 # ትግርኛ + TK = 160 # Türkmençe + TL = 161 # Wikang Tagalog + TN = 162 # Setswana + TO = 163 # faka Tonga + TR = 164 # Türkçe + TS = 165 # Xitsonga + TT = 166 # татар теле + TW = 167 # Twi + TY = 168 # Reo Tahiti + UG = 169 # ئۇيغۇرچە‎ + UK = 170 # Українська + UR = 171 # اردو + UZ = 172 # Ўзбек + VE = 173 # Tshivenḓa + VI = 174 # Tiếng Việt + VO = 175 # Volapük + WA = 176 # walon + WO = 177 # Wollof + XH = 178 # isiXhosa + YI = 179 # ייִדיש + YO = 180 # Yorùbá + ZA = 181 # Saɯ cueŋƅ + ZH = 182 # 中文 + ZU = 183 # isiZulu