forked from ma6174/fmpi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ma6174
committed
Feb 23, 2013
0 parents
commit e724c04
Showing
4 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#!/usr/bin/env python | ||
#coding=utf-8 | ||
|
||
import os | ||
import web | ||
import time | ||
import sqlite3 | ||
from threading import Thread | ||
from get_sogou_mp3 import getlink | ||
|
||
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 name_or_url |sudo pifm - %s %s"%(name_or_url,freq,rate) | ||
print cmd | ||
os.system(cmd) | ||
return 0 | ||
def fmpi(self): | ||
while True: | ||
query = DB.getall(self) | ||
try: | ||
one = query[0] | ||
except: | ||
one = None | ||
if one is not None: | ||
# self.play(link) | ||
DB.updateone(self,one[0]) | ||
time.sleep(2) | ||
|
||
class INDEX(DB): | ||
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>正在播放</h1>%s<h2>播放列表</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 | ||
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>歌曲在播放列表中已经存在''' | ||
raise web.seeother('/') | ||
except: | ||
return self.index() | ||
|
||
if __name__=='__main__': | ||
db = DB() | ||
db.create_table() | ||
pi = FMPI() | ||
player = Thread(target=pi.fmpi) | ||
player.setDaemon(True) | ||
player.start() | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python | ||
#coding=utf-8 | ||
import urllib | ||
import urllib2 | ||
import re | ||
def getlink(music_name): | ||
quote_name = urllib.quote(music_name.decode('utf-8').encode('gbk')) | ||
query_url = 'http://mp3.sogou.com/music.so?query=%s&class=1&pf=&w=02009900&st=&ac=1&sourcetype=sugg&_asf=mp3.sogou.com&_ast=1361525645'%quote_name | ||
data = urllib2.urlopen(query_url,timeout=10).read() | ||
re_com = re.compile('onclick="window.open\(\'(.*?)\'') | ||
forward_links = re_com.findall(data) | ||
total = len(forward_links) | ||
if total == 0: | ||
return None | ||
for i in range(total): | ||
next_link = "http://mp3.sogou.com"+forward_links[i] | ||
data2 = urllib2.urlopen(next_link,timeout=10).read() | ||
re_com2 = re.compile('<div class="dl"><a href=\"(.*?)\"') | ||
download_link = re_com2.findall(data2) | ||
if download_link[0][-3:] == 'mp3': | ||
return download_link[0] | ||
return None | ||
|
||
if __name__=='__main__': | ||
print getlink('滴答') |
Binary file not shown.
Oops, something went wrong.