def read_file_content(self, file_url=None):
        """Return name of temp file in which remote file is saved."""
        if not file_url:
            file_url = self.url
            pywikibot.warning("file_url is not given. "
                              "Set to self.url by default.")
        pywikibot.output(u'Reading file %s' % file_url)
        resume = False
        rlen = 0
        _contents = None
        dt = 15
        uo = URLopener()
        retrieved = False

        while not retrieved:
            if resume:
                pywikibot.output(u"Resume download...")
                uo.addheader('Range', 'bytes=%s-' % rlen)

            infile = uo.open(file_url)

            if 'text/html' in infile.info().getheader('Content-Type'):
                pywikibot.output(u"Couldn't download the image: "
                                 "the requested URL was not found on server.")
                return

            content_len = infile.info().getheader('Content-Length')
            accept_ranges = infile.info().getheader('Accept-Ranges') == 'bytes'

            if resume:
                _contents += infile.read()
            else:
                _contents = infile.read()

            infile.close()
            retrieved = True

            if content_len:
                rlen = len(_contents)
                content_len = int(content_len)
                if rlen < content_len:
                    retrieved = False
                    pywikibot.output(
                        u"Connection closed at byte %s (%s left)" %
                        (rlen, content_len))
                    if accept_ranges and rlen > 0:
                        resume = True
                    pywikibot.output(u"Sleeping for %d seconds..." % dt)
                    time.sleep(dt)
                    if dt <= 60:
                        dt += 15
                    elif dt < 360:
                        dt += 60
            else:
                pywikibot.log(
                    u"WARNING: length check of retrieved data not possible.")
        handle, tempname = tempfile.mkstemp()
        with os.fdopen(handle, "wb") as t:
            t.write(_contents)
        return tempname
Beispiel #2
0
    def read_file_content(self, file_url=None):
        """Return name of temp file in which remote file is saved."""
        if not file_url:
            file_url = self.url
            pywikibot.warning("file_url is not given. "
                              "Set to self.url by default.")
        pywikibot.output(u'Reading file %s' % file_url)
        resume = False
        rlen = 0
        _contents = None
        dt = 15
        uo = URLopener()
        retrieved = False

        while not retrieved:
            if resume:
                pywikibot.output(u"Resume download...")
                uo.addheader('Range', 'bytes=%s-' % rlen)

            infile = uo.open(file_url)

            if 'text/html' in infile.info().getheader('Content-Type'):
                pywikibot.output(u"Couldn't download the image: "
                                 "the requested URL was not found on server.")
                return

            content_len = infile.info().getheader('Content-Length')
            accept_ranges = infile.info().getheader('Accept-Ranges') == 'bytes'

            if resume:
                _contents += infile.read()
            else:
                _contents = infile.read()

            infile.close()
            retrieved = True

            if content_len:
                rlen = len(_contents)
                content_len = int(content_len)
                if rlen < content_len:
                    retrieved = False
                    pywikibot.output(
                        u"Connection closed at byte %s (%s left)"
                        % (rlen, content_len))
                    if accept_ranges and rlen > 0:
                        resume = True
                    pywikibot.output(u"Sleeping for %d seconds..." % dt)
                    time.sleep(dt)
                    if dt <= 60:
                        dt += 15
                    elif dt < 360:
                        dt += 60
            else:
                pywikibot.log(
                    u"WARNING: length check of retrieved data not possible.")
        handle, tempname = tempfile.mkstemp()
        with os.fdopen(handle, "wb") as t:
            t.write(_contents)
        return tempname
Beispiel #3
0
def downloadAsset(uri, dirname):
    tUrl = uri
    o = urlparse(tUrl)
    contentType = ""
    # targetDir = os.path.join(CURRENT_DIRECTORY, dirname, '/'.join(o.path.split('/')[1:-1]))
    targetDir = CURRENT_DIRECTORY + '/' + dirname + '/' + '/'.join(
        o.path.split('/')[1:-1])

    # javascript, fragment의 경우 다운로드 불필요
    if o.scheme == "javascript" or (o.netloc == '' and o.path == ''):
        return

    if o.scheme == "":
        if uri.startswith("//"):
            tUrl = f"https:{uri}"
        else:
            tUrl = f"https://{uri}"

    try:
        contentType = getContentType(tUrl)
    except Exception:
        try:
            if uri.startswith('//'):
                tUrl = f"http:{uri}"
            else:
                tUrl = f"http://{uri}"
            contentType = getContentType(tUrl)
        except Exception:
            pass
            # raise Exception("Error during connection")
    else:
        # text/html 무시
        if contentType in mimeTypes[1:]:
            if not os.path.exists(targetDir):
                path = Path(targetDir)
                path.mkdir(parents=True)

            targetFile = targetDir + '/' + o.path.split('/')[-1]
            if not os.path.exists(targetFile):
                try:
                    urlretrieve(tUrl, targetFile)
                    print(f"[Retrieved] {targetFile}")
                except Exception:
                    try:
                        opener = URLopener()
                        opener.addheader('User-Agent', 'Mozilla/5.0')
                        filename, headers = opener.retrieve(tUrl, targetFile)
                    except Exception:
                        try:
                            tUrl = tUrl.replace('www.', '')
                            tUrl = tUrl.replace('http:', 'https:')
                            filename, headers = opener.retrieve(
                                tUrl, targetFile)
                        except Exception as e:
                            print(str(e))
                            raise Exception

        else:
            pass
