Skip to content

Commit

Permalink
Updated rds_config_db.cpython-37.pycf_creator.py
Browse files Browse the repository at this point in the history
.
  • Loading branch information
upggr committed Jun 2, 2019
1 parent fca79e8 commit 91a6b57
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
Binary file modified __pycache__/rds_config_db.cpython-37.pyc
Binary file not shown.
65 changes: 49 additions & 16 deletions f_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
import logging
import boto3
import os
import lxml
from lxml import etree

mydb = pymysql.connect(rds_config_db.host,rds_config_db.user,rds_config_db.passwd,rds_config_db.database)
bucket_name = rds_config_db.bucket_name

aws_endpoint = rds_config_db.aws_endpoint
s3_bucket_location = rds_config_db.s3_bucket_location
s3_access_key = rds_config_db.s3_access_key
s3_secret_key = rds_config_db.s3_secret_key

s3_r = boto3.resource('s3')
s3_c = boto3.client('s3')


#s3 = boto3.client('s3')

#conn = tinys3.Connection(s3_access_key,s3_secret_key,tls=True,endpoint=aws_endpoint)
s3_img_location = "https://"+bucket_name+".s3."+s3_bucket_location+".amazonaws.com/img/"

def db_get_countries():
mycursor = mydb.cursor()
Expand All @@ -39,19 +38,22 @@ def db_get_channels():

def db_get_cats():
mycursor = mydb.cursor()
sql = "select cat_name from v_cats"
sql = "select cat_name,cat_img_sd,cat_img_hd from v_cats"
mycursor.execute(sql)
rows = mycursor.fetchall()
return (rows)

def upload_to_s3(temp_path,fname):
def upload_to_s3(temp_path,fname,the_type):
fpath_orig = temp_path+"/"+fname
filename = fname
s3_c.upload_file(fpath_orig, bucket_name, filename)
if the_type == 'json':
s3_c.upload_file(fpath_orig, bucket_name, filename,ExtraArgs={'ContentType': "application/json"})
else:
s3_c.upload_file(fpath_orig, bucket_name, filename)
object_acl = s3_r.ObjectAcl(bucket_name,filename)
response = object_acl.put(ACL='public-read')

def db_get_channels_by_country(country,cat):
def db_get_channels_by_country_and_cat(country,cat):

try:
curs = mydb.cursor()
Expand All @@ -63,19 +65,41 @@ def db_get_channels_by_country(country,cat):
print(e)


def db_get_channels_by_country(country):

try:
curs = mydb.cursor()
sql = 'select v_streams.stream_title,stream_cats.cat_name,stream_images.image_logo,stream_log.stream_path,stream_log.stream_type,sum(stream_log.stream_status) AS status,stream_log.stream_status_country AS country from v_streams left join stream_log on stream_log.stream_title = v_streams.stream_title left join stream_images on stream_images.stream_name = v_streams.stream_title left join stream_cats on stream_cats.stream_name = v_streams.stream_title where stream_log.stream_status_timestamp > NOW()-INTERVAL 5 HOUR and stream_log.stream_status_country = %s group by stream_log.stream_title,stream_log.stream_status_country'
curs.execute(sql,(country))
rows = curs.fetchall()
return (rows)
except Exception as e:
print(e)


def start_creation(t1,t2):
countries = db_get_countries()
cats = db_get_cats()
for country in countries:
fname_basic_xml = "greektv-active-"+country[0]+".xml"
fname_roku_category_leaf_xml = "roku_category_leaf_"+country[0]+".xml"
fname_roku_dp = "roku_dp_active_"+country[0]+".json"



# basic xml feed

data_basic_xml = {}
data_basic_xml["providerName"] = "Greek TV xml feed"
# channels = db_get_channels_by_country(country[0])

# basic xml feed

# roku category leaf xml
data_el = etree.Element('categories')
category_el = etree.SubElement(data_el, "category", title = 'Greek Broadcasting', sd_img = "test", hd_img = "test")
for cat in cats:
category_leaf_el = etree.SubElement(category_el, "categoryLeaf", title = cat[0], description='test', feed="test")
data_roku_category_leaf = etree.tostring(data_el, encoding='utf-8', xml_declaration = True, pretty_print = True)
# roku category leaf xml

# roku direct publisher feed
data_roku_dp = {}
Expand All @@ -85,18 +109,17 @@ def start_creation(t1,t2):
data_roku_dp["tvSpecials"] = []

for cat in cats:
channels = db_get_channels_by_country(country[0],cat[0])
channels = db_get_channels_by_country_and_cat(country[0],cat[0])
for channel in channels:
channel_title = str(channel[0])
channel_cat = channel[1]
channel_img = channel[2]
channel_img = s3_img_location+channel[2]
channel_path = channel[3]
channel_type = channel[4]
channel_active = int(channel[5])
channel_country = channel[6]
channel_cat = cat[0]

# roku direct publisher feed
if channel_type == "hls" and channel_active > 0:
data_roku_dp_content = []
data_roku_dp_videos = []
Expand Down Expand Up @@ -125,13 +148,23 @@ def start_creation(t1,t2):
})
# roku direct publisher feed

# basic xml feed



with open("output/"+fname_basic_xml, "w") as outfile:
json.dump(data_basic_xml, outfile)
upload_to_s3('output',fname_basic_xml)
upload_to_s3('output',fname_basic_xml,'xml')
os.remove('output'+"/"+fname_basic_xml)

with open("output/"+fname_roku_category_leaf_xml, "w") as outfile:
outfile.write(str(data_roku_category_leaf))
print (data_roku_category_leaf.decode('utf-8'))
# upload_to_s3('output',fname_roku_category_leaf,'xml')
os.remove('output'+"/"+fname_roku_category_leaf_xml)


with open("output/"+fname_roku_dp, "w") as outfile:
json.dump(data_roku_dp, outfile)
upload_to_s3('output/',fname_roku_dp)
upload_to_s3('output/',fname_roku_dp,'json')
os.remove("output/"+"/"+fname_roku_dp)

0 comments on commit 91a6b57

Please sign in to comment.