aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 7b84260b6ae67d2a8d550d2696f2750434cf9994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from VaporSong import VaporSong
import os
import sys
import youtube_dl
import logzero
from logzero import logger
from logzero import setup_logger
import re
import urllib.request
import urllib.parse

import time

MAX_DURATION = 600 # In-case the program finds a compilation
youtube_urls = ('youtube.com', 'https://www.youtube.com/', 'http://www.youtube.com/', 'http://youtu.be/', 'https://youtu.be/', 'youtu.be')

def download_file(query,request_id=1):
    """Returns audio to the vapor command handler

    Searches YouTube for 'query', finds first match that has
    duration under the limit, download video with youtube_dl
    and extract .wav audio with ffmpeg.

    Query can be YouTube link. 
    """
    ydl_opts = {
        'quiet': 'True',
        'format': 'bestaudio/best',
        'outtmpl': str(request_id) +'.%(ext)s',
        'prefer_ffmpeg': 'True', 
        'noplaylist': 'True',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',
            'preferredquality': '192',
        }],
    }

    original_path = str(request_id) + ".wav"
    file_title = ""

    # check if query is youtube url
    if not query.lower().startswith((youtube_urls)):
        # search for youtube videos matching query
        query_string = urllib.parse.urlencode({"search_query" : query})
        html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
        search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
        info = False

        # find video that fits max duration
        logger.info("Get video information...")
        for url in search_results:
            # check for video duration
            try:
                info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url,download = False)
            except Exception as e:
                logger.error(e)
                raise ValueError('Could not get information about video.')
            full_title = info['title']
            if (info['duration'] < MAX_DURATION and info['duration'] >= 5):
                # get first video that fits the limit duration
                logger.info("Got video: " + str(full_title))
                file_title = info['title']
                break
        
        # if we ran out of urls, return error
        if (not info):
            raise ValueError('Could not find a video.')

    # query was a youtube link
    else:
        logger.info("Query was a YouTube URL.")
        url = query
        info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url,download = False)
        file_title = info['title']
        # check if video fits limit duration
        if (info['duration'] < 5 or info['duration'] > MAX_DURATION):
            raise ValueError('Video is too short. Need 5 seconds or more.')

    # download video and extract audio
    logger.info("Downloading video...")
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        try:
            ydl.download([url])
        except Exception as e:
            logger.error(e)
            raise ValueError('Could not download ' + str(full_title) + '.')

    return original_path, file_title


def gen_vapor(filePath, title):
	# Delete stuff if there is anything left over.
	os.system("rm -r download/")
	os.system("rm -r beats/")

    # Make the proper folders for intermediate steps
	os.system("mkdir download/")
	os.system("mkdir beats/")


	# Download the youtube query's first result. Might be wrong but YOLO
	#YTDownloader.download_wav_to_samp2(query)

	# For every song in download folder(just one for now)
	"""
	for fs in os.listdir("download/"):
		# Slow down the song.
		VaporSong.vaporize_song(query,"download/"+fs)
		pass
	# When we are finished, delete the old folders.
	"""
	VaporSong.vaporize_song(filePath, title)

	os.system("rm -r download/")
	os.system("rm -r beats/")


	
## Makes this a command line tool: disable when we get the webserver going
sys.argv.pop(0)
query = ""
for s in sys.argv:
	query = query + s

name, title = download_file(query)
gen_vapor(name, title)