forked from ma6174/fmpi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfmpi.py
executable file
·149 lines (139 loc) · 4.1 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
#coding=utf-8
import os
import web
import time
import sqlite3
import random
from threading import Thread
from get_sogou_mp3 import getlink
import locale
locale.setlocale(locale.LC_ALL, '')
urls = (
'/',"INDEX",
)
app = web.application(urls,globals())
#web.config.debug = False
class DB:
'''数据库相关操作'''
db_name = "query.db"
def create_table(self):
conn = sqlite3.connect(self.db_name)
cur = conn.cursor()
sql_create = '''create table if not exists music(
id integer primary key autoincrement,
title text,
status text
)'''
cur.execute(sql_create)
conn.commit()
cur.close()
conn.close()
return 0
def put(self,music_name):
conn = sqlite3.connect(self.db_name)
cur = conn.cursor()
sql_insert = 'insert into music(title,status) values ("%s","%s")'%(music_name,"wait")
cur.execute(sql_insert)
conn.commit()
cur.close()
conn.close()
return 0
def getall(self):
conn = sqlite3.connect(self.db_name)
cur = conn.cursor()
sql_select = 'select id,title from music where status = "wait"'
cur.execute(sql_select)
data = cur.fetchall()
cur.close()
conn.close()
return data
def updateone(self,id):
conn = sqlite3.connect(self.db_name)
cur = conn.cursor()
sql_update = 'update music set status = "done" where id = %s'%id
cur.execute(sql_update)
conn.commit()
cur.close()
conn.close()
return 0
class FMPI(DB):
'''从播放队列获取歌曲并播放'''
def play(self,name_or_url,freq=97.5,rate=44100):
'''调用外部播放命令'''
cmd = "mpg123 -m -C -q -s %s |sudo pifm - %s %s"%(name_or_url,freq,rate)
print cmd
os.system(cmd)
return 0
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 fmpi(self):
'''循环检测'''
while True:
query = DB.getall(self)
try:
one = query[0]
except:
one = None
if one is not None:
url = getlink(one[1].encode('utf-8'))
self.play(url)
DB.updateone(self,one[0])
else:
rand_music = self.get_random_music()
DB.put(self,rand_music)
time.sleep(1)#降低CPU占用率
class INDEX(DB):
'''web页面相关'''
def index(self):
html = '''<head><meta charset="UTF-8"></head>
<html>
<form action="/" method="GET">
I want to listen:
<input type="TEXT" name="m" />
<input type="submit" value="submit" />
</from>
'''
query = DB.getall(self)
try:
html += u"<h2>Playing</h1>%s<h2>list</h1>"%query[0][1]
num = 1
for i in query[1:]:
html = html + '<li>%d----%s</li>'%(num,i[1])
num+=1
return html
except:
return html
def check_name_exist(self,name):
'''检查歌曲名字是否存在'''
all = DB.getall(self)
for i in all:
print i[1]
if i[1] == name:
return True
return False
def GET(self,args=None):
input = web.input()
try:
music_name = input['m']
print music_name
except:
print "no input"
return self.index()
if self.check_name_exist(music_name) is False:
DB.put(self,music_name)
raise web.seeother('/')
else:
return '''<head><meta charset="UTF-8"></head>
<h1>music already exists'''
if __name__=='__main__':
db = DB()
db.create_table()
pi = FMPI()
player = Thread(target=pi.fmpi) #播放线程在后台
player.setDaemon(True) #随主线程一块退出
player.start()
app.run() #启动web服务器