Small update
[Python-Scripts.git] / PyXV / xv-dl.py
blobcfc36757561e6d9b3159dc667724b47386767c45
1 #!/usr/bin/python
2 '''
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 Revised BSD License for more details.
11 Copyright 2011-2013 Cool Dude 2k - http://idb.berlios.de/
12 Copyright 2011-2013 Game Maker 2k - http://intdb.sourceforge.net/
13 Copyright 2011-2013 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
15 $FileInfo: xv.py - Last Update: 04/01/2013 Ver. 1.0.0 - Author: cooldude2k $
16 '''
17 from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
18 import re
19 import os
20 import sys
21 import urllib
22 import urllib2
23 import cookielib
24 import StringIO
25 import gzip
26 import time
27 import datetime
28 import argparse
29 import urlparse
30 import pygame
32 parser = argparse.ArgumentParser()
33 parser.add_argument("-name", help="title name")
34 parser.add_argument("file", nargs="*", help="file name")
35 parser.add_argument("--user-agent", nargs="?",
36 default="Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0", help="specify a custom user agent")
37 parser.add_argument("--referer", nargs="?", default="http://motherless.com/",
38 help="specify a custom referer, use if the video access")
39 getargs = parser.parse_args()
40 fakeua = getargs.user_agent
41 geturls_cj = cookielib.CookieJar()
42 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(geturls_cj))
43 geturls_opener.addheaders = [("Referer", getargs.referer), ("User-Agent", fakeua), ("Accept-Encoding", "gzip, deflate"), ("Accept-Language", "en-US,en;q=0.8,en-CA,en-GB;q=0.6"),
44 ("Accept-Charset", "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7"), ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), ("Connection", "close")]
45 if(getargs.file == None or len(getargs.file) == 0):
46 pygame.display.quit()
47 pygame.quit()
48 sys.exit(0)
49 numurlarg = len(getargs.file)
50 cururlarg = 0
51 while(cururlarg < numurlarg):
52 geturls_text = geturls_opener.open(getargs.file[cururlarg])
53 if(geturls_text.info().get("Content-Encoding") == "gzip" or geturls_text.info().get("Content-Encoding") == "deflate"):
54 strbuf = StringIO.StringIO(geturls_text.read())
55 gzstrbuf = gzip.GzipFile(fileobj=strbuf)
56 outbuf = StringIO.StringIO(gzstrbuf.read()[:])
57 if(geturls_text.info().get("Content-Encoding") != "gzip" and geturls_text.info().get("Content-Encoding") != "deflate"):
58 outbuf = StringIO.StringIO(geturls_text.read()[:])
59 pygame.display.init()
60 pyres = (pygame.display.Info().current_h, pygame.display.Info().current_w)
61 ppmimg = pygame.image.load(outbuf)
62 width, height = ppmimg.get_size()
63 if(width > pyres[0] or height > pyres[1]):
64 dest_w = float(pyres[0])
65 dest_h = float(pyres[1])
66 scale = dest_w / width
67 if(height*scale > dest_h):
68 scale = dest_h / height
69 size = (int(width * scale), int(height * scale))
70 width = size[0]
71 height = size[1]
72 ppmimg = pygame.transform.scale(ppmimg, (width, height))
73 screen = pygame.display.set_mode((width, height))
74 if(getargs.name != None):
75 pygame.display.set_caption("PyXV - "+str(getargs.name))
76 if(getargs.name == None):
77 pygame.display.set_caption("PyXV - "+str(getargs.file[cururlarg]))
78 pygame.display.get_active()
79 pygame.mouse.set_visible(0)
80 screen.blit(ppmimg, (0, 0))
81 pygame.display.flip()
82 pygame.display.toggle_fullscreen()
83 done = False
84 while not done:
85 for event in pygame.event.get():
86 if (event.type == pygame.QUIT):
87 done = True
88 if (event.type == pygame.KEYDOWN):
89 if (event.key == pygame.K_t) or (event.key == pygame.K_w):
90 pygame.display.toggle_fullscreen()
91 if (event.key == pygame.K_i) or (event.key == pygame.K_m):
92 pygame.display.iconify()
93 if (event.key == pygame.K_ESCAPE) or (event.key == pygame.K_q):
94 done = True
95 pygame.display.quit()
96 pygame.quit()
97 cururlarg = cururlarg + 1
98 sys.exit(0)