How to handle json.decoder.JSONDecodeError and requests.exceptions.JSONDecodeError ?
  • "Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearCO
    CoderSupreme
    1y ago 80%
    def get_admin_ids(domain: str) -> List[str]:
        url = f"https://{domain}/api/v3/site"
        response = requests.get(url)
        try:
            data = response.json()
        except json.JSONDecodeError:
            print(f"Error: Invalid or empty JSON response for domain {domain}")
            return []
    
        admin_ids = [
            item["person"]["id"] for item in data["admins"] if item["person"].get("admin")
        ]
        return admin_ids
    
    3
  • There was a programming instance called heapoverflow.ml that already had syntax highlighting, but unfortunately, it closed down and the feature wasn't merged with the upstream lemmy project. To enhance the user experience on Programming.dev, it would be beneficial to have syntax highlighting implemented. One possible solution is to reach out to the admin [@Thann@lemmy.ml](https://lemmy.ml/u/Thann) and inquire about obtaining the code from heapoverflow.ml to incorporate syntax highlighting into this instance as well. Syntax highlighting plays a crucial role in improving code readability and comprehension, making it easier to identify different program elements such as keywords, comments, and strings. By enhancing the syntax highlighting feature, https://programming.dev can provide a more user-friendly environment for developers to share and discuss code.

    27
    1

    `message_admins.py` ```python import csv import requests from typing import Any, Dict, List from pythorhead import Lemmy from config import * def get_admin_ids(domain: str) -> List[str]: url = f"https://{domain}/api/v3/site" response = requests.get(url) data = response.json() admin_ids = [ item["person"]["id"] for item in data["admins"] if item["person"].get("admin") ] return admin_ids def send_private_message(lemmy: Lemmy, user_id: str, content: str) -> bool: pm = lemmy.private_message(content, user_id) if not pm: print("Sending private message failed") return False return True def get_domains_from_csv(filename: str) -> List[str]: domains = [] with open(filename, "r") as csvfile: reader = csv.DictReader(csvfile) for row in reader: domains.append(row["Domain"]) return domains def main() -> None: domains = get_domains_from_csv("output.csv") lemmy = Lemmy(LEMMY_INSTANCE_URL) lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD) for domain in domains: admin_ids = get_admin_ids(domain) content = f''' Dear admins, I hope this message finds you well. I wanted to bring to your attention a recent change in stance regarding bots on the lemm.ee instance. As stated in the post lemm.ee/post/1847525, lemm.ee has decided to introduce new rules to limit repost bots. I am part of the !youtube_feed@lemm.ee reposting community, and manually posting everything has become quite burdensome. Therefore, I am kindly requesting if it would be possible to move our community to the {domain} instance. Thank you for your consideration. Best regards, {LEMMY_USERNAME} ''' for admin_id in admin_ids: pm = lemmy.private_message(content, admin_id) if not pm: print("Sending private message failed") else: print(f"Sent private message to {admin_id}") if __name__ == "__main__": main() ``` Traceback ``` Traceback (most recent call last): File "/home/user/code/python/lemmy/venv/lib/python3.11/site-packages/requests/models.py", line 971, in json return complexjson.loads(self.text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/json/__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/user/code/python/lemmy/spam_admins.py", line 60, in main() File "/home/user/code/python/lemmy/spam_admins.py", line 42, in main admin_ids = get_admin_ids(domain) ^^^^^^^^^^^^^^^^^^^^^ File "/home/user/code/python/lemmy/spam_admins.py", line 13, in get_admin_ids data = response.json() ^^^^^^^^^^^^^^^ File "/home/user/code/python/lemmy/venv/lib/python3.11/site-packages/requests/models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ``` `output.csv` ``` Domain,Latency lemmy.ml,459.306001663208 lemm.ee,467.4856662750244 midwest.social,514.9338245391846 lemmy.one,550.7447719573975 slrpnk.net,586.4944458007812 ```

    9
    5
    Which AI have you replaced StackOverflow with?
  • "Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearCO
    CoderSupreme
    1y ago 100%

    I use Perplexity.ai, it uses ChatGPT + search and improves accuracy a lot. It only has 5 free uses of GPT-4 every 4 hours, but the normal ChatGPT + search is still better than any of the other LLMs I've tried.

    9
  • After seeing [this graph about the fall of StackOverflow](https://programming.dev/post/931727), it's clear to me that I'm not the only one who has stopped using it in favor of LLMS (large language model) alternatives. With the rise of generative AI-powered initiatives like OverflowAI by Stack Overflow, and the potential of ChatGPT by OpenAI, the landscape of programming knowledge sharing is evolving. These AI models have the ability to generate human-like text and provide answers to programming questions. It's fascinating to witness the potential of AI as a game-changing tool in programming problem solving. So, I'm curious, which AI have you replaced StackOverflow with? Share your favorite LLMS or generative AI platform for programming in the comments below!

    21
    21
    The RemindMe Bot! A lemmy bot that reminds you after an amount of time
  • "Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearCO
    CoderSupreme
    1y ago 100%

    Couldn't this be made private too? That way there wouldn't be so much bot spam. Also I would like to be able to upvote a public remindme comment to be reminded too in private.

    1
  • git.disroot.org

    I've been working on some Python scripts for Lemmy that you might find useful. Here's what's included: - Reposting invidious feed - Purging your user posts and comments - Reposting GitHub issues and comments - Checking which instances block a given instance - Pinging instances multiple times and sorting them by latency - Awesome Lemmy Instances has an added column that shows total blocked users for each instance. Thanks to [@db0@lemmy.dbzer0.com](https://lemmy.dbzer0.com/u/db0) for making [Pythörhead](https://github.com/db0/pythorhead). You can find other useful Lemmy tools in [Awesome Lemmy](https://github.com/dbeley/awesome-lemmy/blob/master/README.md#awesome-lemmy-)

    17
    2
    [Solved] How to Retrieve All Posts for a User or Community on Lemmy Using Pythörhead Library?
  • "Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearCO
    CoderSupreme
    1y ago 75%
    import json
    import sqlite3
    
    from config import *
    from pythorhead import Lemmy
    
    # Connect to the database
    conn = sqlite3.connect('lemmy_github.db')
    cursor = conn.cursor()
    
    def import_missing_posts(posts_list):
        for post in posts_list:
            post = post['post']
            try:
                cursor.execute('SELECT * FROM posts WHERE issue_number = ?', (issue_number(post['url']),))
                result = cursor.fetchone()
                if result is None:
                    cursor.execute('INSERT INTO posts (issue_number, lemmy_post_id, issue_title, issue_body) VALUES (?, ?, ?, ?)',
                                (issue_number(post['url']), post['id'], post['name'], post['body']))
                    conn.commit()
            except sqlite3.Error as e:
                print(f"SQLite error occurred: {e}")
            except KeyError as e:
                print(f"KeyError occurred: {e}. Check if the input dictionary has all the required keys.")
            except Exception as e:
                print(f"An error occurred: {e}")
    
    def issue_number(url) -> int:
        return int(url.split("/")[-1])
    
    def load_json(filename):
        with open(filename) as f:
            return json.load(f)
    
    
    def process_posts(lemmy, username):
        page = 1
        while True:
            posts = lemmy.user.get(username=username, page=page)['posts']
            if not posts:
                break
            import_missing_posts(posts)
            page += 1
    
    lemmy = Lemmy(LEMMY_INSTANCE_URL)
    lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD)
    process_posts(lemmy, LEMMY_USERNAME)
    
    # Close the connection
    conn.close()
    
    2
  • I have encountered a situation where I need to retrieve all posts for a user or community on Lemmy and store them in a database. Due to a recent mishap, I had to revert to an older version of the database, resulting in a loss of some posts. To overcome this issue, I am seeking guidance on how to efficiently retrieve all posts for a user or community on Lemmy and extract important information such as the URL, Title, Body, and Posted Timestamp. To accomplish this task, I am utilizing the [Pythörhead](https://github.com/db0/pythorhead) library, which provides a Python interface for interacting with the Lemmy API. One approach I have considered is searching for each URL before posting to avoid duplicating posts. However, this method would be very slow and would create a significant load on the instance, potentially impacting performance. Any suggestions on how best to solve this issue would be invaluable. For reference, [this is the Lemmy API](https://join-lemmy.org/api) endpoint that can be utilized for retrieving posts is. Thank you in advance for your assistance!

    6
    1

    With recent advancements in AI technologies such as Code Interpreter and LongNet, it seems that we are getting closer to a point where AI can write any software, including an operating system. Surprisingly, predictions from platforms like Metaculus suggest that this could become a reality as early as 2029 [^1]. The combination of GPT-5, access to factual information through search, the ability to understand and keep track of vast amounts of code with LongNet, and the ability to debug and refine the code until it achieves the desired outcome with Code Interpreter, could enable AI to achieve tasks previously thought unimaginable by a single entity. It would be interesting to explore alternative methods of assessing this progress, such as analyzing the exponential growth of GitHub issues opened versus solved over time, to determine if there is an expected convergence in the near future. [^1]: [Metaculus: AI Programming - 50k Lines of Code](https://www.metaculus.com/questions/16173/ai-programming-50k-lines-of-code)

    14
    10