Beispiel #4
0
def call_api(url, wait=1):
    time.sleep(wait)
    req = URLopener()
    req.addheader('Authorization', 'token ' + TOKEN)
    fp = req.open(url)
    data = json.load(fp)
    fp.close()
    return data
Beispiel #5
0
class HttpFetcherBasic(HttpFetcher):
    def __init__(self, url):
        super().__init__(url)
        self.urlop = URLopener()
        for hdr, val in (tuple(x.split("=", 1)) if "=" in x else (x, "")
                         for x in url.fragment.split("&") if x):
            self.urlop.addheader(hdr, val)

    def open(self, url):
        return self.urlop.open(url)
def main():
    username = input("username: "******"password: "******"http://www.loxa.edu.tw/index.php")
    with urlopen(r) as response:
        phpsessid = response.getheader("set-cookie").split("; ")[0].split("=")[1]

    cookie = "PHPSESSID={0}; Cookie_Allow=1".format(phpsessid)
    data = {"loginname": username, "loginpswd": password}
    r = Request(
        "http://www.loxa.edu.tw/check.php",
        data=urlencode(data).encode("utf8"),
        headers={"cookie": cookie},
        method="POST",
    )
    try:
        response = urlopen(r)
    except HTTPError:
        sys.exit("Invalid username or password.")

    r = Request("http://www.loxa.edu.tw/index.php?login=1&show_msg=Y", headers={"cookie": cookie})
    response = urlopen(r)

    r = Request("http://www.loxa.edu.tw/jewelbox/foldertree.php", headers={"cookie": cookie})
    with urlopen(r) as response:
        html = response.read().decode("big5")

    folder_tree_pattern = re.compile('insFld\(.+?, gFld\(".+?", "file_list.php\?dir_id=(\d+?)", "\w"\)\);')
    file_url_pattern = re.compile('<td colspan=3 nowrap>\s+?<a href="(http.+?)"')
    for i in folder_tree_pattern.finditer(html):
        dir_id = i.group(1)
        r = Request(
            "http://www.loxa.edu.tw/jewelbox/file_list.php?dir_id={0}".format(dir_id), headers={"cookie": cookie}
        )
        with urlopen(r) as response:
            html = response.read().decode("big5")

            for i in file_url_pattern.finditer(html):
                url = i.group(1)
                url_data = urlparse(url)
                file_path = url_data.path.lstrip("/")
                dir_name, base_name = os.path.split(file_path)
                if not os.path.exists(dir_name):
                    os.makedirs(dir_name)
                url_opener = URLopener()
                url_opener.addheader("cookie", cookie)
                print("Download: {0} -> {1}".format(url, file_path))
                url_opener.retrieve(url, file_path)
Beispiel #7
0
def scrape_pokemon_image(url):
    req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    page = urlopen(req).read()
    soup = BeautifulSoup(page, 'html.parser')

    images = soup.find_all('img')
    image_link = images[0].get('src')

    print("[INFO] downloading {}".format(image_link))
    name = str(image_link.split('/')[-1])
    opener = URLopener()
    opener.addheader('User-Agent', 'Mozilla/5.0')
    opener.retrieve(image_link, os.path.join('data/images/', name))

    print(image_link)
from urllib.request import URLopener
from fake_useragent import UserAgent

# Arguments
name, resolution, category, fileformat = argv

#File Count
filesnum = 0

print(f"Resolution {resolution}")
print(f"Category: {category}")
print(f"Format: {fileformat}")

ua = UserAgent()
opener = URLopener()
opener.addheader('User-Agent', ua.chrome)

url = 'https://texturehaven.com/textures/'
url_category = url + '?c=' + category

