forked from ma6174/fmpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmpi.py
executable file
·87 lines (79 loc) · 2.71 KB
/
fmpi.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# coding=utf-8
import os
import time
import random
import douban
import config
import logging
import subprocess
from db import DB
from get_sogou_mp3 import getlink, get_all_mp3
class FMPI(DB):
'''从播放队列获取歌曲并播放'''
def play(self, name_or_url, freq=98.5, rate=44100):
'''调用外部播放命令'''
# cmd = "mpg123 -m -C -q -s %s | sudo pifm - %s
# %s"%(name_or_url,freq,rate)
cmd1 = "mpg123 -m -C -q -s %s" % name_or_url
cmd2 = "sudo pifm - %s %s" % (freq, rate)
self.print_local_ip()
logging.info(
'''press q to play next songs,\npress Ctrl+c to terminate''')
logging.info("")
self.p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE)
self.p2 = subprocess.Popen(
cmd2,
shell=True,
stdin=self.p1.stdout,
stdout=subprocess.PIPE)
self.p1.wait()
# os.system(cmd)
return 0
def print_local_ip(self):
cmd = "hostname -I"
logging.info("your IP is:")
p = subprocess.Popen(cmd,shell=True)
p.wait()
logging.info("Just open http://Your_IP:8080/ in broswer to add music")
def control(self, key):
'''has problem'''
out, err = self.p1.communicate(key)
return out
def get_random_music(self):
'''获取随机歌曲'''
music_set = file("./music_name.txt").readlines()
total = len(music_set)
rand = random.randint(0, total - 1)
return music_set[rand][:-1]
def get_douban_music(self):
'''获取豆瓣歌曲'''
song = douban.getmusic()
return song['title']
def fmpi(self):
'''循环检测'''
while True:
query = DB.getall(self)
try:
one = query[0]
except:
one = None
os.system("clear")
if one is not None:
logging.info('>>>>%s' % one[1])
if one[1].startswith('http://'): # 直接播放url对应的音乐
if one[1].endswith('.mp3'):
self.play(one[1], config.freq, config.rate)
else:
links = get_all_mp3(one[1])
for i in links:
DB.put(self, i)
else:
url = getlink(one[1].encode('utf-8'))
self.play(url, config.freq, config.rate)
DB.updateone(self, one[0])
else:
# rand_music = self.get_random_music()
douban_music = self.get_douban_music()
DB.put(self, douban_music)
time.sleep(1) # 降低CPU占用率