- Display clip info more clearly
-- Clip comments?
-
-- Refresh tokens
# Provider specific settings
SOCIALACCOUNT_PROVIDERS = {
"twitch": {
- "SCOPE": [
- ],
+ "SCOPE": [],
},
}
AUTHENTICATION_BACKENDS = [
urlpatterns = [
path("admin/", admin.site.urls),
- path('accounts/', include('allauth.urls')),
- path('', TemplateView.as_view(template_name="manager/index.html"), name='index'),
- path('input/', get_name, name="input"),
- path('show/<int:id>', show_clips, name="show"),
- path('final', final_ranking, name="final"),
+ path("accounts/", include("allauth.urls")),
+ path("", TemplateView.as_view(template_name="manager/index.html"), name="index"),
+ path("input/", get_name, name="input"),
+ path("show/<int:id>", show_clips, name="show"),
+ path("final", final_ranking, name="final"),
]
class ClipTooOldError(Exception):
pass
+
+
+class BadRequestError(Exception):
+ pass
+
+
+class UnauthorizedError(Exception):
+ pass
+
+
+class NotFoundError(Exception):
+ pass
reset_data = models.ForeignKey(ResetData, on_delete=models.CASCADE)
class Meta:
- unique_together = ('broadcaster_id', 'reset_data',)
+ unique_together = (
+ "broadcaster_id",
+ "reset_data",
+ )
class Clip(models.Model):
import requests
import datetime
import pytz
-from .errors import TooManyClipsError, TooLateError, ChannelNotAllowedError, UserNotCreatedClipError, ClipTooOldError
+from .errors import (
+ TooManyClipsError,
+ TooLateError,
+ ChannelNotAllowedError,
+ UserNotCreatedClipError,
+ ClipTooOldError,
+ BadRequestError,
+ UnauthorizedError,
+ NotFoundError,
+)
from .models import Clip, ResetData
from urllib.parse import urlparse
from allauth.socialaccount.models import SocialApp, SocialToken, SocialAccount
from django.contrib.auth.models import User
+def update_tokens(social_app: SocialApp, social_token: SocialToken):
+ token_secret = social_token.token_secret
+ client_id = social_app.client_id
+ client_secret = social_app.secret
+
+ r = requests.post(
+ "https://id.twitch.tv/oauth2/token",
+ headers={
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ params={
+ "client_id": client_id,
+ "client_secret": client_secret,
+ "grant_type": "refresh_token",
+ "refresh_token": token_secret,
+ },
+ )
+
+ r.raise_for_status()
+
+ data = r.json()
+ social_token.token = data["access_token"]
+ social_token.token_secret = data["refresh_token"]
+ social_token.save()
+
+
def request_clip(user, clip: str):
id = urlparse(clip).path.split("/")[-1]
user = User.objects.get(id=user.id)
- reset_data = ResetData.objects.latest('date_time')
+ social_account = SocialAccount.objects.get(user=user)
+ reset_data = ResetData.objects.latest("date_time")
reset_time = reset_data.date_time
max_clips = reset_data.max_clips
raise TooLateError
if (
- len(
- Clip.objects.filter(account=user).filter(
- date_added__gt=reset_time
- )
- )
+ len(Clip.objects.filter(account=user).filter(date_added__gt=reset_time))
>= max_clips
):
raise TooManyClipsError
social_app: SocialApp = SocialApp.objects.first()
- oauth = SocialToken.objects.first().token
+ social_token = SocialToken.objects.get(account=social_account)
+ oauth = social_token.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}",
+ "Client-Id": client_id,
},
params={"id": id},
)
+ if r.status_code == 401:
+ update_tokens(social_app, social_token)
+
+ oauth = social_token.token
+ client_id = social_app.client_id
+ r = requests.get(
+ "https://api.twitch.tv/helix/clips",
+ headers={
+ "Authorization": f"Bearer {oauth}",
+ "Client-Id": client_id,
+ },
+ params={"id": id},
+ )
- if r.status_code != 200:
- raise Exception
+ if r.status_code == 400:
+ raise BadRequestError
+ if r.status_code == 404:
+ raise NotFoundError
+ if r.status_code == 401:
+ raise UnauthorizedError
+ r.raise_for_status()
- data = r.json()["data"][0]
+ try:
+ data = r.json()["data"][0]
+ except IndexError:
+ raise NotFoundError
if reset_data.user_created_clip:
if not SocialAccount.objects.get(user=user).uid == data["creator_id"]:
raise UserNotCreatedClipError
if reset_data.allowedchannel_set.count() != 0:
- allowed_channels = reset_data.allowedchannel_set.filter(broadcaster_id=data["broadcaster_id"])
+ allowed_channels = reset_data.allowedchannel_set.filter(
+ broadcaster_id=data["broadcaster_id"]
+ )
if not allowed_channels.exists():
raise ChannelNotAllowedError
from .request_clip import request_clip
from .forms import ClipForm, RankForm
-from .errors import TooManyClipsError, TooLateError, ChannelNotAllowedError, UserNotCreatedClipError, ClipTooOldError
+from .errors import (
+ TooManyClipsError,
+ TooLateError,
+ ChannelNotAllowedError,
+ UserNotCreatedClipError,
+ ClipTooOldError,
+ BadRequestError,
+ UnauthorizedError,
+ NotFoundError,
+)
from .models import Clip, ResetData
+import traceback
@staff_member_required
def show_clips(request, id):
- reset_data = ResetData.objects.latest('date_time')
+ reset_data = ResetData.objects.latest("date_time")
reset_time = reset_data.date_time
try:
video = Clip.objects.filter(
video.rank = form.cleaned_data["value"]
video.save()
return redirect(show_clips, id=id + 1)
- return render(request, "manager/clip_viewer.html", context={
- "video": video,
- "ranks": range(reset_data.ranks, 0, -1),
- })
+ return render(
+ request,
+ "manager/clip_viewer.html",
+ context={
+ "video": video,
+ "ranks": range(reset_data.ranks, 0, -1),
+ },
+ )
@staff_member_required
def final_ranking(request):
- reset_data = ResetData.objects.latest('date_time')
+ reset_data = ResetData.objects.latest("date_time")
reset_time = reset_data.date_time
- return render(request, "manager/final.html", context={
- "videos": Clip.objects.filter(date_added__gt=reset_time),
- "ranks": range(reset_data.ranks, 0, -1),
- })
+ return render(
+ request,
+ "manager/final.html",
+ context={
+ "videos": Clip.objects.filter(date_added__gt=reset_time),
+ "ranks": range(reset_data.ranks, 0, -1),
+ },
+ )
def get_name(request):
message = "Sorry, you need to be the one who has created the clip."
except ClipTooOldError:
message = "Sorry, the clip is too old. Please send a newer clip."
+ except BadRequestError:
+ message = "Sorry, a bad request happened on our side."
+ except UnauthorizedError:
+ message = "We couldn't connect to twitch servers. Try logging out and logging back in."
+ except NotFoundError:
+ message = "Sorry, we couldn't find that twitch clip. Make sure to check the url."
except Exception:
message = "Something went wrong."
else: