from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
+from manager.views import get_name
urlpatterns = [
path("admin/", admin.site.urls),
path('accounts/', include('allauth.urls')),
path('', TemplateView.as_view(template_name="manager/index.html")),
+ path('input/', get_name, name="input"),
]
from django.contrib import admin
# Register your models here.
+from .models import Clip
+
+admin.site.register(Clip)
--- /dev/null
+from django import forms
+
+
+class ClipForm(forms.Form):
+ clip = forms.CharField(label="clip", max_length=200)
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 11:53
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = []
+
+ operations = [
+ migrations.CreateModel(
+ name="Clip",
+ fields=[
+ ("id", models.TextField(primary_key=True, serialize=False)),
+ ("url", models.URLField()),
+ ("embed_url", models.URLField()),
+ ("broadcaster_id", models.TextField()),
+ ("broadcaster_name", models.TextField()),
+ ("creator_id", models.TextField()),
+ ("creator_name", models.TextField()),
+ ("video_id", models.TextField()),
+ ("game_id", models.TextField()),
+ ("title", models.TextField()),
+ ("view_count", models.IntegerField()),
+ ("created_at", models.DateTimeField()),
+ ("thumbnail_url", models.TextField()),
+ ("duration", models.FloatField()),
+ ("vod_offset", models.IntegerField()),
+ ],
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 11:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0001_initial"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="clip",
+ name="broadcaster_id",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="broadcaster_name",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="created_at",
+ field=models.DateTimeField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="creator_id",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="creator_name",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="duration",
+ field=models.FloatField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="embed_url",
+ field=models.URLField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="game_id",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="thumbnail_url",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="title",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="url",
+ field=models.URLField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="video_id",
+ field=models.TextField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="view_count",
+ field=models.IntegerField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="vod_offset",
+ field=models.IntegerField(null=True),
+ ),
+ ]
# Create your models here.
class Clip(models.Model):
id = models.TextField(primary_key=True)
- url = models.URLField()
- embed_url = models.URLField()
- broadcaster_id = models.TextField()
- broadcaster_name = models.TextField()
- creator_id = models.TextField()
- creator_name = models.TextField()
- video_id = models.TextField()
- game_id = models.TextField()
- title = models.TextField()
- view_count = models.IntegerField()
- created_at = models.TextField()
- thumbnail_url = models.TextField()
- duration = models.FloatField()
- vod_offset = models.IntegerField()
+ url = models.URLField(null=True)
+ embed_url = models.URLField(null=True)
+ broadcaster_id = models.TextField(null=True)
+ broadcaster_name = models.TextField(null=True)
+ creator_id = models.TextField(null=True)
+ creator_name = models.TextField(null=True)
+ video_id = models.TextField(null=True)
+ game_id = models.TextField(null=True)
+ title = models.TextField(null=True)
+ view_count = models.IntegerField(null=True)
+ created_at = models.DateTimeField(null=True)
+ thumbnail_url = models.TextField(null=True)
+ duration = models.FloatField(null=True)
+ vod_offset = models.IntegerField(null=True)
--- /dev/null
+import requests
+from .models import Clip
+from urllib.parse import urlparse
+from allauth.socialaccount.models import SocialApp, SocialToken
+
+
+def request_clip(url: str):
+ id = urlparse(url).path.split("/")[-1]
+
+ social_app: SocialApp = SocialApp.objects.first()
+ oauth = SocialToken.objects.first().token
+ client_id = social_app.client_id
+
+ r = requests.get(
+ "https://api.twitch.tv/helix/clips",
+ headers={
+ "Authorization": f"Bearer {oauth}",
+ "Client-Id": f"{client_id}",
+ },
+ params={"id": id},
+ )
+
+ if r.status_code != 200:
+ raise Exception
+
+ data = r.json()["data"][0]
+ print(data)
+ Clip(
+ id=data["id"],
+ url=data["url"],
+ embed_url=data["embed_url"],
+ broadcaster_id=data["broadcaster_id"],
+ broadcaster_name=data["broadcaster_name"],
+ creator_id=data["creator_id"],
+ creator_name=data["creator_name"],
+ video_id=data["video_id"],
+ game_id=data["game_id"],
+ title=data["title"],
+ view_count=data["view_count"],
+ created_at=data["created_at"],
+ thumbnail_url=data["thumbnail_url"],
+ duration=data["duration"],
+ vod_offset=data["vod_offset"],
+ ).save()
+
+ return
<header id="heading">
{% if user.is_authenticated %}
<h1>Please Input Clip Link</h1>
- <br />
- <form>
- <input type="text">
+ <form action="{% url 'input' %}" method="post">
+ {% csrf_token %}
+ <input id="clip" type="text" name="clip">
<button>Submit Clip</button>
</form>
{% else %}
+import requests
+
+from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
-# Create your views here.
+from .request_clip import request_clip
+from .forms import ClipForm
+
+
+def get_name(request):
+ # if this is a POST request we need to process the form data
+ if request.method == "POST":
+ # create a form instance and populate it with data from the request:
+ form = ClipForm(request.POST)
+ # check whether it's valid:
+ if form.is_valid():
+ try:
+ request_clip(form.cleaned_data.get("clip"))
+ except Exception as e:
+ print(e)
+ return HttpResponse(b"<h1>NOPE</h1>")
+ # process the data in form.cleaned_data as required
+ # ...
+ # redirect to a new URL:
+ return HttpResponse(b"<h1>THANKS</h1>")
+ # return HttpResponseRedirect("/thanks/")
+
+ # if a GET (or any other method) we'll create a blank form
+ else:
+ form = ClipForm()
+
+ return render(request, "name.html", {"form": form})