summaryrefslogtreecommitdiff
path: root/urlopen.py
blob: c5b700c140a8a25becaf706acf18737436b5bfa8 (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
#!/usr/bin/env python2.7

# Copyright Daniel Jones 2018
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys, os;
from urlparse import urlparse;
def run(process, arg):
    L = [];
    for each in process:
        L.append(each);
    L.append(arg);
    os.spawnvpe(os.P_NOWAIT, L[0], L, os.environ)

def main(args):
    images = ["jpg", "jpeg", "png"]; imageapp = ["feh"];
    videos = ["gif", "gifv", "webm", "mp4"]; videoapp = ["mpv", "--loop"]; #--yt-dlformat=22
    videourls = ["youtube.com", "youtu.be", "streamable.com"];
    pdf = ["pdf"]; pdfapp = ["mupdf"];
    defaultapp = ["/home/daniel_j/compiled/waterfox/waterfox"];
    defaultforcedurls = ["slack.com"];
    x = 0;
    for each in args:
        parsed_uri = urlparse(each);
        domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri);
        if (any (s in domain for s in defaultforcedurls)):
                print("running with forced default application {}".format(each));
                run(defaultapp, each);
        elif (each.lower().endswith(tuple(images))):
                print("image {}".format(each));
                run(imageapp, each);
        elif (each.lower().endswith(tuple(videos))):
                print("video {}".format(each));
                run(videoapp, each);
        elif (each.lower().endswith(tuple(pdf))):
                print("pdf {}".format(each));
                run(pdfapp, each);
        elif (any(s in domain for s in videourls)):
                print("video {}".format(each));
                run(videoapp, each);
        else:
            print("running with default application {}".format(each));
            run(defaultapp, each);
        x += 1;

if __name__ == "__main__":
    if sys.argv > 1:
        main(sys.argv[1:]);