File size: 1,494 Bytes
836f75b
 
83371d8
836f75b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83371d8
65e40ba
836f75b
65e40ba
836f75b
65e40ba
836f75b
65e40ba
836f75b
 
65e40ba
836f75b
 
 
83371d8
 
 
 
836f75b
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import logging
from urllib.parse import quote

class InstancesAPI:
    def __init__(self, instances):
        self.instances = instances

    def fetch_reports(self):
        reports = {}
        for instance_url in self.instances:
            try:
                response = requests.get(f"{instance_url}/api/get/report")
                response.raise_for_status()
                reports[instance_url] = response.json()
            except requests.exceptions.RequestException as e:
                logging.error(f"Error contacting instance {instance_url}: {e}")
        return reports


    def download_music(self, instance_url, title):
        """
        Download a music file to an instance.

        If the download starts, it returns a JSON like this:
        example: 
            {"music_id": "song_title_2024",
            "status": "Download started"}

        If the music file has already been downloaded, it will return the audio file.
        """
        data = {}
        try:
            # URL-encode the title to handle special characters
            encoded_title = quote(title)
            
            response = requests.get(f"{instance_url}/api/get/music/{encoded_title}")
            response.raise_for_status()
            data = response.json()
            
        except requests.exceptions.RequestException as e:
            logging.error(f"Error contacting instance {instance_url}: {e}")
            data = {"error": str(e)}

        return data