r = requests.get(url_category, allow_redirects=True, headers={'User-Agent': ua.chrome})
soup = BeautifulSoup(r.text, 'html.parser')

save_to = category+' Texture ' + resolution

try:
    os.mkdir(save_to)
except Exception as e:
    pass
os.chdir(save_to)
Beispiel #9
0
    def read_file_content(self, file_url=None):
        """Return name of temp file in which remote file is saved."""
        if not file_url:
            file_url = self.url
            pywikibot.warning('file_url is not given. '
                              'Set to self.url by default.')
        pywikibot.output('Reading file {}'.format(file_url))
        resume = False
        rlen = 0
        _contents = None
        dt = 15
        uo = URLopener()
        retrieved = False

        while not retrieved:
            if resume:
                pywikibot.output('Resume download...')
                uo.addheader('Range', 'bytes={}-'.format(rlen))

            with closing(uo.open(file_url)) as infile:
                info = infile.info()

                info_get = info.get
                content_type = info_get('Content-Type')
                content_len = info_get('Content-Length')
                accept_ranges = info_get('Accept-Ranges')

                if 'text/html' in content_type:
                    pywikibot.output(
                        "Couldn't download the image: "
                        'the requested URL was not found on server.')
                    return

                valid_ranges = accept_ranges == 'bytes'

                if resume:
                    _contents += infile.read()
                else:
                    _contents = infile.read()

            retrieved = True
            if content_len:
                rlen = len(_contents)
                content_len = int(content_len)
                if rlen < content_len:
                    retrieved = False
                    pywikibot.output(
                        'Connection closed at byte {} ({} left)'.format(
                            rlen, content_len))
                    if valid_ranges and rlen > 0:
                        resume = True
                    pywikibot.output('Sleeping for {} seconds...'.format(dt))
                    pywikibot.sleep(dt)
                    if dt <= 60:
                        dt += 15
                    elif dt < 360:
                        dt += 60
            else:
                pywikibot.log(
                    'WARNING: length check of retrieved data not possible.')
        handle, tempname = tempfile.mkstemp()
        with os.fdopen(handle, 'wb') as t:
            t.write(_contents)
        return tempname
Beispiel #10
0
# Check if already are images in the saving folder, shows them and ask to delete.
existing_imgs = os.listdir(SAVE_FOLDER)
if len(existing_imgs) > 0:
    print('The following images already exist.\n')
    for x in existing_imgs:
        print("---> " + x)
    inp = input('Do you want to remove them? [y/n]: ')
    if inp == 'y':
        for x in existing_imgs:
            os.remove(os.path.join(SAVE_FOLDER, x))
        print('Old images removed.\n')

# Creating the header to later open the images path.
opener = URLopener()
opener.addheader('Referer',
                 "http://www.meteoam.it/prodotti_grafici/bassiStrati")
print('Header created.\n')

# Download the page
print('The page is loading.\n')
PAGE = "http://www.meteoam.it/prodotti_grafici/bassiStrati"
header = {'User-Agent': 'Chrome/87.0.4280.66'}
result = requests.get(PAGE, headers=header)

# Keep repeating until it gets a good output.
while result.status_code != 200:
    print('Code ' + str(result.status_code) + " returned. I'll try again in " +
          str(DELAY) + ' seconds.')
    sleep(DELAY)

# If successful parse the download into a BeautifulSoup object, which allows
Beispiel #11
0
import requests
from urllib.request import URLopener
from fake_useragent import UserAgent

# settings
category = "all"  # choose category.
down_sizes = ["1k", "2k", "4k"]  # you can give multiple values 1,2,4,8,16.
down_thumbnail = False  # download thumbnail image.
down_preview = True  # download preview image.
down_spheres = True  # download spheres image.
owerwrite = False  # overwrite if the file exists, otherwise skip.
# down_folder = "C:\\Users\\agm\\Documents\\GitHub\\hdrihaven_down\\dw"

# urlopener
urlopener = URLopener()
urlopener.addheader("User-Agent", UserAgent().chrome)

# soup
url = "https://hdrihaven.com/hdris/?c=" + category
r = requests.get(url, headers={"User-Agent": UserAgent().chrome})
soup = BeautifulSoup(r.content, "lxml")
items = soup.find("div", id="item-grid").find_all("a")

# filelist
files = [
    f for f in os.listdir(".")
    if os.path.isfile(f) and f.endswith((".jpg", ".hdr", ".exr"))
]


