util.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import datetime
  2. import re
  3. import humanize
  4. # given amount of seconds makes it into this 54 MINUTES AND 53 SECONDS (from humanize) then
  5. # perform a bunch of replace operations to make it look like 54mins. 53secs.
  6. import pytz
  7. def getHumanizedTimeString(seconds):
  8. return humanize.precisedelta(
  9. datetime.timedelta(seconds=seconds)).upper(). \
  10. replace(' month'.upper(), 'm.').replace(' months'.upper(), 'm.').replace(' days'.upper(), 'd.').replace(
  11. ' day'.upper(), 'd.').replace(' hours'.upper(), 'hrs.').replace(' hour'.upper(), 'hr.').replace(
  12. ' minutes'.upper(), 'mins.').replace(' minute'.upper(), 'min.').replace(
  13. 'and'.upper(), '').replace(' seconds'.upper(), 'secs.').replace(' second'.upper(), 'sec.').replace(',', '')
  14. # input => ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ..., '100'] <- array of 100 video ids
  15. # output => ['1,2,3,4,...,50', '51,52,53,...,100'] <- array of 2 video id strings, each with upto 50 comma sperated video ids
  16. def getVideoIdsStrings(video_ids):
  17. output = []
  18. i = 0
  19. while i < len(video_ids):
  20. output.append(','.join(video_ids[i:i + 50]))
  21. i += 50
  22. return output
  23. # input: array of youtube video duration strings => ['24M23S', '2H2M2S', ...]
  24. # output:integer => seconds
  25. def calculateDuration(vid_durations):
  26. hours_pattern = re.compile(r'(\d+)H')
  27. minutes_pattern = re.compile(r'(\d+)M')
  28. seconds_pattern = re.compile(r'(\d+)S')
  29. total_seconds = 0
  30. for duration in vid_durations:
  31. hours = hours_pattern.search(duration) # returns matches in the form '24H'
  32. mins = minutes_pattern.search(duration) # '24M'
  33. secs = seconds_pattern.search(duration) # '24S'
  34. hours = int(hours.group(1)) if hours else 0 # returns 24
  35. mins = int(mins.group(1)) if mins else 0
  36. secs = int(secs.group(1)) if secs else 0
  37. video_seconds = datetime.timedelta(hours=hours, minutes=mins, seconds=secs).total_seconds()
  38. total_seconds += video_seconds
  39. return total_seconds
  40. def getThumbnailURL(thumbnails):
  41. priority = ('maxres', 'standard', 'high', 'medium', 'default')
  42. for quality in priority:
  43. if quality in thumbnails:
  44. return thumbnails[quality]['url']
  45. return ''
  46. # generates a message in the form of '1 / 19 watched! 31mins. 15secs. left to go!'
  47. def generateWatchingMessage(playlist):
  48. """
  49. This is the message that will be seen when a playlist is set to watching.
  50. Takes in the playlist object and calculates the watch time left by looping over unwatched video
  51. and using their durations
  52. """
  53. pass
  54. def getVideoId(video_link):
  55. """
  56. takes in a valid video link and returns a video id
  57. """
  58. if '?' not in video_link:
  59. return video_link
  60. temp = video_link.split('?')[-1].split('&')
  61. for el in temp:
  62. if 'v=' in el:
  63. return el.split('v=')[-1]
  64. def increment_tag_views(playlist_tags):
  65. """
  66. Increments playlist tag overall views and views per week. If its been a week, views per week is reset to
  67. zero.
  68. """
  69. for tag in playlist_tags:
  70. # reset tag views if its been a week
  71. if tag.last_views_reset + datetime.timedelta(days=7) < datetime.datetime.now(pytz.utc):
  72. tag.times_viewed_per_week = 0
  73. tag.last_views_reset = datetime.datetime.now(pytz.utc)
  74. else:
  75. tag.times_viewed_per_week += 1
  76. tag.times_viewed += 1
  77. tag.save(update_fields=['times_viewed', 'last_views_reset', 'times_viewed_per_week'])