pythorhead 0.0.2__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.
pythorhead/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from pythorhead.lemmy import Lemmy
pythorhead/lemmy.py ADDED
@@ -0,0 +1,52 @@
1
+ import requests
2
+ from loguru import logger
3
+
4
+ class Lemmy:
5
+
6
+ _auth_token = None
7
+ _api_base_url = None
8
+ _known_communities = {}
9
+
10
+ def __init__(self, api_base_url):
11
+ self._api_base_url = api_base_url
12
+
13
+
14
+ def log_in(self, username_or_email, password):
15
+ payload = {
16
+ "username_or_email": username_or_email,
17
+ "password": password
18
+ }
19
+ try:
20
+ re = requests.post(f"{self._api_base_url}/api/v3/user/login", json=payload)
21
+ self._auth_token = re.json()["jwt"]
22
+ except Exception as err:
23
+ logger.error(f"Something went wrong while logginf in as {username_or_email}: {err}")
24
+ return False
25
+ return True
26
+
27
+ def discover_community(self, community_name):
28
+ if community_name in self._known_communities:
29
+ return self._known_communities[community_name]
30
+ try:
31
+ req = requests.get(f"{self._api_base_url}/api/v3/community?name={community_name}")
32
+ community_id = req.json()["community_view"]["community"]["id"]
33
+ self._known_communities[community_name] = community_id
34
+ except Exception as err:
35
+ logger.error(f"Error when looking up community '{community_name}': {err}")
36
+ return None
37
+ return community_id
38
+
39
+ def post(self, community_id, post_name, post_url = None, post_body = None):
40
+ new_post = {
41
+ "auth": self._auth_token,
42
+ "community_id": community_id,
43
+ "name": post_name,
44
+ }
45
+ if post_url:
46
+ new_post["url"] = post_url
47
+ if post_body:
48
+ new_post["body"] = post_body
49
+ re = requests.post(f"{self._api_base_url}/api/v3/post", json=new_post)
50
+ if not re.ok:
51
+ logger.error(f"Error encountered while posting: {re.text}")
52
+ return re.ok
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## [v0.0.2](https://github.com/db0/pythorhead/tree/v0.0.2) (2023-06-14)
4
+
5
+ [Full Changelog](https://github.com/db0/pythorhead/compare/0.0.1...v0.0.2)
6
+
7
+ ## [0.0.1](https://github.com/db0/pythorhead/tree/0.0.1) (2023-06-14)
8
+
9
+ [Full Changelog](https://github.com/db0/pythorhead/compare/5279ffc27243872bef2478e08c482d0e19989a41...0.0.1)
10
+
11
+ **Merged pull requests:**
12
+
13
+ - release: pypi [\#1](https://github.com/db0/pythorhead/pull/1) ([db0](https://github.com/db0))
14
+
15
+
16
+
17
+ \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*