summaryrefslogtreecommitdiff
path: root/backend/views.py
diff options
context:
space:
mode:
authorMaksim A. Boyko <maksim.a.boyko@gmail.com>2012-08-25 11:42:40 -0400
committerMaksim A. Boyko <maksim.a.boyko@gmail.com>2012-08-25 11:42:40 -0400
commit6cd2246f44c9769256f722cbb363b7e6acd999d4 (patch)
tree88324407d3ffffdbe9c5afef11b830c592217749 /backend/views.py
parentdedfb9f47b675aac2916e851d1c641b5eba48e29 (diff)
Backend: Add api_room_say view
Diffstat (limited to 'backend/views.py')
-rw-r--r--backend/views.py161
1 files changed, 160 insertions, 1 deletions
diff --git a/backend/views.py b/backend/views.py
index 854e935..5328879 100644
--- a/backend/views.py
+++ b/backend/views.py
@@ -12,8 +12,11 @@ from django.db.models import Q
from datetime import datetime
from datetime import timedelta
+import re
+import os
import sha
import time
+import urllib
import cStringIO
from backend.models import SJLike
@@ -24,6 +27,7 @@ from backend.models import SJUserProfile
from browser import Browser
API_HEADER = '#@scanjam 0.3b\n'
+
ROOM_CHAT_LOG_SIZE = 50
ROOM_VIDEO_LOG_SIZE = 50
ACCESS_USER = 0
@@ -32,10 +36,39 @@ ACCESS_ADMIN = 9
LASTSEEN_TIMEOUT= 7
MAX_BG_SIZE = 400 * 1024
+HTML_TITLE_RE = re.compile('<title>([^<]+)</title>')
+YT_INFO = "http://gdata.youtube.com/feeds/api/videos/"
+YT_INFO_SUFFIX = "?v=2"
+YT_PREFIX = "http://www.youtube.com/watch?v="
+YT_ID_RE = re.compile('http.*v=([^&]+)&?#?')
+VIMEO_PREFIX = "http://vimeo.com/"
+VIMEO_ID_RE = re.compile('^https?://.*vimeo.com/([0-9]+)*')
+SC_PREFIX = "http://api.soundcloud.com/resolve.xml?client_id=iFkfwAx5U1rkd6FImp0Tg&url="
+BANDCAMP_KEY = "PENDING"
+BANDCAMP_API_URL = "http://api.bandcamp.com/api/url/1/info?key="+BANDCAMP_KEY+"&url="
+BANDCAMP_API_BAND = "http://api.bandcamp.com/api/band/3/discography?key="+BANDCAMP_KEY+"&band_id="
+BANDCAMP_API_ALBUM = "http://api.bandcamp.com/api/album/2/info?key="+BANDCAMP_KEY+"&album_id="
+BANDCAMP_API_TRACK = "http://api.bandcamp.com/api/track/1/info?key="+BANDCAMP_KEY+"&track_id="
+DEFAULT_BG = "http://scannerjammer.com/bgz/gridzy9.jpg"
+
#
# Common funtions
#
+def is_image(url):
+ path, ext = os.path.splitext(url)
+ return ext.lower() in ['.gif', '.jpg', '.jpeg', '.png']
+
+def is_number(s):
+ try:
+ int(s)
+ return True
+ except:
+ return False
+
+def title_from_url (url):
+ return url.split("/")[-1].replace(".mp3", "").replace("%20"," ").replace("_"," ")
+
def headers(response):
""" Setup additional headers for response
"""
@@ -142,6 +175,126 @@ def likes_response_str(likes):
strio.write('LIKE\t%s\n' % like.user.username)
return strio.getvalue()
+def store_video_youtube(room, user, ytid):
+ """
+ """
+ ytinfourl = YT_INFO+ytid+YT_INFO_SUFFIX
+ ytinfo = urllib.urlopen(ytinfourl).read()
+ ytmatch = HTML_TITLE_RE.search(ytinfo)
+ title = '___'
+ if ytmatch:
+ title = ytmatch.group(1)
+ video = SJContent(room=room, content_type='video', user=user, datetime=datetime.now())
+ video.settings = {'url': YT_PREFIX+ytid, 'title': title}
+ video.save()
+ return video.id
+
+def store_video_vimeo(room, user, vimeoid):
+ """
+ """
+ vimeoinfourl = VIMEO_PREFIX+vimeoid
+ vimeoinfo = urllib.urlopen(vimeoinfourl).read()
+ match = HTML_TITLE_RE.search(vimeoinfo)
+ title = '___'
+ if match:
+ title = match.group(1)
+ title = title[0:-9]
+ video = SJContent(room=room, content_type='video', user=user, datetime=datetime.now())
+ video.settings = {'url': VIMEO_PREFIX+vimeoid, 'title': title}
+ video.save()
+ return video.id
+
+def store_video_soundcloud(room, user, url):
+ """
+ """
+ scinfourl = SC_PREFIX+url
+ scinfo = urllib.urlopen(scinfourl).read()
+ title = "___"
+ if "<title>" in scinfo:
+ match = HTML_TITLE_RE.search(scinfo)
+ title = '___'
+ if match:
+ title = match.group(1)
+ elif "<user>" in scinfo:
+ scinfourl = SC_PREFIX+url+"/tracks"
+ scinfo = urllib.urlopen(scinfourl).read()
+ match = HTML_TITLE_RE.search(scinfo)
+ title = '___'
+ if match:
+ title = match.group(1)
+ video = SJContent(room=room, content_type='video', user=user, datetime=datetime.now())
+ video.settings = {'url': url, 'title': title}
+ video.save()
+ return video.id
+
+def store_media_str(room, user, msg):
+ """
+ """
+ stri= cStringIO.StringIO()
+ user_profile = user.get_profile()
+ words = msg.split()
+ music = None
+ image = None
+ for word in words:
+ if 'youtube.com' in word:
+ ytid = ''
+ match = YT_ID_RE.match(word)
+ if match:
+ ytid = match.group(1)
+ elif 'user' in word:
+ ytid = word[-11:]
+ elif 'embed' in word:
+ embedpos = word.find("embed")
+ ytid = word[embedpos+6:embedpos+17]
+ elif '/v/' in word:
+ embedpos = word.find("/v/")
+ ytid = word[embedpos+3:embedpos+14]
+ elif '/feeds/api/videos/' in word:
+ embedpos = word.find("/feeds/api/videos/")
+ ytid = word[embedpos+18:embedpos+29]
+ else:
+ continue
+ videos = SJContent.objects.filter(id=store_video_youtube(room, user, ytid))
+ strio.write(videos_response_str(videos))
+ elif "http://youtu.be/" in word:
+ try:
+ ytid = word[16:27]
+ videos = SJContent.objects.filter(id=store_video_youtube(room, user, ytid))
+ strio.write(videos_response_str(videos))
+ except:
+ pass
+ elif "vimeo.com" in word:
+ try:
+ match = VIMEO_ID_RE.match(word)
+ if not match:
+ continue
+ vimeoid = match.group(1)
+ videos = SJContent.objects.filter(id=store_video_vimeo(room, user, vimeoid))
+ strio.write(videos_response_str(videos))
+ except:
+ pass
+ elif "soundcloud.com" in word:
+ try:
+ videos = SJContent.objects.filter(id=store_video_soundcloud(room, user, word))
+ strio.write(videos_response_str(videos))
+ except:
+ pass
+ elif word[-3:] == "mp3":
+ music = word
+ elif word.startswith("http"):
+ url = SJContent(room=room, content_type='url', user=user, datetime=datetime.now())
+ url.settings = {'url': word}
+ url.save()
+ if is_image(word):
+ image = word
+ if image is not None and music is not None:
+ title = title_from_url(music)
+ audio = SJContent(room=room, content_type='video', user=user, datetime=datetime.now())
+ audio.settings = {'url': image+music, 'title': title}
+ audio.save()
+ strio.write(videos_response_str([audio]))
+ return strio.getvalue()
+
def thraw(settings_text):
settings = dict()
if settings_text:
@@ -631,7 +784,13 @@ def api_room_say(request):
room = SJRoom.objects.get(name=request.POST['room'])
except:
return response_error('NO ROOM')
- return HttpResponse('Not implemented yet!\n')
+ text = SJContent(room=room, content_type='text', user=user, datetime=datetime.now())
+ text.settings = {'text': request.POST['msg']}
+ text.save()
+ response = response_success('%d\t%s\t%s\t%s' % (text.id, str(datetime_to_timestamp(text.datetime)), user.username, request.POST['msg']))
+ if 'http' in request.POST['msg']:
+ response.write(store_media_str(room, user, request.POST['msg']))
+ return response
@require_POST
def api_room_video(request):