Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created February 21, 2010 06:25
Show Gist options
  • Save andrewle/310167 to your computer and use it in GitHub Desktop.
Save andrewle/310167 to your computer and use it in GitHub Desktop.
#
# Implementation of Justin.TV's Namespace API for creating and playing application
# channels. In it's current form the application will dynamically allocate new
# Justin.TV channels for the sake of connecting two people together in a chat/date
# format.
# @author WinkVid
# @source http://sourceforge.net/projects/winkvid/
#
from django.views.generic.simple import direct_to_template
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.core.urlresolvers import reverse
import jtv
import random
from xml.dom import minidom
from winkvid.viddate.models import *
REQUEST_TOKEN_URL = 'http://api.justin.tv/oauth/request_token'
ACCESS_TOKEN_URL = 'http://api.justin.tv/oauth/access_token'
AUTHORIZATION_URL = 'http://api.justin.tv/oauth/authorize'
# key and secret
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
def home(request):
#If POST we create a new entry in the database, query a new channel key based on the entry_id
if request.method == "POST":
name = request.POST.get('name', '')
sex = request.POST.get('sex', '')
desired_sex = request.POST.get('desired_sex', '')
new_person = Person.objects.create(name=name, sex=sex, desired_sex=desired_sex)
channel_name = "datechannel%s" % new_person.id
client = jtv.jtv_client.JtvClient(CONSUMER_KEY, CONSUMER_SECRET)
response = client.get("/stream/new_stream_key/%s.xml" % channel_name)
key_string = response.read()
print key_string
dom = minidom.parseString(key_string)
stream_key = dom.getElementsByTagName("stream_key")[0].firstChild.nodeValue
new_person.channel = channel_name
new_person.stream_key = stream_key
new_person.save()
#Data saved, take them to the searching for date page
return HttpResponseRedirect(reverse('searching', args=[new_person.id]))
return direct_to_template(request, 'home.html', {})
def view(request):
return direct_to_template(request, 'view.html', {})
def searching(request, uid):
return direct_to_template(request, 'searching.html', {"uid":uid})
def date(request):
#Mark both people as dating
mid = request.GET.get('mid', '').strip()
did = request.GET.get('did', '').strip()
me = Person.objects.get(id=mid)
myDate = Person.objects.get(id=did)
me.in_date_with = myDate
me.status = Person.IN_DATE
myDate.status = Person.IN_DATE
me.save()
myDate.save()
#Return my channel and my stream key and my date's channel
return direct_to_template(request, 'date.html', {
"my_channel": me.channel,
"my_name": me.name,
"stream_key": me.stream_key,
"date_channel": myDate.channel,
"date_name": myDate.name,
})
def check_for_date(request):
mid = request.POST.get('uid', '').strip()
me = Person.objects.get(id=mid)
#Check to see if I'm already in a date with someone and then go to it.
try:
date_check = Person.objects.get(in_date_with=me)
except Person.DoesNotExist:
date_check = False
if date_check:
return HttpResponse("{'result':'success', 'mid': %s, 'did': %s}" % (me.id, date_check.id))
#If I'm not in a date, query for another date and return that ID if found
possible_dates = Person.objects.filter(desired_sex=me.sex, sex=me.desired_sex, status=Person.SEARCHING_FOR_DATE).exclude(id=me.id)
if len(possible_dates)>0:
mydate = possible_dates[0].id
else:
mydate = None
#mydate = random(1,len(possible_dates))
#If found id
if mydate:
return HttpResponse("{'result':'success', 'mid': %s, 'did': %s}" % (mid, mydate))
else:
return HttpResponse("{'result':'nomatch'}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment