def create_new_videos(videos_info): videos = [] for video_info in videos_info: video = Video(yt_id=video_info['id'], yt_title=video_info['title'], published_at_yt=video_info['published_at']) video.title = clean_video_title(video_info['title']) video.description = clean_video_description(video_info['description']) video.save() videos.append(video) request = requests.get(video_info['thumbnail_url'], stream=True) if request.status_code == requests.codes.ok: tempimg = tempfile.NamedTemporaryFile() for block in request.iter_content(1024 * 8): if not block: break tempimg.write(block) video.thumbnail = files.File(tempimg) video.save() # make thumbnail be "cut" in the right ratio for the video thumbnails ratio = 275.0 / 154.0 t_width = int( min(video.thumbnail.width, video.thumbnail.height * ratio)) t_height = int( min(video.thumbnail.width / ratio, video.thumbnail.height)) #center the cut y_origin = video.thumbnail.height / 2 - t_height / 2 y_end = y_origin + t_height t_ratio = '0,' + str(y_origin) + str(t_width) + ',' + str(y_end) video.thumbnail_ratio = t_ratio video.save() if 'tags' in video_info: for video_tag in video_info['tags']: tag = Tag.objects.create(video=video, name=video_tag) return videos
def save_obj_from_entry(response): """ Takes response object and stores it into db """ v = Video() v.title = response['snippet']['title'] v.ytid = response['id'] unique_slugify(v, v.title) v.published_on = isodate.parse_datetime(response['snippet']['publishedAt']) # get duration of video duration = response["contentDetails"]["duration"] v.duration = int(isodate.parse_duration(duration).total_seconds()) # get description of video try: v.description = response['snippet']['description'] except: pass # get category try: category_response = youtube_api.videoCategories().list( part="snippet", id=response['snippet']['categoryId']).execute() v.category = category_response.get("items", [])[0]['snippet']['title'] except: pass # get thumbnail - "default" try: v.thumbnail = response['snippet']['thumbnails']['default']['url'] except: pass # get view count try: v.views = int(response['statistics']['viewCount']) except: pass v.save() return v
def _import_video(self, video_url, videoid, title, description, thumbnail, videosrt): videoid_match = VIDEOID_RE.search(videoid) videoid = videoid_match.group(1) video_type = YoutubeVideoType( 'http://www.youtube.com/watch?v={0}'.format(videoid)) try: video_url_obj = VideoUrl.objects.get( url=video_type.convert_to_video_url()) video = video_url_obj.video except ObjectDoesNotExist: video_url_obj = None video = Video() video.youtube_videoid = videoid video.title = title video.description = description if video_type.entry.media.duration: video.duration = int(video_type.entry.media.duration.seconds) video.thumbnail = thumbnail video.save() Action.create_video_handler(video) if videosrt: self._import_srt(video, videosrt) else: SubtitleLanguage(video=video, language='en', is_original=True, is_forked=True).save() if not video_url_obj: video_url_obj = VideoUrl(videoid=videoid, url=video_type.convert_to_video_url(), type=video_type.abbreviation, original=True, primary=True, video=video) video_url_obj.save() self._save_alternate_url(video, video_url)
def save_obj_from_entry(entry): """ Takes entry(gdata) object and stores it into db """ v = Video() v.title = entry.media.title.text v.ytid = getid(entry) unique_slugify(v, v.title) v.duration = int(entry.media.duration.seconds) v.description = entry.media.description.text v.published_on = get_date(entry.published.text) v.category = entry.media.category[0].text if entry.rating is not None: v.rating = float(entry.rating.average) if entry.media.thumbnail is not None: v.thumbnail = entry.media.thumbnail[0].url try: v.views = int(entry.statistics.view_count) except: v.views = 0 v.save() return v
def _import_video(self, video_url, videoid, title, description, thumbnail, videosrt): videoid_match = VIDEOID_RE.search(videoid) videoid = videoid_match.group(1) video_type = YoutubeVideoType( 'http://www.youtube.com/watch?v={0}'.format(videoid)) try: video_url_obj = VideoUrl.objects.get( url=video_type.convert_to_video_url()) video = video_url_obj.video except ObjectDoesNotExist: video_url_obj = None video = Video() video.youtube_videoid = videoid video.title = title video.description = description if video_type.entry.media.duration: video.duration = int(video_type.entry.media.duration.seconds) video.thumbnail = thumbnail video.save() Action.create_video_handler(video) if videosrt: self._import_srt(video, videosrt) else: SubtitleLanguage(video=video, language='en', is_original=True, is_forked=True).save() if not video_url_obj: video_url_obj = VideoUrl( videoid=videoid, url=video_type.convert_to_video_url(), type=video_type.abbreviation, original=True, primary=True, video=video) video_url_obj.save() self._save_alternate_url(video, video_url)
def handle(self, *args, **options): if len(args) > 2: raise CommandError('Too many arguments given. Provide either one or two usernames.') elif len(args) == 2: try: free_user = User(username=args[0], first_name=args[0] + '_free', last_name='User', email='*****@*****.**') paid_user = User(username=args[1], first_name=args[1] + '_paid', last_name='User', email='*****@*****.**') free_user.save() paid_user.save() self.stdout.write('Successfully created fake users using supplied usernames.\n') except: raise CommandError("Could not create users with supplied usernames. Try different usernames.") elif len(args) == 1: try: free_user = User(username=args[0] + '_free', first_name=args[0] + '_free', last_name='User', email='*****@*****.**') paid_user = User(username=args[0] + '_paid', first_name=args[0] + '_paid', last_name='User', email='*****@*****.**') free_user.save() paid_user.save() self.stdout.write('Successfully created fake users using the supplied username + suffixes "_free" and "_paid".\n') except: raise CommandError("Could not create users with supplied username. Try a different username.") elif len(args) == 0: try: free_user = User(username='******', first_name='fakeuser_free', last_name='User', email='*****@*****.**') paid_user = User(username='******', first_name='fakeuser_paid', last_name='User', email='*****@*****.**') free_user.save() paid_user.save() self.stdout.write('Successfully created fake users using default usernames.\n') except: raise CommandError("Fake users already exist. Create differently named users using 'manage.py create_test_accounts <username>'") free_user.set_password('password') paid_user.set_password('password') free_user.save() paid_user.save() # Set paid_user account paid_level = AccountLevel.objects.get(level='Paid') paid_user.userprofile.account_level = paid_level paid_user.userprofile.save() # Create videos for free users for u in (free_user, paid_user): created_videos = () for i in range(10): temp_slug = generate_slug(5) temp_vid = Video(uploader=u, title="Test Fake Uploads Video " + str(i), slug=temp_slug) temp_vid.save() created_videos = created_videos + (temp_vid,) #### Videos #Normal temp_vid = created_videos[0] temp_vid.title = "Normal private video" temp_vid.description = "Normal private video." temp_vid.save() upload_to_s3(temp_vid.slug) ### Expired videos, for free user temp_vid = created_videos[1] if not u == free_user: upload_to_s3(temp_vid.slug) temp_vid = created_videos[2] upload_to_s3(temp_vid.slug) temp_vid = created_videos[3] upload_to_s3(temp_vid.slug) #Expired Yesterday, created 2 days ago temp_vid = created_videos[1] temp_vid.title = "Expired the day previous to creation" temp_vid.description = "Expired the day previous to creation." temp_vid.created = datetime.datetime.now() - datetime.timedelta(days=2) temp_vid.save() if u == free_user: temp_vid.expired = True temp_vid.expiry_date = datetime.datetime.now() - datetime.timedelta(days=1) temp_vid.save() temp_vid.delete() #Expires Today in 4 hours temp_vid = created_videos[2] temp_vid.title = "Expires 4 hours from creation" temp_vid.description = "Expires 4 hours from creation." if u == free_user: temp_vid.expiry_date = datetime.datetime.now() + datetime.timedelta(hours=4) temp_vid.save() #Expires in 3 days temp_vid = created_videos[3] temp_vid.title = "Expires 3 days from creation" temp_vid.description = "Expires 3 days from creation." if u == free_user: temp_vid.expiry_date = datetime.datetime.now() + datetime.timedelta(days=3) temp_vid.save() #Created Yesterday temp_vid = created_videos[4] upload_to_s3(temp_vid.slug) temp_vid.created = datetime.datetime.now() - datetime.timedelta(days=1) temp_vid.title = "Time-stamped as created the day previous to actual creation" temp_vid.description = "Time-stamped as created the day previous to actual creation." temp_vid.save() #Created A Week Previous temp_vid = created_videos[5] upload_to_s3(temp_vid.slug) temp_vid.created = datetime.datetime.now() - datetime.timedelta(days=7) temp_vid.title = "Time-stamped as created a week previous to actual creation" temp_vid.description = "Time-stamped as created a week previous to actual creation." temp_vid.save() #Public temp_vid = created_videos[6] upload_to_s3(temp_vid.slug) temp_vid.is_public = True temp_vid.title = "Public." temp_vid.description = "Public." temp_vid.save() #Public. Expired Yesterday, created 2 days ago temp_vid = created_videos[7] upload_to_s3(temp_vid.slug) temp_vid.title = "Public. Expired the day previous to creation" temp_vid.description = "Public. Expired the day previous to creation." temp_vid.is_public = True temp_vid.created = datetime.datetime.now() - datetime.timedelta(days=2) if u == free_user: temp_vid.expired = True temp_vid.expiry_date = datetime.datetime.now() - datetime.timedelta(days=1) temp_vid.save() #Outlined temp_vid = created_videos[8] upload_to_s3(temp_vid.slug) temp_vid.title = "Outlined" temp_vid.description = "Outlined." temp_vid.save() outline = VideoOutline(video=temp_vid) outline.save() outline_pin = VideoOutlinePin(video_outline=outline, text="Start") outline_pin.save() #Transferred to Youtube, created 2 days ago, deleted from server yesterday temp_vid = created_videos[9] upload_to_s3(temp_vid.slug) temp_vid.youtube_embed_url= "http://www.youtube.com/embed/yc5W-AClSlI" temp_vid.created = datetime.datetime.now() - datetime.timedelta(days=2) temp_vid.youtube_video_expiry_date = datetime.datetime.now() - datetime.timedelta(days=1) temp_vid.title = "YouTube video" temp_vid.description = "YouTube video." temp_vid.save() self.stdout.write('Successfully created videos!\n')