Small update
[Python-Scripts.git] / PyXV / xv-dl.py
blob174d32828118c01f8c7a6cdf294d23abb5e7f8db
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
19 import argparse
20 import datetime
21 import gzip
22 import os
23 import re
24 import sys
25 import time
26 import urllib
28 import cookielib
29 import pygame
30 import StringIO
31 import urllib2
32 import urlparse
34 parser = argparse.ArgumentParser()
35 parser.add_argument("-name", help="title name")
36 parser.add_argument("file", nargs="*", help="file name")
37 parser.add_argument(
38 "--user-agent",
39 nargs="?",
40 default="Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0",
41 help="specify a custom user agent")
42 parser.add_argument("--referer", nargs="?", default="http://motherless.com/",
43 help="specify a custom referer, use if the video access")
44 getargs = parser.parse_args()
45 fakeua = getargs.user_agent
46 geturls_cj = cookielib.CookieJar()
47 geturls_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(geturls_cj))
48 geturls_opener.addheaders = [
49 ("Referer",
50 getargs.referer),
51 ("User-Agent",
52 fakeua),
53 ("Accept-Encoding",
54 "gzip, deflate"),
55 ("Accept-Language",
56 "en-US,en;q=0.8,en-CA,en-GB;q=0.6"),
57 ("Accept-Charset",
58 "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7"),
59 ("Accept",
60 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
61 ("Connection",
62 "close")]
63 if (getargs.file is None or len(getargs.file) == 0):
64 pygame.display.quit()
65 pygame.quit()
66 sys.exit(0)
67 numurlarg = len(getargs.file)
68 cururlarg = 0
69 while (cururlarg < numurlarg):
70 geturls_text = geturls_opener.open(getargs.file[cururlarg])
71 if (geturls_text.info().get("Content-Encoding") ==
72 "gzip" or geturls_text.info().get("Content-Encoding") == "deflate"):
73 strbuf = StringIO.StringIO(geturls_text.read())
74 gzstrbuf = gzip.GzipFile(fileobj=strbuf)
75 outbuf = StringIO.StringIO(gzstrbuf.read()[:])
76 if (geturls_text.info().get("Content-Encoding") !=
77 "gzip" and geturls_text.info().get("Content-Encoding") != "deflate"):
78 outbuf = StringIO.StringIO(geturls_text.read()[:])
79 pygame.display.init()
80 pyres = (pygame.display.Info().current_h, pygame.display.Info().current_w)
81 ppmimg = pygame.image.load(outbuf)
82 width, height = ppmimg.get_size()
83 if (width > pyres[0] or height > pyres[1]):
84 dest_w = float(pyres[0])
85 dest_h = float(pyres[1])
86 scale = dest_w / width
87 if (height * scale > dest_h):
88 scale = dest_h / height
89 size = (int(width * scale), int(height * scale))
90 width = size[0]
91 height = size[1]
92 ppmimg = pygame.transform.scale(ppmimg, (width, height))
93 screen = pygame.display.set_mode((width, height))
94 if (getargs.name is not None):
95 pygame.display.set_caption("PyXV - " + str(getargs.name))
96 if (getargs.name is None):
97 pygame.display.set_caption("PyXV - " + str(getargs.file[cururlarg]))
98 pygame.display.get_active()
99 pygame.mouse.set_visible(0)
100 screen.blit(ppmimg, (0, 0))
101 pygame.display.flip()
102 pygame.display.toggle_fullscreen()
103 done = False
104 while not done:
105 for event in pygame.event.get():
106 if (event.type == pygame.QUIT):
107 done = True
108 if (event.type == pygame.KEYDOWN):
109 if (event.key == pygame.K_t) or (event.key == pygame.K_w):
110 pygame.display.toggle_fullscreen()
111 if (event.key == pygame.K_i) or (event.key == pygame.K_m):
112 pygame.display.iconify()
113 if (event.key == pygame.K_ESCAPE) or (event.key == pygame.K_q):
114 done = True
115 pygame.display.quit()
116 pygame.quit()
117 cururlarg = cururlarg + 1
118 sys.exit(0)