# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="Ec0Frygbq2aa" outputId="320f51fa-6319-42c0-c31a-70b655e8c7e2"
#@title
# @markdown <center><h3>Input Your Mega ID</h3></center><br>
from functools import wraps
import errno
import os
import signal
import subprocess
import shlex


class TimeoutError(Exception):
    pass


def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator


if not os.path.exists("/root/.ipython/ocr.py"):
    from subprocess import run
    from shlex import split

    shellCmd = "wget -qq https://raw.githubusercontent.com/biplobsd/OneClickRun/master/res/ocr.py \
                    -O /root/.ipython/ocr.py"
    run(split(shellCmd))
from ocr import runSh

@timeout(10)
def runShT(args):
    return runSh(args, output=True)


# MEGAcmd installing
if not os.path.exists("/usr/bin/mega-cmd"):
    print("Installing MEGA ...")
    runSh('sudo apt-get -y update')
    runSh('sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https')
    runSh('sudo curl -sL -o /var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb', output=True)
    runSh('sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb', output=True)
    print("MEGA is installed.")
else:
    # !pkill mega-cmd

# INPUT YOUR MEGA ID

USERNAME = '<EMAIL>'  # @param {type:"string"}
PASSWORD = "<PASSWORD>"  # @param {type:"string"}
if not (USERNAME == "" or PASSWORD == ""):
    try:
        runShT(f"mega-login {USERNAME} {PASSWORD}")
    except TimeoutError:
        runSh('mega-whoami', output=True)
else:
    print("Please Input your Mega IDs.")
# @markdown *After signing in, use the above cell(Main Cell) to transfer files from Mega to GDrive using the transfer quota of your Mega Pro Account*

# + [markdown] id="G01b9F1VrQwE"
# # Download the Yolo database form Mega.nz drive

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="-sJ24PLUrXXz" outputId="dc114f19-1754-4e7a-aea4-6aa039e5ea4c"
import sys, os, urllib.request
import time
import subprocess
import contextlib
from IPython.display import clear_output
#@markdown <br><center><img src='https://mega.nz/favicon.ico?v=3' height="50" alt="MEGA-logo"/></center>
#@markdown <center><h2>Transfer from Mega to GDrive</h2></center><br>
HOME = os.path.expanduser("~")
if not os.path.exists(f"{HOME}/.ipython/ocr.py"):
    hCode = "https://raw.githubusercontent.com/biplobsd/" \
                "OneClickRun/master/res/ocr.py"
    urllib.request.urlretrieve(hCode, f"{HOME}/.ipython/ocr.py")

from ocr import (
    runSh,
    loadingAn,
)
#@title MEGA public link download
URL = "https://mega.nz/folder/xEljCCgD#KAq3WTA897PFhXWlaR1s6g" #@param {type:"string"}
OUTPUT_PATH = "" #@param {type:"string"}
if not OUTPUT_PATH:
  os.makedirs("downloads", exist_ok=True)
  OUTPUT_PATH = "downloads"
# MEGAcmd installing
if not os.path.exists("/usr/bin/mega-cmd"):
    loadingAn()
    print("Installing MEGA ...")
    runSh('sudo apt-get -y update')
    runSh('sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https')
    runSh('sudo curl -sL -o /var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb', output=True)
    runSh('sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb', output=True)
    print("MEGA is installed.")
    clear_output()

# Unix, Windows and old Macintosh end-of-line
newlines = ['\n', '\r\n', '\r']

def unbuffered(proc, stream='stdout'):
    stream = getattr(proc, stream)
    with contextlib.closing(stream):
        while True:
            out = []
            last = stream.read(1)
            # Don't loop forever
            if last == '' and proc.poll() is not None:
                break
            while last not in newlines:
                # Don't loop forever
                if last == '' and proc.poll() is not None:
                    break
                out.append(last)
                last = stream.read(1)
            out = ''.join(out)
            yield out


def transfare():
    import codecs
    decoder = codecs.getincrementaldecoder("UTF-8")()
    cmd = ["mega-get", URL, OUTPUT_PATH]
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        # Make all end-of-lines '\n'
        universal_newlines=True,
    )
    for line in unbuffered(proc):
        print(line)
        


transfare()



# + [markdown] id="u9dH81eXubaL"
# # Convering Every video to some phot in folders

# + colab={"base_uri": "https://localhost:8080/"} id="WOWY1-JFuawQ" outputId="42333b4c-216e-4b09-db61-a2f5d2e60b4f"


from ocr import runSh

def runShTS(args):
    return runSh(args, output=True)
runShTS('mega-cd /')
runShTS('mega-mkdir Yolo_car_database_photos')
runShTS('mega-cd Yolo_car_database_photos')

def change_Video_name(name):
    name_file = ''.join(name)
    name_file = name_file.replace(".mp4", "_")
    
    return name_file
import os

def convert_to_photo(root, Video_name,photo_target_folder):
  runShTS('mkdir '+photo_target_folder)
  print (Video_name+'\n')
  print ('ffmpeg -i {}'.format(root)+' -vf fps=1 '+Video_name+'out%d.png')
  runShTS('ffmpeg -i {}'.format(root)+' -vf fps=1 '+Video_name+'out%d.png')
   
def Upload_phtos_to_mega(root,mkdir_name):
  runShTS('mega-cd /Yolo_car_database_photos')
  runShTS('mega-mkdir '+mkdir_name)
  runShTS('mega-cd '+mkdir_name)
  runShTS('mega-put '+root+ ' .')
  # for path, subdirs, files in os.walk( Yolo_video_path):
  #     for name in subdirs:
  
  #       runShTS('mega-cd Yolo_car_database_photos' + title1 + '/')
  #       runShTS('mega-put '+ root+' . ')
  #       runShTS(' mega-ls')

Yolo_video_path='/content/downloads/Yolo_car_database/'
Yolo_photo_path='/content/downloads/Yolo_car_database_photo/'
runShTS('rm -rf '+Yolo_photo_path)
runShTS('mkdir '+Yolo_photo_path)

for path, subdirs, files in os.walk( Yolo_video_path):
      for name in subdirs:
        #  convert_to_phot(Yolo_video_path)
        print (path+'/'+name+'\n')
        Vidoe_folder=path+'/'+name+'/'
        for path2, subdirs2, files2 in os.walk( path+'/'+name):
          for name2 in files2:
             print (path2+'/'+name2+'\n')
             if name2.endswith(".mp4"):
                photo_folder=Yolo_photo_path+'/'+name+'/'
                convert_to_photo(path2+'/'+name2,Yolo_photo_path+name+'/'+change_Video_name(name2),Yolo_photo_path+name+'/')
                Upload_phtos_to_mega(photo_folder,name)

# for path, subdirs, files in os.walk( Yolo_photo_path):
#       for name in subdirs:
#         #  convert_to_phot(Yolo_video_path)
#         print ('Photo Path is :'+path+'/'+name+'\n')
#         photo_folder=path+'/'+name+'/'
#         Upload_phtos_to_mega(photo_folder,name)


# + id="16UDlsv-1gC6"
# !sudo apt install tree
# !tree /content/downloads/Yolo_car_database_photo

StarcoderdataJupyterScriptsDedupFiltered