mubashiralli commited on
Commit
75e8808
1 Parent(s): 4526437

updating uploading to wordpress function

Browse files
Files changed (1) hide show
  1. app.py +66 -12
app.py CHANGED
@@ -10,38 +10,92 @@ import soundfile as sf
10
  from scipy.signal import butter, sosfilt
11
 
12
  from wordpress_xmlrpc.compat import xmlrpc_client
13
- from xmlrpc.client import ServerProxy
14
  from io import BytesIO
15
  import soundfile as sf
16
  from datetime import datetime
17
 
 
 
 
 
 
 
 
 
 
 
18
  def save_to_wordpress(audio, sample_rate):
19
  # Define your WordPress site URL and authentication credentials
20
  wordpress_url = 'https://songlabai.com/xmlrpc.php'
 
 
 
21
  username = 'admin_h2ibbgql'
22
  password = 'um^VdaNK0H8Vw2*KNJlYABkh'
23
 
24
  # Authenticate with WordPress XML-RPC API
25
- proxy = ServerProxy(wordpress_url)
26
  wav_bytes = BytesIO()
27
  sf.write(wav_bytes, audio, samplerate=sample_rate, format='WAV')
28
-
29
  file_data = {
30
- 'name': f"generated_audio_{datetime.now().timestamp()}.wav",
31
  'type': 'audio/x-wav', # Change the MIME type according to your file type
32
  'bits': xmlrpc_client.Binary(wav_bytes.getvalue()),
33
  }
34
-
35
  for _ in range(4):
36
  try:
37
  # Upload the file to WordPress Media Library
38
- response_upload = proxy.wp.uploadFile(0, username, password, file_data)
 
39
 
40
  # Handle the response
41
- if response_upload['id']:
42
- print(response_upload)
43
- attachment_id = response_upload['id']
44
- print("File successfully uploaded to WordPress with attachment ID:", attachment_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
  print("Error uploading file to WordPress.")
47
  break
@@ -114,9 +168,9 @@ if st.button("Generate Audio") and genre and energy_level and description and te
114
  audio = np.array(response['generated_audio'], dtype=np.float32)
115
  sample_rate = response['sample_rate']
116
  st_state.audio = audio
117
- save_to_wordpress(audio, sample_rate)
118
  st.audio(st_state.audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
119
-
120
  # Post-processing options
121
  st.header("Post-processing Options")
122
 
 
10
  from scipy.signal import butter, sosfilt
11
 
12
  from wordpress_xmlrpc.compat import xmlrpc_client
 
13
  from io import BytesIO
14
  import soundfile as sf
15
  from datetime import datetime
16
 
17
+ from wordpress_xmlrpc import Client
18
+ from wordpress_xmlrpc.methods import media
19
+ from xmlrpc.client import Binary
20
+ from woocommerce import API
21
+ from wordpress_xmlrpc.compat import xmlrpc_client
22
+ from io import BytesIO
23
+ import soundfile as sf # This library is used for saving audio data as WAV file
24
+ from datetime import datetime
25
+ import uuid
26
+
27
  def save_to_wordpress(audio, sample_rate):
28
  # Define your WordPress site URL and authentication credentials
29
  wordpress_url = 'https://songlabai.com/xmlrpc.php'
30
+ woocommerce_url = 'https://songlabai.com'
31
+ consumer_key = 'ck_93d516ba12289a6fd0eced56bbc0b05ecbf98735'
32
+ consumer_secret = 'cs_9d5eb716d631db408a4c47796b5d18b0313d8559'
33
  username = 'admin_h2ibbgql'
34
  password = 'um^VdaNK0H8Vw2*KNJlYABkh'
35
 
36
  # Authenticate with WordPress XML-RPC API
 
37
  wav_bytes = BytesIO()
38
  sf.write(wav_bytes, audio, samplerate=sample_rate, format='WAV')
39
+ title = f"generated_audio_{datetime.now().timestamp()}.wav"
40
  file_data = {
41
+ 'name': title,
42
  'type': 'audio/x-wav', # Change the MIME type according to your file type
43
  'bits': xmlrpc_client.Binary(wav_bytes.getvalue()),
44
  }
45
+ wp_client = Client(wordpress_url, username, password)
46
  for _ in range(4):
47
  try:
48
  # Upload the file to WordPress Media Library
49
+
50
+ media_response = wp_client.call(media.UploadFile(file_data))
51
 
52
  # Handle the response
53
+ if media_response:
54
+ print("File successfully uploaded to WordPress with attachment ID:", media_response)
55
+
56
+ # Create product data for WooCommerce
57
+ product_data = {
58
+ 'status':'pending',
59
+ 'name': title,
60
+ 'type': 'simple',
61
+ 'regular_price': '1.00', # Set the price as needed
62
+ 'sku': str(uuid.uuid4()),
63
+ 'downloadable': True,
64
+ 'download_limit': -1,
65
+ 'download_expiry': -1,
66
+ }
67
+
68
+ # Authenticate with WooCommerce API
69
+ wc_api = API(
70
+ url=woocommerce_url,
71
+ consumer_key=consumer_key,
72
+ consumer_secret=consumer_secret,
73
+ version="wc/v3"
74
+ )
75
+
76
+ # Create the product
77
+ response = wc_api.post("products", product_data)
78
+
79
+ # Handle the response
80
+ if response.status_code == 201:
81
+ print("Product successfully created in WooCommerce:", response.json())
82
+ # Update product to add downloadable file URL
83
+ product_update_data = {
84
+ 'downloads': [{
85
+ 'name': media_response['title'],
86
+ 'file': media_response['link']
87
+ }]
88
+ }
89
+ product_id = response.json().get('id')
90
+ response = wc_api.put(f"products/{product_id}", product_update_data)
91
+
92
+ if response.status_code == 200:
93
+ print("Downloadable file URL added to product:", response.json())
94
+ return response.json()['permalink']
95
+ else:
96
+ print("Error adding downloadable file URL to product:", response.text)
97
+ else:
98
+ print("Error creating product in WooCommerce:", response.text)
99
  else:
100
  print("Error uploading file to WordPress.")
101
  break
 
168
  audio = np.array(response['generated_audio'], dtype=np.float32)
169
  sample_rate = response['sample_rate']
170
  st_state.audio = audio
171
+ permalink = save_to_wordpress(audio, sample_rate)
172
  st.audio(st_state.audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
173
+ st.text(f"For downloading/publishing please contact the administrator, from your loged in accout. Remember to send the following link:\n{permalink}")
174
  # Post-processing options
175
  st.header("Post-processing Options")
176