forked from tripod31/get_lyric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_lyric.py
60 lines (48 loc) · 1.88 KB
/
get_lyric.py
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 python3
# -*- coding: utf-8 -*-
'''
Search lyric from the site,and put it to standard output.
'''
import argparse
import logging
import sys,io
# sites classes
from get_lyric.sites import list_scrapers,choose_scrapers
from get_lyric.common import read_config
if __name__ == '__main__':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') #for unicodeerror
parser = argparse.ArgumentParser()
parser.add_argument('--artist')
parser.add_argument('--song')
parser.add_argument('--sites'
,help="name of sites to search,splitted by ','.site names are displayed py 'get_lyric.py --list'")
parser.add_argument('--proxy'
,help="format is [site name=proxy url:port],splitted by ','")
parser.add_argument('--list' ,action='store_true'
,help="print site names and exit")
args=parser.parse_args()
if args.list:
for s in list_scrapers():
print(s.site)
sys.exit(0)
read_config(args)
logging.basicConfig(level=logging.INFO,
stream = open("get_lyric.log",mode="w",encoding="utf-8"))
logging.info("argument:" + str(args))
if args.artist is None or args.song is None:
logging.info("artist and song is required.exit")
sys.exit(0)
scrapers = choose_scrapers(args.sites, args.artist, args.song)
ret = False
for scraper in scrapers:
try:
obj = scraper(args.artist, args.song,args.proxy)
ret=obj.get_lyric()
except Exception as e:
logging.error(obj.log_msg("error:[%s]" % e))
continue
if ret == True:
print(obj.lyric,end="")
break
if ret==False:
logging.info("no lyrics at all sites")