# down operator
Beispiel #12
0
class BackgroundMediaSyncer(Thread):

    urllibInstance = None

    def __init__(self):
        Thread.__init__(self)
        self.cancel = False
        self.arg = None
        self.messages = ThreadQueue()
        self.messagePump = ePythonMessagePump()

        self.progress = ThreadQueue()
        self.progressPump = ePythonMessagePump()

        self.resolution = getSkinResolution()

        self.running = False

    #===========================================================================
    #
    #===========================================================================
    def getMessagePump(self):
        printl("", self, "S")

        printl("", self, "C")
        return self.messagePump

    #===========================================================================
    #
    #===========================================================================
    def getMessageQueue(self):
        printl("", self, "S")

        printl("", self, "C")
        return self.messages

    #===========================================================================
    #
    #===========================================================================
    def getProgressPump(self):
        printl("", self, "S")

        printl("", self, "C")
        return self.progressPump

    #===========================================================================
    #
    #===========================================================================
    def getProgressQueue(self):
        printl("", self, "S")

        printl("", self, "C")
        return self.progress

    #===========================================================================
    #
    #===========================================================================
    def setMode(self, mode):
        printl("", self, "S")

        self.mode = mode

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def setServerConfig(self, serverConfig):
        printl("", self, "S")

        self.serverConfig = serverConfig

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def setPlexInstance(self, plexInstance):
        printl("", self, "S")

        self.plexInstance = plexInstance

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def Cancel(self):
        printl("", self, "S")

        self.cancel = True

        printl("", self, "C")

    #===========================================================================
    #PROPERTIES
    #===========================================================================
    MessagePump = property(getMessagePump)
    Message = property(getMessageQueue)

    ProgressPump = property(getProgressPump)
    Progress = property(getProgressQueue)

    #===========================================================================
    #
    #===========================================================================
    def startSyncing(self):
        printl("", self, "S")

        # Once a thread object is created, its activity must be started by calling the thread’s start() method.
        # This invokes the run() method in a separate thread of control.
        self.start()
        printl("mode: " + str(self.mode), self, "D")

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def run(self):
        printl("", self, "S")

        if self.mode == "render":
            self.renderBackdrops()

        elif self.mode == "sync":
            self.syncMedia()

        else:
            pass

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def renderBackdrops(self):
        printl("", self, "S")

        self.running = True
        self.cancel = False

        if self.resolution == "FHD":
            msg_text = _(
                "\n\nStarting to search for picture files with 1920x1080 in its name ..."
            )
        else:
            msg_text = _(
                "\n\nStarting to search for picture files with 1280x720 in its name ..."
            )
        self.messages.push((THREAD_WORKING, msg_text))
        self.messagePump.send(0)

        import math
        import glob
        import subprocess

        try:
            from PIL import Image
        except Exception as e:
            printl("Error: " + str(e), self, "D")
            self.messages.push(
                (THREAD_WORKING, _("Error!\nError-message:%s" % e)))
            self.messagePump.send(0)
            return

        #try:
        if self.resolution == "FHD":
            resolutionString = "*1920x1080_v2.jpg"
            searchString = "1920x1080_v2.jpg"
        else:
            resolutionString = "*1280x720_v2.jpg"
            searchString = "1280x720_v2.jpg"

        self.count = len(
            glob.glob1(config.plugins.dreamplex.mediafolderpath.value,
                       resolutionString))

        msg_text = _("\n\nFiles found: ") + str(self.count)
        self.messages.push((THREAD_WORKING, msg_text))
        self.messagePump.send(0)
        from time import sleep
        sleep(0.5)

        if int(self.count) > 0:
            self.currentIndex = 0
            for myFile in glob.glob1(
                    config.plugins.dreamplex.mediafolderpath.value,
                    resolutionString):
                sleep(0.2)
                self.currentIndex += 1
                if self.cancel:
                    break
                try:
                    # firt we check for images in the right size
                    if searchString in myFile:
                        # if we got one we remove the .jpg at the end to check if there was a backdropmovie generated already
                        myFileWoExtension = myFile[:-4]
                        extension = str.upper(myFile[-3:])
                        if extension == "JPG":
                            extension = "JPEG"
                        imageLocationWoExtension = config.plugins.dreamplex.mediafolderpath.value + myFileWoExtension

                        # location string
                        videoLocation = imageLocationWoExtension + ".m1v"
                        # check if backdrop video exists
                        if fileExists(videoLocation):
                            msg_text = _(
                                "backdrop video exists under the location "
                            ) + self.getCounter()
                            self.messages.push((THREAD_WORKING, msg_text))
                            self.messagePump.send(0)
                            continue
                        else:
                            msg_text = _("trying to render backdrop now ...: "
                                         ) + self.getCounter()
                            self.messages.push((THREAD_WORKING, msg_text))
                            self.messagePump.send(0)

                            # now we check if we are are jpeg or png
                            imageLocation = config.plugins.dreamplex.mediafolderpath.value + myFile

                            i = Image.open(imageLocation)

                            data = i.size
                            printl("data: " + str(data), self, "D")

                            myType = i.format
                            printl("myType: " + str(myType), self, "D")

                            if myType == "JPEG":
                                renderCommand = "jpeg2yuv"

                            elif myType == "PNG":
                                renderCommand = "png2yuv"

                            else:
                                msg_text = _(
                                    "skipping because unsupported image type. "
                                ) + self.getCounter()
                                self.messages.push((THREAD_WORKING, msg_text))
                                self.messagePump.send(0)
                                continue

                            xValid, xResult = isValidSize(i.size[0])
                            yValid, yResult = isValidSize(i.size[1])

                            printl("xValid: " + str(xValid), self, "D")
                            printl("yValid: " + str(yValid), self, "D")

                            if not xValid or not yValid:
                                xResult = math.ceil(xResult) * 16
                                yResult = math.ceil(yResult) * 16
                                newSize = (int(xResult), int(yResult))
                                resizedImage = i.resize(newSize)
                                resizedImage.save(imageLocation,
                                                  format=extension)

                            printl("started rendering : " + str(videoLocation),
                                   self, "D")

                            if self.resolution == "FHD":
                                cmd = renderCommand + " -v 0 -f 25 -n1 -I p -j " + imageLocation + " | mpeg2enc -v 0 -f 12 -x 1920 -y 1080 -a 3 -4 1 -2 1 -q 1 -H --level high -o " + videoLocation
                            else:
                                cmd = renderCommand + " -v 0 -f 25 -n1 -I p -j " + imageLocation + " | mpeg2enc -v 0 -f 12 -x 1280 -y 720 -a 3 -4 1 -2 1 -q 1 -H --level high -o " + videoLocation

                            printl("cmd: " + str(cmd), self, "D")

                            if PY2:
                                import commands
                                (status,
                                 response) = commands.getstatusoutput(cmd)
                            else:
                                (status,
                                 response) = subprocess.getstatusoutput(cmd)

                            if fileExists(videoLocation) and status == 0:
                                printl(
                                    "finished rendering myFile: " +
                                    str(myFile), self, "D")
                            else:
                                printl("File does not exist after rendering!",
                                       self, "D")
                                printl("Error: " + str(response), self, "D")

                                self.messages.push(
                                    (THREAD_WORKING,
                                     _("Error: ") + str(response) +
                                     "Location: " + imageLocation))
                                self.messagePump.send(0)

                                sleep(1)

                                self.running = False
                                self.cancel = True
                                break

                except Exception as e:
                    printl("Error: " + str(e), self, "D")
                    self.messages.push(
                        (THREAD_WORKING, _("Error!\nError-message:%s" % e)))
                    self.messagePump.send(0)
        else:
            msg_text = _("\n\nNo Files found. Nothing to do!")
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)

            sleep(1)

        if self.cancel:
            self.messages.push(
                (THREAD_FINISHED, _("Process aborted.\nPress Exit to close.")))
        else:
            self.messages.push((THREAD_FINISHED, _("\n\nWe are done!")))

        self.messagePump.send(0)

        sleep(1)
        self.running = False

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def getCounter(self):
        printl("", self, "S")

        printl("", self, "C")
        return "(" + str(self.currentIndex) + "/" + str(self.count) + ") "

    #===========================================================================
    #
    #===========================================================================
    def prepareMediaVariants(self):
        # get params
        viewTypes = ["movies", "mixed", "shows", "music"]
        self.backdropVariants = []
        self.posterVariants = []

        for viewType in viewTypes:
            views = getViews(viewType)
            for viewParams in views:
                p_height = viewParams[2]["settings"]["posterHeight"]
                p_width = viewParams[2]["settings"]["posterWidth"]
                p_postfix = "_poster_" + p_width + "x" + p_height + "_v2.jpg"
                variant = [p_height, p_width, p_postfix]

                if variant in self.posterVariants:
                    printl("variant already exists", self, "D")
                else:
                    self.posterVariants.append(variant)

                b_height = viewParams[2]["settings"]["backdropHeight"]
                b_width = viewParams[2]["settings"]["backdropWidth"]
                b_postfix = "_backdrop_" + b_width + "x" + b_height + "_v2.jpg"
                variant = [b_height, b_width, b_postfix]

                if variant in self.backdropVariants:
                    printl("variant already exists", self, "D")
                else:
                    self.backdropVariants.append(variant)

        if self.resolution == "FHD":
            l_height = "1080"
            l_width = "1920"
            l_postfix = "_backdrop_1920x1080_v2.jpg"
        else:
            # we use this for fullsize m1v backdrops that can be loaded to miniTv
            l_height = "720"
            l_width = "1280"
            l_postfix = "_backdrop_1280x720_v2.jpg"

        variant = [l_height, l_width, l_postfix]

        if variant in self.backdropVariants:
            printl("variant already exists", self, "D")
        else:
            self.backdropVariants.append(variant)

        printl("posterVariants: " + str(self.posterVariants), self, "D")
        printl("backdropVariants: " + str(self.backdropVariants), self, "D")

    #===========================================================================
    #
    #===========================================================================
    def syncMedia(self):
        printl("", self, "S")

        self.running = True
        self.cancel = False
        msg_text = _("\n\nstarting ...")
        self.messages.push((THREAD_WORKING, msg_text))
        self.messagePump.send(0)

        # get sections from server
        self.sectionList = self.plexInstance.getAllSections()
        self.sectionCount = len(self.sectionList)
        printl("sectionList: " + str(self.sectionList), self, "D")

        # get servername
        self.prefix = self.plexInstance.getServerName().lower()

        # prepare variants
        self.prepareMediaVariants()

        self.movieCount = 0
        self.showCount = 0
        self.seasonCount = 0
        self.episodeCount = 0
        self.artistCount = 0
        self.albumCount = 0

        try:
            msg_text = _(
                "\n\nFetching complete library data. This could take a while ..."
            )
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)

            # in this run we gather only the information
            self.cylceThroughLibrary()

            printl("sectionCount " + str(self.sectionCount), self, "D")
            printl("movieCount " + str(self.movieCount), self, "D")
            printl("showCount  " + str(self.showCount), self, "D")
            printl("seasonCount " + str(self.seasonCount), self, "D")
            printl("episodeCount " + str(self.episodeCount), self, "D")
            printl("artistCount " + str(self.artistCount), self, "D")
            printl("albumCount " + str(self.albumCount), self, "D")

            # this run really fetches the data
            self.cylceThroughLibrary(dryRun=False)

            if self.cancel:
                self.messages.push(
                    (THREAD_FINISHED,
                     _("Process aborted.\nPress Exit to close.")))
            else:
                self.messages.push((THREAD_FINISHED, _("We did it :-)")))

        except Exception as e:
            self.messages.push(
                (THREAD_FINISHED,
                 _("Error!\nError-message:%s\nPress Exit to close." % e)))
        finally:
            self.messagePump.send(0)

        self.running = False

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def cylceThroughLibrary(self, dryRun=True):
        printl("cylceThroughLibrary", self, "S")

        for section in self.sectionList:
            # interupt if needed
            if self.cancel:
                break

            if self.serverConfig.syncMovies.value:
                if section[2] == "movieEntry":
                    printl("movie", self, "D")
                    movieUrl = section[3]["contentUrl"]

                    if "/all" not in movieUrl:
                        movieUrl += "/all"

                    printl("movieUrl: " + str(movieUrl), self, "D")
                    library, mediaContainer = self.plexInstance.getMoviesFromSection(
                        movieUrl)

                    if not dryRun:
                        self.syncThrougMediaLibrary(library, myType="Movie")
                    else:
                        self.movieCount += len(library)

            if self.serverConfig.syncShows.value:
                if section[2] == "showEntry":
                    printl("show: " + str(section))
                    showUrl = section[3]["contentUrl"]\

                    if "/all" not in showUrl:
                        showUrl += "/all"

                    printl("showUrl: " + str(showUrl), self, "D")
                    library, mediaContainer = self.plexInstance.getShowsFromSection(
                        showUrl)

                    if not dryRun:
                        self.syncThrougMediaLibrary(library, myType="Show")
                    else:
                        self.showCount += len(library)

                    for seasons in library:
                        if self.cancel:
                            break
                        printl("seasons: " + str(seasons))

                        seasonsUrl = seasons[1]["server"] + seasons[1]["key"]
                        printl("seasonsUrl: " + str(seasonsUrl), self, "D")
                        library, mediaContainer = self.plexInstance.getSeasonsOfShow(
                            seasonsUrl)

                        if not dryRun:
                            self.syncThrougMediaLibrary(library,
                                                        myType="Season")
                        else:
                            self.seasonCount += len(library)

                        for episodes in library:
                            if self.cancel:
                                break
                            printl("episode: " + str(episodes))

                            episodesUrl = episodes[1]["server"] + episodes[1][
                                "key"]
                            printl("episodesUrl: " + str(episodesUrl), self,
                                   "D")
                            library, mediaContainer = self.plexInstance.getEpisodesOfSeason(
                                episodesUrl)

                            if not dryRun:
                                self.syncThrougMediaLibrary(library,
                                                            myType="Episode")
                            else:
                                self.episodeCount += len(library)

            if self.serverConfig.syncMusic.value:
                if section[2] == "musicEntry":
                    printl("music", self, "D")

                    # first we go through the artists
                    url = section[3]["contentUrl"]\

                    if "/all" not in url:
                        url += "/all"

                    printl("url: " + str(url), self, "D")
                    library, mediaContainer = self.plexInstance.getMusicByArtist(
                        url)

                    if not dryRun:
                        self.syncThrougMediaLibrary(library, myType="Music")
                    else:
                        self.artistCount += len(library)

                    # now we go through the albums
                    url = section[3]["contentUrl"] + "/albums"

                    printl("url: " + str(url), self, "D")
                    library, mediaContainer = self.plexInstance.getMusicByAlbum(
                        url)

                    if not dryRun:
                        self.syncThrougMediaLibrary(library, myType="Albums")
                    else:
                        self.albumCount += len(library)

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def syncThrougMediaLibrary(self, library, myType):
        printl("", self, "S")

        for media in library:
            if self.cancel:
                break
            printl("media: " + str(media), self, "D")
            msg_text = "\n" + str(myType) + " with ratingKey: " + str(
                media[1]["ratingKey"])
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)
            msg_text = _("title: " + encodeThat(media[1]["title"]))
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)

            for variant in self.backdropVariants:
                sleep(0.2)
                # interupt if needed
                if self.cancel:
                    break

                t_height = variant[0]
                t_width = variant[1]
                t_postfix = variant[2]

                # location string
                location = config.plugins.dreamplex.mediafolderpath.value + str(
                    self.prefix) + "_" + str(
                        media[1]["ratingKey"]) + str(t_postfix)

                # check if backdrop exists
                if fileExists(location):
                    msg_text = _("found backdrop - size(" + str(t_width) +
                                 "x" + str(t_height) + ")")
                    self.messages.push((THREAD_WORKING, msg_text))
                    self.messagePump.send(0)
                    continue
                else:
                    if "art" in media[1] and media[1]["art"] != "":
                        msg_text = _(
                            "backdrop not found, trying to download ...")
                        self.messages.push((THREAD_WORKING, msg_text))
                        self.messagePump.send(0)
                        #download backdrop
                        self.downloadMedia(media[1]["art"], location, t_width,
                                           t_height)

            for variant in self.posterVariants:
                # interupt if needed
                if self.cancel:
                    break

                t_height = variant[0]
                t_width = variant[1]
                t_postfix = variant[2]

                # location string
                location = config.plugins.dreamplex.mediafolderpath.value + str(
                    self.prefix) + "_" + str(
                        media[1]["ratingKey"]) + str(t_postfix)

                # check if poster exists
                if fileExists(location):
                    msg_text = _("found poster - size(" + str(t_width) + "x" +
                                 str(t_height) + ")")
                    self.messages.push((THREAD_WORKING, msg_text))
                    self.messagePump.send(0)
                    continue
                else:
                    if "thumb" in media[1] and media[1]["thumb"] != "":
                        msg_text = _(
                            "poster not found, trying to download ...")
                        self.messages.push((THREAD_WORKING, msg_text))
                        self.messagePump.send(0)
                        self.downloadMedia(media[1]["thumb"], location,
                                           t_width, t_height)

            self.decreaseQueueCount(myType=myType)

            msg_text = "Movies: " + str(self.movieCount) + "\n" + "Shows: " + str(self.showCount)\
               + "\n" + "Seasons: " + str(self.seasonCount) + "\n" + "Episodes: " + str(self.episodeCount) \
               + "\n" + "Artists: " + str(self.artistCount) + "\n" + "Albums: " + str(self.albumCount)
            self.progress.push((THREAD_WORKING, msg_text))
            self.progressPump.send(0)

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def decreaseQueueCount(self, myType):
        printl("", self, "S")

        if myType == "Movie":
            self.movieCount -= 1

        elif myType == "Show":
            self.showCount -= 1

        elif myType == "Season":
            self.seasonCount -= 1

        elif myType == "Episode":
            self.episodeCount -= 1

        elif myType == "Music":
            self.artistCount -= 1

        elif myType == "Albums":
            self.albumCount -= 1

        else:
            raise Exception

        printl("", self, "C")

    #===========================================================================
    #
    #===========================================================================
    def downloadMedia(self, download_url, location, width, height):
        printl("", self, "S")

        download_url = download_url.replace(
            '&width=999&height=999', '&width=' + width + '&height=' + height)
        printl("download url " + download_url, self, "D")

        if self.urllibInstance is None:
            server = self.plexInstance.getServerFromURL(download_url)
            self.initUrllibInstance(server)

        printl("starting download", self, "D")
        try:
            self.urllibInstance.retrieve(download_url, location)

            msg_text = _("... success")
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)
        except Exception as e:
            printl("download not possible: " + str(e), self, "D")

            msg_text = _("... failed")
            self.messages.push((THREAD_WORKING, msg_text))
            self.messagePump.send(0)

        printl("", self, "C")
        return True

    #===========================================================================
    #
    #===========================================================================
    def initUrllibInstance(self, server):
        printl("", self, "S")

        # we establish the connection once here
        self.urllibInstance = URLopener()

        # we add headers only in special cases
        connectionType = self.serverConfig.connectionType.value
        localAuth = self.serverConfig.localAuth.value

        if connectionType == "2" or localAuth:
            authHeader = self.plexInstance.get_hTokenForServer(server)
            self.urllibInstance.addheader("X-Plex-Token",
                                          authHeader["X-Plex-Token"])

        printl("", self, "C")
