from django.contrib import admin
# Register your models here.
-from .models import Clip
+from .models import Clip, ResetData
admin.site.register(Clip)
+admin.site.register(ResetData)
--- /dev/null
+class TooManyClips(Exception):
+ pass
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 13:10
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0003_alter_clip_account"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="clip",
+ name="id",
+ field=models.CharField(
+ max_length=100, primary_key=True, serialize=False, unique=True
+ ),
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 13:30
+
+from django.conf import settings
+import django.contrib.auth.models
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("auth", "0012_alter_user_first_name_max_length"),
+ ("manager", "0004_alter_clip_id"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="MyUser",
+ fields=[
+ (
+ "user_ptr",
+ models.OneToOneField(
+ auto_created=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ parent_link=True,
+ primary_key=True,
+ serialize=False,
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ("clip_amount", models.IntegerField(default=0)),
+ ],
+ options={
+ "verbose_name": "user",
+ "verbose_name_plural": "users",
+ "abstract": False,
+ },
+ bases=("auth.user",),
+ managers=[
+ ("objects", django.contrib.auth.models.UserManager()),
+ ],
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="account",
+ field=models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE, to="manager.myuser"
+ ),
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 13:42
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ("manager", "0005_myuser_alter_clip_account"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="clip",
+ name="date_added",
+ field=models.DateTimeField(null=True),
+ ),
+ migrations.AlterField(
+ model_name="clip",
+ name="account",
+ field=models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
+ ),
+ ),
+ migrations.DeleteModel(
+ name="MyUser",
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 13:50
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0006_clip_date_added_alter_clip_account_delete_myuser"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="ResetTime",
+ fields=[
+ (
+ "id",
+ models.BigAutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ ("date_time", models.DateTimeField()),
+ ],
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 13:58
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0007_resettime"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="resettime",
+ name="clip_amount",
+ field=models.IntegerField(default=2),
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 14:00
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0008_resettime_clip_amount"),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name="resettime",
+ old_name="clip_amount",
+ new_name="max_clips",
+ ),
+ ]
--- /dev/null
+# Generated by Django 4.1.5 on 2023-01-22 14:02
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("manager", "0009_rename_clip_amount_resettime_max_clips"),
+ ]
+
+ operations = [
+ migrations.RenameModel(
+ old_name="ResetTime",
+ new_name="ResetData",
+ ),
+ ]
from django.contrib.auth.models import User
-# Create your models here.
+class ResetData(models.Model):
+ date_time = models.DateTimeField()
+ max_clips = models.IntegerField(default=2)
+
+
class Clip(models.Model):
- id = models.CharField(primary_key=True, max_length=100)
+ id = models.CharField(primary_key=True, max_length=100, unique=True)
url = models.URLField(null=True)
embed_url = models.URLField(null=True)
broadcaster_id = models.CharField(null=True, max_length=100)
thumbnail_url = models.URLField(null=True)
duration = models.FloatField(null=True)
vod_offset = models.IntegerField(null=True)
+ date_added = models.DateTimeField(null=True)
account = models.ForeignKey(User, on_delete=models.CASCADE)
import requests
-from .models import Clip
+import datetime
+from .errors import TooManyClips
+from .models import Clip, ResetData
from urllib.parse import urlparse
from allauth.socialaccount.models import SocialApp, SocialToken
from django.contrib.auth.models import User
def request_clip(user, clip: str):
id = urlparse(clip).path.split("/")[-1]
+ user = User.objects.get(id=user.id)
+ reset_data = ResetData.objects.first()
+ reset_time = reset_data.date_time
+ max_clips = reset_data.max_clips
+
+ if (
+ len(
+ Clip.objects.filter(account=user).filter(
+ date_added__gt=reset_time
+ )
+ )
+ >= max_clips
+ ):
+ raise TooManyClips
social_app: SocialApp = SocialApp.objects.first()
oauth = SocialToken.objects.first().token
data = r.json()["data"][0]
print(data)
- Clip(
+ clip = Clip(
id=data["id"],
url=data["url"],
embed_url=data["embed_url"],
thumbnail_url=data["thumbnail_url"],
duration=data["duration"],
vod_offset=data["vod_offset"],
- account=User.objects.get(id=user.id),
- ).save()
+ date_added=datetime.datetime.now(),
+ account=user,
+ )
+ clip.validate_unique()
+ clip.save()
return
from django.http import HttpResponse
from django.shortcuts import render
+from django.core.exceptions import ValidationError
from .request_clip import request_clip
from .forms import ClipForm
+from .errors import TooManyClips
def get_name(request):
if form.is_valid():
try:
request_clip(user=request.user, clip=form.cleaned_data["clip"])
+ except ValidationError as e:
+ print(e)
+ return HttpResponse(b"<h1>Clip already exists</h1>")
+ except TooManyClips:
+ return HttpResponse(b"<h1>You have submitted the maximum number of clips</h1>")
except Exception as e:
print(e)
return HttpResponse(b"<h1>NOPE</h1>")