mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 22:12:22 +01:00
Initial commit.
This commit is contained in:
40
marietje/marietje/utils.py
Normal file
40
marietje/marietje/utils.py
Normal file
@ -0,0 +1,40 @@
|
||||
import socket, struct, binascii
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def song_to_dict(song, hash=False):
|
||||
data = {
|
||||
'id': song.id,
|
||||
'artist': song.artist,
|
||||
'title': song.title,
|
||||
'duration': song.duration
|
||||
}
|
||||
|
||||
if hash:
|
||||
data['hash'] = song.hash
|
||||
|
||||
if hasattr(song, 'uploader_name') and song.uploader_name is not None and len(song.uploader_name) > 1:
|
||||
data['uploader_name'] = song.uploader_name
|
||||
return data
|
||||
|
||||
|
||||
def playlist_song_to_dict(playlist_song, hash=False):
|
||||
return {
|
||||
'id': playlist_song.id,
|
||||
'requested_by': 'Marietje' if playlist_song.user is None else playlist_song.user.name,
|
||||
'song': song_to_dict(playlist_song.song, hash=hash)
|
||||
}
|
||||
|
||||
|
||||
# Send a file to bertha file storage.
|
||||
def send_to_bertha(file):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect(settings.BERTHA_HOST)
|
||||
sock.sendall(struct.pack("<BQ", 4, file.size))
|
||||
|
||||
for chunk in file.chunks():
|
||||
sock.sendall(chunk)
|
||||
sock.shutdown(socket.SHUT_WR)
|
||||
hash = binascii.hexlify(sock.recv(64))
|
||||
sock.close()
|
||||
return hash
|
||||
Reference in New Issue
Block a user