Beispiel #13
0
def downloadAsset(uri, dirname, contentType):
    # if contentType == 'text/javascript':
    #     return
    down = time.time()
    tUrl = uri
    o = urlparse(tUrl)
    targetDir = CURRENT_DIRECTORY + '/' + dirname + '/' + '/'.join(o.path.split('/')[1:-1])

    # javascript, fragment의 경우 다운로드 불필요
    if o.scheme == "javascript" or (o.netloc == '' and o.path == ''):
        return
    global ret_time
    global ret
    ret += 1

    if o.scheme == "":
        if uri.startswith("//"):
            tUrl = f"https:{uri}"
        else:
            tUrl = f"https://{uri}"

    if not uri.startswith('http'):
        if uri.startswith('//'):
            tUrl = f"http:{uri}"
        else:
            tUrl = f"http://{uri}"

    # text/html 무시
    if contentType in mimeTypes[1:]:
        if not os.path.exists(targetDir):
            path = Path(targetDir)
            path.mkdir(parents=True)

        targetFile = targetDir + '/' + o.path.split('/')[-1]
        if not os.path.exists(targetFile):
            try:
                urlretrieve(tUrl, targetFile)
                print(f"[Retrieved] {tUrl}", time.time() - down)
                # print(f"[Retrieved] {targetFile}", time.time() - down)
                ret_time += time.time() - down
            except Exception as e:
                try:
                    print(type(e).__name__ , tUrl)
                    opener = URLopener()
                    opener.addheader('User-Agent', 'Mozilla/5.0')
                    filename, headers = opener.retrieve(tUrl, targetFile)
                    print(f"[Retrieved2] {targetFile}", time.time() - down)
                    ret_time += time.time() - down
                except Exception as e:
                    try:
                        print(type(e).__name__,'헤더 붙여도' , tUrl)
                        tUrl = tUrl.replace('www.', '')
                        tUrl = tUrl.replace('http:', 'https:')
                        opener.retrieve(tUrl, targetFile)
                        print(f"[Retrieved3] {targetFile}", time.time() - down)
                        ret_time += time.time() - down
                    except Exception as e:
                        print(type(e).__name__, 'https:// 에 www 제외', tUrl)
                        if 'bobae' in tUrl : #보배 드림 image 만을 위한 처리 우선은 이렇게 임시방편
                            try :
                                tUrl = tUrl.replace('//', '//image.')
                                opener.retrieve(tUrl, targetFile)
                                print(f"[Retrieved4] 보배드림 image {targetFile}", time.time() - down)
                            except :
                                print(type(e).__name__, 'image 처리도 실패', tUrl)
                                pass
                        return
            finally:
                if contentType == 'text/css':
                    global args
                    parseCSSURLs(targetFile, args.url, dirname)
    else:
        pass