python+django 聚合直播平台
本帖最后由 hubing607 于 2019-7-30 08:49 编辑看个小姐姐还要被各种广告和会员所扰,于是想着自己写一个,做的比较匆忙,演示地址 http://192.144.141.252:8000/ 可以用test/Test123456登陆看效果此为PC版本,需浏览器支持flash或H5播放器,移动端只有部分源可播放
view文件
import datetime
import json
import os
import time
from os import path
import hashlib
import requests
from PIL import Image
# from .models import Home
# Create your views here.
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_http_methods
from django.views.generic import View
# @method_decorator(login_required, name='dispatch')
@method_decorator(login_required, name='dispatch')
class HomeListView(View):
def pingtai(self):
url = 'http://api.hclyz.com:81/mf/json.txt'
response = requests.get(url)
self.pingtai = json.loads(response.text)['pingtai']
return self.pingtai
def get(self, request):
return render(request,'home.html',{'pingtai':self.pingtai})
@method_decorator(login_required, name='dispatch')
class liveRoomView(View):
def render(self, request):
return render(request, 'liveRoom.html',{"room_title":self.title,"anchors":self.liveroom} )
def get(self, request,title):
plates = HomeListView()
rooms = plates.pingtai()
for plate in rooms:
try:
if plate['title'] == title:
self.address = plate['address']
self.title = plate['title']
break
else:
self.address = "获取不到地址"
except:
self.address = "获取不到地址"
zhubo = str("http://api.hclyz.com:81/mf/"+self.address)
response = requests.get(zhubo)
self.liveroom = json.loads(response.text)['zhubo']
return self.render(request)
@method_decorator(login_required, name='dispatch')
class anchor(View):
def render(self, request):
return render(request, 'anchor.html', {'anchor_name':self.anchor_name,'room_title':self.room_title, 'anchor_address':self.anchor_address})
def get(self, request,title,title2):
self.anchor_name = str(title2)
self.room_title = str(title)
plates = HomeListView()
rooms = plates.pingtai()
for plate in rooms:
if plate['title'] == self.room_title:
pingtai = plate['address']
self.title = plate['title']
zhubo = str("http://api.hclyz.com:81/mf/" + pingtai)
response = requests.get(zhubo)
self.liveroom = json.loads(response.text)['zhubo']
break
for zhubo in self.liveroom:
if zhubo['title'] == self.anchor_name:
if getHttp(zhubo['address']) is not None :
self.anchor_address = getHttp(zhubo['address'])
else:
self.anchor_address = zhubo['address']
print(self.anchor_address)
break
else:
self.anchor_address = "获取不到地址"
return self.render(request)
@method_decorator(login_required, name='dispatch')
class anchor_mo(View):
def render(self, request):
return render(request, 'anchor_mo.html', {'anchor_name':self.anchor_name,'room_title':self.room_title, 'anchor_address':self.anchor_address})
def get(self, request,title,title2):
self.anchor_name = str(title2)
self.room_title = str(title)
plates = HomeListView()
rooms = plates.pingtai()
for plate in rooms:
if plate['title'] == self.room_title:
pingtai = plate['address']
self.title = plate['title']
zhubo = str("http://api.hclyz.com:81/mf/" + pingtai)
response = requests.get(zhubo)
self.liveroom = json.loads(response.text)['zhubo']
break
for zhubo in self.liveroom:
if zhubo['title'] == self.anchor_name:
if getHttp(zhubo['address']) is not None :
self.anchor_address = getHttp(zhubo['address'])
else:
self.anchor_address = zhubo['address']
print(self.anchor_address)
break
else:
self.anchor_address = "获取不到地址"
return self.render(request)
def getHttp(url):
Ourl = str(url)
if 't.cn' in Ourl:
try:
apiUrl = 'https://api.weibo.com/2/short_url/expand.json'
apiKey = '1242113336'
request = requests.get('http://api.t.sina.com.cn/short_url/expand.json?source=%s&url_short=%s' % (apiKey, url))
nUrl = json.loads(request.text)['url_long']
return nUrl
except:
return Ourl
else:
Nurl = Ourl
return Nurl
class support(View):
def render(self, request):
return render(request, 'support.html', )
def get(self,request):
return self.render(request)
主页文件
{% extends 'base.html' %}
{% block content %}
<tbody>
<div class="card-body p-3">
<div class="container">
<div class="row">
{% for pi in pingtai %}
<div style="position:inherit;width:200px;height:200px;">
<a href="{% url 'liveRoom' pi.title %}"><img src="{{ pi.xinimg }}" width="100" height="100" alt="{{ pi.title }}"></a>
<div style="position:absolute;width:100px;height:100px;">
{{ pi.title }}
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</tbody>
{% endblock %}
直播平台文件
{% block breadcrumb %}
<li class="breadcrumb-item"><a href="{% url 'main' %}">返回</a></li>
<li class="breadcrumb-item active">{{ room_title }}</li>
{% endblock %}
{% block content %}
<div class="card-body p-3">
<div class="container">
<div class="row">
{% for anchor in anchors%}
<div style="position:inherit;width:200px;height:200px;">
<a href="{% url 'zhubo' room_title anchor.title%}"><img src="{{ anchor.img }}" width="100" height="100"></a>
<div style="position:absolute;width:100px;height:100px;">
<a href="{% url 'zhubo' room_title anchor.title%}">{{anchor.title }}</a>
<a href="{% url 'yidong' room_title anchor.title%}"><small>移动版</small></a>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
主播间文件
{% block breadcrumb %}
<li class="breadcrumb-item"><a href="{% url 'main' %}">返回</a></li>
<li class="breadcrumb-item"><a href="{% url 'liveRoom' room_title %}">{{ room_title }}</a></li>
<li class="breadcrumb-item active">{{ anchor_name }}</li>
{# <li class="breadcrumb-item active">{{ anchor_address }}</li>#}
{% endblock %}
{% block content %}
<div align="center">
<link rel="stylesheet"/>
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.8.0/aliplayer-flash-min.js"></script>
<divclass="prism-player" id="J_prismPlayer"></div>
<script>
var player = new Aliplayer({
id: 'J_prismPlayer',
width: '400px',
height:'600px',
autoplay: true,
isLive:true,
snapshot:true,
//支持播放地址播放,此播放优先级最高
source : '{{ anchor_address }}'
});
</script>
</div>
{% endblock %} 一夜恶补 Django+ html
elliotxin 发表于 2019-3-21 13:03
真厉害,可以分享一下源代码吗?
https://blog.csdn.net/qq_35554125/article/details/79462885
整个架构是按照这个来的 ,替换其中的view文件和页面文件,还有url文件需要对应下即可 django可以学习下:Dweeqw 最近刚好在学习Django,向大佬学习了 强啊,正准备学PY 老哥分享下源码? 强,给力,学习到了 强啊,正准备学PY 感谢楼主分享{:17_1073:} 现在使用python做网页的公司多吗
爬小萌新虫 发表于 2019-3-15 14:30
现在使用python做网页的公司多吗
只能说有一些 百度 新浪 都有业务线在用