Guest User

Untitled

a guest
Apr 1st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.67 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import bs4, requests, urllib, glob, base64
  4. from random import randint
  5. from google import google
  6. import sys, re, os
  7. reload(sys)
  8. sys.setdefaultencoding("utf-8")
  9. import urllib
  10. import ftplib
  11. from ftplib import FTP
  12. import paramiko
  13. import hashlib
  14.  
  15. import MySQLdb
  16. from pydrive.auth import GoogleAuth
  17. from pydrive.drive import GoogleDrive
  18.  
  19. from datetime import datetime
  20. from bs4 import BeautifulSoup
  21. import urllib2, json, cookielib
  22.  
  23.  
  24. def get_soup(url,header):
  25.     return BeautifulSoup(urllib2.urlopen(urllib2.Request(url,headers=header)),'html.parser')
  26.  
  27.  
  28. def get_images(query):
  29.     image_type="ActiOn"
  30.     query= query.split()
  31.     query='+'.join(query)
  32.  
  33.     url = "https://www.google.com/search?q=" + query + "&as_st=y&tbs=islt:qsvga,isz:ex,iszw:130,iszh:130&tbm=isch&source=lnt"
  34.     print 'Search URL is: ' + url
  35.     #add the directory for your image here
  36.     DIR="img"
  37.     header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
  38.     }
  39.     soup = get_soup(url,header)
  40.  
  41.  
  42.     ActualImages=[]# contains the link for Large original images, type of  image
  43.     for a in soup.find_all("div",{"class":"rg_meta"}):
  44.         link , Type =json.loads(a.text)["ou"]  ,json.loads(a.text)["ity"]
  45.         ActualImages.append((link,Type))
  46.  
  47.     print  "There are total" , len(ActualImages),"images"
  48.  
  49.     if not os.path.exists(DIR):
  50.                 os.mkdir(DIR)
  51.  
  52.     if not os.path.exists(DIR):
  53.                 os.mkdir(DIR)
  54.     ###print images
  55.     for i , (img , Type) in enumerate( ActualImages):
  56.         try:
  57.             req = urllib2.Request(img, headers={'User-Agent' : header})
  58.             raw_img = urllib2.urlopen(req).read()
  59.  
  60.             cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
  61.             print cntr
  62.             if len(Type)==0:
  63.                 f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+".jpg"), 'wb')
  64.             else :
  65.                 f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+"."+Type), 'wb')
  66.  
  67.  
  68.             f.write(raw_img)
  69.             f.close()
  70.         except Exception as e:
  71.             print "could not load : "+img
  72.             print e
  73.  
  74. def get_pages(url):
  75.     text = requests.get(url).text
  76.     soup = bs4.BeautifulSoup(text, 'html.parser')
  77.  
  78.     #aa = soup.find_all('a')
  79.     aa = [d for d in soup.find_all('a') if d.has_attr('data-visibility-tracking') and d.has_attr('aria-label')]
  80.  
  81.     link_list = []
  82.     for a in xrange(len(aa)-1):
  83.         # get page no
  84.         link_list.append('https://www.youtube.com' + aa[a]['href'])
  85.    
  86.     return link_list
  87.  
  88. def get_wo(jumper):
  89.     searchTerm = sys.argv[1].replace(' ', '+')
  90.     url = 'https://www.youtube.com/results?search_query='+searchTerm
  91.  
  92.     link_list = [url,] + get_pages(url)
  93.  
  94.     for i in xrange(jumper):
  95.         url = link_list[-1]
  96.         link_list = link_list + get_pages(url)
  97.    
  98.     #return link_list
  99.     return list(set(link_list))
  100.            
  101.  
  102. def get_video_url(page_url):
  103.     text = requests.get(page_url).text
  104.     soup = bs4.BeautifulSoup(text, 'html.parser')
  105.  
  106.     div = [d for d in soup.find_all('div') if d.has_attr('class') and 'yt-lockup-dismissable' in d['class']]
  107.     video_urls = []
  108.     for d in div:
  109.         aa = [x for x in d.find_all('a') if x.has_attr('title')]
  110.         for a in aa:
  111.             video_urls.append(['https://www.youtube.com' + a['href'], a['title']])
  112.    
  113.     return video_urls
  114.  
  115. # return a download link to mp3 file
  116. def ftp_ssh_mp3(mp3_file, ARTIST_SLUG):
  117.     host = '173.208.200.58'
  118.     ARTIST_SLUG = ARTIST_SLUG.replace(' ','').replace(' ','').replace('\r','')
  119.     # make an ssh connection
  120.     ssh = paramiko.SSHClient()
  121.     ssh.load_system_host_keys()
  122.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  123.     ssh.connect(host, username='customer', password='anhyeuem12AB')
  124.  
  125.     #
  126.     m = hashlib.md5()
  127.     m.update(mp3_file)
  128.     old_mp3_file = mp3_file
  129.     mp3_file = m.hexdigest() + '.mp3'    
  130.     # check if ex_dir existed, if not create it
  131.     ex_dir = '/var/www/html/mp3/' + ARTIST_SLUG
  132.     ex_dir = ex_dir.replace('\r', '')
  133.  
  134.     remote_mp3_file = ('').join([ex_dir,'/',mp3_file])
  135.     print 'remote_mp3_file: ' + remote_mp3_file
  136.     url = 'http://'+host+'/'+'mp3/'+ ARTIST_SLUG
  137.     print 'Check response code of : ' + url
  138.  
  139.     ret = requests.get(url).status_code
  140.  
  141.     if ret != 200:        
  142.         print 'Directory not existed: ' + ex_dir
  143.        
  144.         command = 'mkdir "' + ex_dir + '"'
  145.         print 'Executing commmand on Server: ' +  command  
  146.         ssh.exec_command(command)
  147.     else:
  148.         print 'Directory existed'
  149.    
  150.     # check if mp3_file existed, if not upload it.
  151.    
  152.     url = 'http://' + host + '/mp3/' + ARTIST_SLUG + '/' + mp3_file
  153.  
  154.     print 'Check if mp3 file existed: ' + remote_mp3_file
  155.     print 'Check response code of : ' + url
  156.  
  157.     ret = requests.head(url).status_code
  158.  
  159.     if ret != 200:    
  160.         print 'mp3 file not exsited yet'
  161.         print 'FTP to server & uploading file ...'
  162.         session = ftplib.FTP(host,'customer','anhyeuem12AB')
  163.         session.cwd(ex_dir)
  164.         file = open(old_mp3_file,'rb')                  # file to send
  165.         session.storbinary('STOR ' + mp3_file, file)     # send the file
  166.         file.close()                                    # close file and FTP
  167.         session.quit()
  168.  
  169.         command = 'chmod +r "' + remote_mp3_file + '"'
  170.         print 'Executing commmand on Server: ' +  command
  171.         ssh.exec_command(command)
  172.     else:
  173.         print 'mp3 file existed.'
  174.    
  175.     return 'http://173.208.200.58/mp3/' + ARTIST_SLUG + '/' + urllib.quote_plus(mp3_file)
  176.  
  177. def image_upload(media_file):
  178.     print 'Try uploading ' + media_file
  179.    
  180.     if media_file.endswith('.png'):
  181.         headers = {
  182.             'Authorization' : 'Basic ' +  base64.b64encode( 'van7hu' + ':' + 'bongda.com.vn22AB' ),
  183.             "Content-Disposition" : "attachment;filename=\"" + media_file + "\"",
  184.             "Content-Type" : "image/png"
  185.         }
  186.     elif media_file.endswith('.jpg'):
  187.         headers = {
  188.             'Authorization' : 'Basic ' +  base64.b64encode( 'van7hu' + ':' + 'bongda.com.vn22AB' ),
  189.             "Content-Disposition" : "attachment;filename=\"" + media_file + "\"",
  190.             "Content-Type" : "image/jpg"
  191.         }
  192.     elif media_file.endswith('.jpeg'):
  193.         headers = {
  194.             'Authorization' : 'Basic ' +  base64.b64encode( 'van7hu' + ':' + 'bongda.com.vn22AB' ),
  195.             "Content-Disposition" : "attachment;filename=\"" + media_file + "\"",
  196.             "Content-Type" : "image/jpg"
  197.         }
  198.  
  199.     else:
  200.         print 'Not supported media type, exit: ' + media_file
  201.         sys.exit(1)
  202.        
  203.     endpoint = 'http://apkapp.info/wp-json/wp/v2/media/'    
  204.     data = open(media_file, 'rb').read();
  205.  
  206.     res = requests.post(endpoint, data = data, headers = headers)
  207.    
  208.     res_lst = res.text.split(' ')
  209.     res_lst = res.text.split(',')
  210.    
  211.     media_id = res_lst[0].replace('\r\n{"id":', '')
  212.     media_link = res_lst[3].replace('"guid":{"rendered":"', '').replace('"', '').replace('\\', '')
  213.    
  214.     return [int(media_id.replace('{"id":','')), str(media_link)]
  215.  
  216. def get_meta_info():
  217.     with open('meta_info.txt', 'r') as f:
  218.         lines = f.readlines()
  219.    
  220.     for i in xrange(len(lines)):
  221.         lines[i] = lines[i].replace('\n', '')
  222.    
  223.     return lines
  224.  
  225. # as webm and mp3
  226. def download_youtube_mp3(ytb_link):
  227.     command = 'youtube-dl -x --audio-format mp3 --audio-quality 0 ' + ytb_link
  228.     print 'Executing command: ' + command
  229.     os.system(command)
  230.  
  231. def download_youtube_mp4(ytb_link):
  232.     command = 'youtube-dl --recode-video mp4 -k --postprocessor-args "-strict experimental" ' + ytb_link
  233.     print 'Executing command: ' + command
  234.     os.system(command)
  235.  
  236. def remove_downloaded_youtube():
  237.     command = 'rm -rf *.webm *.mp3 *.mp4 *.mkv'
  238.     print 'Executing command: ' + command
  239.     os.system(command)
  240.  
  241. def prepare_post_content_mp3(ARTIST_SLUG, ARTIST_NAME, SONG_NAME, THUMB_URL, mp3_url):
  242.     post_sample_content = """Artist: <font size='3'><a href='http://apkapp.info/?s=ARTIST_SLUG'>ARTIST_NAME</a></font>
  243. <table border=0 id='pointer'>
  244. <tr>
  245. <td onclick='window.location.href="AD_URL1"' id='width20'>
  246. <a href="AD_URL1"><img src="AD_IMG1" id="img_ad"><span id='ad_text_size'>AD_TITLE1</span></a>
  247. </td>
  248. <td onclick='window.location.href="AD_URL2"' id='width20'>
  249. <a href="AD_URL2"><img src="AD_IMG2" id="img_ad"><span id='ad_text_size'>AD_TITLE2</span></a>
  250. </td>
  251. <td onclick='window.location.href="AD_URL3"' id='width20'>
  252. <a href="AD_URL3"><img src="AD_IMG3" id="img_ad"><span id='ad_text_size'>AD_TITLE3</span></a>
  253. </td>
  254. <td onclick='window.location.href="AD_URL4"' id='width20'>
  255. <a href="AD_URL4"><img src="AD_IMG4" id="img_ad"><span id='ad_text_size'>AD_TITLE4</span></a>
  256. </td>
  257.  
  258. </tr>
  259. </table>
  260. <button class='xemsaox' onclick="location.href='mp3_url'">Get mp3</button>
  261. [zoomsounds_player source='mp3_url' config='skinwavewithcomments' playerid='118800' waveformbg='https://lh3.googleusercontent.com/OCkCqtmYpqevOPlhNY4R8oy37CmypYXtsM6CdwstJp-2X8y4O_MdmnOOyTZ2dODVq7sfxLqoRG2H-fGJ8GAwYDp7jtiyyesUiMjIZA4czV7dDqnaw0qhpkRBpfSmqW_uOkQtGvhJUn9nYAK2MQwQ_PtCfl4uHgb1cae5n7qNC8DjRgVorBBr_gZVLg0IZFXbLW0UTp-8KsqrZSyGHAgxbh7Q40-CKFvBKxZ7KblCTfwsEun4LElkYFe5ZPZOsn1EBrxsbXrSyAZVmm0VX7UXRnEQR-5YTIzZ6ttugwYonTFNwmiGxOCsg5RyYpwTNWMLE1v2fBUsBgSStiLrnwQqrK4VAfV-irLXdfXsy6ZG174u0uPdjGJq3qw3PcJUHatmxZDC5PbSrxTHR-K6OqTOV7bM641t40ZVNZfZmjOTzzL-eDWkKCUu5q5VBm254sJ4FK63bP5QbxOQem6nPadxEayRSKfyF4z4HUnoqsR1giPk8eWI63LcgGOZeSWGVw0T27N_Ugwz37Twr5Ilyk7q66elCiyOxK7IUuiur6-QYi0=w1170-h140-no' waveformprog='https://lh3.googleusercontent.com/3ZCeepH9HAhs1ojwrMVKRW4poGaqPSbeczAAs8XjBl8E4zh0vSzXY4ou7KtRXUoMDff70qz8vEa5YLwq_4kp4ufRHcTK8_7lbs5Ux4jTETAkhluI75nUweiBYztNkwtxRggzTLnu2kdyVn3lubZGDbe4-pxyvBtz2tWauKs9fw7wiMCcrkFz5BFi_X1q7ViGA205qTfuTLjltWzom09Xm8vgt5EsTHyInFoMAeSobImMrG5j67VTgrX_9vYDNu3RE_TbISRY9c7wdEXOplQZXJDHH3c86rdVaoclhGAbli3mHJ92iZmGrZM1JH0glyj-ymSSq8RU1Tw2Slb1QFYEwzJpr_wOR9BqqccLAf-yLawNG5TqTQLhrYekNfPaWEtUrcYvHMDeg2R_x7zZg0Q_FI4qvUjBrTu8ClZIf_fml4mer7KEl3uhNEDNr7pe9suucRGO_f_whT8bqjFsRCvh9obFhvj0Suvc-SNFTeLavV6EwIqFVYdHCwyedHxdmOGTsruvXw3CRqon0UFb2jqR2GO6ZUSQ9k9emXdGCZAVzqY=w1170-h140-no' thumb='THUMB_URL' autoplay='on' cue='on' enable_likes='on' enable_views='on' songname="SONG_NAME" artistname="ARTIST_NAME"]"""
  262.    
  263.     post_sample_content = post_sample_content.replace('ARTIST_SLUG', ARTIST_SLUG)
  264.     post_sample_content = post_sample_content.replace('ARTIST_NAME', ARTIST_NAME)
  265.     post_sample_content = post_sample_content.replace('mp3_url',mp3_url)
  266.     post_sample_content = post_sample_content.replace('SONG_NAME', SONG_NAME)
  267.     post_sample_content = post_sample_content.replace('THUMB_URL', THUMB_URL)
  268.    
  269.     return post_sample_content
  270.  
  271.  
  272. def prepare_post_content_mp4(ARTIST_SLUG, ARTIST_NAME, SONG_NAME, THUMB_URL, GOOGLE_DRIVE_LINK_ID, LINK_DESKTOP, LINK_MOBILE):
  273.     post_sample_content = """Artist: <font size='3'><a href='http://apkapp.info/?s=ARTIST_SLUG'>ARTIST_NAME</font></a>
  274. <table border=0 id='pointer'>
  275. <tr>
  276. <td onclick='window.location.href="AD_URL1"' id='width20'>
  277. <a href="AD_URL1"><img src="AD_IMG1" id="img_ad"><span id='ad_text_size'>AD_TITLE1</span></a>
  278. </td>
  279. <td onclick='window.location.href="AD_URL2"' id='width20'>
  280. <a href="AD_URL2"><img src="AD_IMG2" id="img_ad"><span id='ad_text_size'>AD_TITLE2</span></a>
  281. </td>
  282. <td onclick='window.location.href="AD_URL3"' id='width20'>
  283. <a href="AD_URL3"><img src="AD_IMG3" id="img_ad"><span id='ad_text_size'>AD_TITLE3</span></a>
  284. </td>
  285. <td onclick='window.location.href="AD_URL4"' id='width20'>
  286. <a href="AD_URL4"><img src="AD_IMG4" id="img_ad"><span id='ad_text_size'>AD_TITLE4</span></a>
  287. </td>
  288.  
  289. </tr>
  290. </table>
  291. <button class='xemsaox' onclick="location.href='GOOGLE_DRIVE_LINK_ID'">Get mp4</button>
  292. <div id='no-mobile'>[fvplayer autoplay="true" src="LINK_DESKTOP" controlbar="yes" ad='<div class="xss"><a href="AD_URL"><img src="AD_IMG" id="feature_img_ad"><br/><span id="ad_text_size">AD_TITLE</span></a></div>' width='100%']</div>
  293. <div id='no-desktop'>
  294. [videojs_video url="LINK_MOBILE"]
  295. </div>
  296. """
  297.  
  298.     post_sample_content = post_sample_content.replace('ARTIST_SLUG', ARTIST_SLUG)
  299.     post_sample_content = post_sample_content.replace('ARTIST_NAME', ARTIST_NAME)
  300.     post_sample_content = post_sample_content.replace('GOOGLE_DRIVE_LINK_ID',GOOGLE_DRIVE_LINK_ID)
  301.     post_sample_content = post_sample_content.replace('SONG_NAME', SONG_NAME)
  302.     post_sample_content = post_sample_content.replace('THUMB_URL', THUMB_URL)
  303.     post_sample_content = post_sample_content.replace('LINK_DESKTOP', LINK_DESKTOP)
  304.     post_sample_content = post_sample_content.replace('LINK_MOBILE', LINK_MOBILE)
  305.     post_sample_content = post_sample_content.replace('azlyrics.com', 'apkapp.info')
  306.    
  307.     return post_sample_content
  308.  
  309.  
  310. def prepare_post_excerpt(ARTIST_SLUG, ARTIST_NAME, mp3_url):
  311.     excerpt = """<a href='http://apkapp.info/?s=ARTIST_SLUG'>ARTIST_NAME</a><br/>
  312. <button class='xemsaox' onclick="location.href='mp3_url'">Get mp3</button>"""
  313.    
  314.     excerpt = excerpt.replace('ARTIST_SLUG', ARTIST_SLUG)
  315.     excerpt = excerpt.replace('ARTIST_NAME', ARTIST_NAME)
  316.     excerpt = excerpt.replace('mp3_url',mp3_url)
  317.    
  318.     return excerpt
  319.  
  320. def prepare_post_excerpt_mp4(ARTIST_SLUG, ARTIST_NAME, mp3_url):
  321.     excerpt = """<a href='http://apkapp.info/?s=ARTIST_SLUG'>ARTIST_NAME</a><br/>
  322. <button class='xemsaox' onclick="location.href='mp3_url'">Get mp4</button>"""
  323.    
  324.     excerpt = excerpt.replace('ARTIST_SLUG', ARTIST_SLUG)
  325.     excerpt = excerpt.replace('ARTIST_NAME', ARTIST_NAME)
  326.     excerpt = excerpt.replace('mp3_url',mp3_url)
  327.    
  328.     return excerpt
  329.  
  330. # this function used inside function do_the_work(drive)
  331. def gd_check_exist(drive, file_name):
  332.     print 'Checking existence for file: ' + file_name
  333.     # Auto-iterate through all files in the root folder.
  334.     file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
  335.     for file1 in file_list:
  336.         if file1['title'] == file_name:
  337.             return True
  338.    
  339.     return False
  340.  
  341. def gd_get_dlink(drive, file_name):
  342.     print 'Checking existence for file: ' + file_name
  343.     # Auto-iterate through all files in the root folder.
  344.     file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
  345.     for file1 in file_list:
  346.         if file1['title'] == file_name:
  347.             return file1['alternateLink']
  348.    
  349.     return False
  350.  
  351. def upload_gd(media_file, drive):
  352.     print 'Try uploading ' + media_file
  353.  
  354.     xfile = drive.CreateFile()
  355.  
  356.     xfile.SetContentFile(media_file)
  357.     xfile.Upload()
  358.     print('Created file %s with mimeType %s' % (xfile['title'], xfile['mimeType']))
  359.  
  360.     permission = xfile.InsertPermission({
  361.         'type': 'anyone',
  362.         'value': 'anyone',
  363.         'withLink': True, # do not allow discovery on Internet
  364.         'role': 'reader'})
  365.  
  366.     print 'Sharable link (to view) is: ' + xfile['alternateLink']
  367.     return  xfile['alternateLink']
  368.  
  369. def insert_dlink(dlink):
  370.     print 'Connect to database'
  371.     # Open database connection
  372.     db = MySQLdb.connect("103.3.60.141","root","anhyeuem12AB","links" )
  373.  
  374.     # prepare a cursor object using cursor() method
  375.     cursor = db.cursor()
  376.    
  377.     command = "insert into link(dlink) values('" + dlink + "')"
  378.     print 'Executing SQL command: ' + command
  379.     try:
  380.         cursor.execute(command)
  381.         db.commit()
  382.     except:
  383.         db.rollback()
  384.     # execute SQL query using execute() method.
  385.     cursor.execute("SELECT id FROM link ORDER BY id DESC LIMIT 1")
  386.  
  387.     # Fetch a single row using fetchone() method.
  388.     data = cursor.fetchone()
  389.     # disconnect from server
  390.     db.close()
  391.    
  392.     return int(data[0])
  393.  
  394. def getid_from_dlink(dlink):
  395.     print 'Connect to database'
  396.     # Open database connection
  397.     db = MySQLdb.connect("103.3.60.141","root","anhyeuem12AB","links" )
  398.  
  399.     # prepare a cursor object using cursor() method
  400.     cursor = db.cursor()
  401.    
  402.     # select id from link where dlink = 'dlink'
  403.     command = "select id from link where dlink = '" + dlink + "'"
  404.     print 'Executing SQL command: ' + command
  405.     try:
  406.         cursor.execute(command)
  407.         db.commit()
  408.     except:
  409.         db.rollback()
  410.     # execute SQL query using execute() method.
  411.     cursor.execute("SELECT id FROM link ORDER BY id DESC LIMIT 1")
  412.  
  413.     # Fetch a single row using fetchone() method.
  414.     data = cursor.fetchone()
  415.     # disconnect from server
  416.     db.close()
  417.    
  418.     return int(data[0])
  419.    
  420.  
  421. def post_to_server(title, content, excerpt, feature_img_id, categories):
  422.     headers = {
  423.         "Authorization" : "Basic " +  base64.b64encode( "van7hu" + ":" + "bongda.com.vn22AB" ),
  424.     }
  425.  
  426.     endpoint = 'http://apkapp.info/wp-json/wp/v2/posts'
  427.     tags = [198,]
  428.  
  429.     data = {
  430.         'title' : title,
  431.         'status': 'publish', # publish
  432.         'content': content,
  433.         'excerpt': excerpt,
  434.         'featured_media': feature_img_id,
  435.         'categories': categories,
  436.         'tags': tags,
  437.     }
  438.  
  439.     res = requests.post(endpoint, data = data, headers = headers)
  440.    
  441.     # get the post link
  442.     res_content = res.text.split(',')
  443.     res_content = filter(None, res_content)
  444.  
  445.     return res.status_code
  446.  
  447. def search(term):
  448.     term = term + " site:http://www.azlyrics.com/"
  449.    
  450.     print 'Search google for: ' + term
  451.     search_results = google.search(term, 1)
  452.  
  453.     if len(search_results) !=0:
  454.         return search_results[0].link
  455.    
  456.     # no lyrics, return 0 (not list)
  457.  
  458. def get_lyrics(link):
  459.     headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
  460.     r = requests.get(link, headers=headers)
  461.     data = r.text
  462.  
  463.     data = data.split(" ")
  464.    
  465.     idx_start = 0
  466.     idx_stop = 0
  467.     tmp = 0
  468.     for tmp in xrange(len(data)):
  469.         if 'ringtone' in  data[tmp]:
  470.             idx_start = tmp
  471.             break
  472.    
  473.     adx = 0
  474.     for tmp in xrange(idx_start, len(data)):
  475.         if '</div>' in data[tmp]:
  476.             if adx == 0:
  477.                 idx_start = tmp+1
  478.                 adx = 1
  479.             else:
  480.                 idx_stop = tmp
  481.                 break
  482.    
  483.     return ' '.join(data[idx_start: idx_stop])
  484.  
  485. def list_all_files(xdir):
  486.     cwd = os.getcwd()
  487.     flist = []
  488.     if not os.path.isdir(xdir):
  489.         return []
  490.    
  491.     os.chdir(xdir)
  492.     for f in glob.glob("*.*"):
  493.         flist.append(f)
  494.    
  495.     os.chdir(cwd)
  496.     return flist
  497.  
  498. def write_info(singer, singer_slug):
  499.     if os.path.isfile('meta_info.txt'):
  500.         while True:
  501.             answer = raw_input('meta_info.txt existed, do you want to continue? (Y/N): ')
  502.             if answer == 'y' or answer == 'Y':
  503.                 break
  504.             elif answer == 'n' or answer == 'N':
  505.                 return
  506.             else:
  507.                 continue
  508.     with open('meta_info.txt', 'w') as f:
  509.         f.write(singer + "\n")
  510.         f.write(singer_slug + "\n")
  511.  
  512. def check_client_secret():
  513.     print 'Checking if client secret existed'
  514.     if os.path.isfile('client_secrets.json'):
  515.         print 'File existed'
  516.     else:
  517.         command = 'cp /home/van7hu/zzz/abc/tmp/client_secrets.json client_secrets.json'
  518.         print 'Executing command: ' + command
  519.         os.system(command)  
  520.        
  521.            
  522. def do_the_work():
  523.     searchTerm = sys.argv[1]
  524.     jumper = sys.argv[2]
  525.     directory = sys.argv[3]
  526.     if len(sys.argv) >= 5:
  527.         singer = sys.argv[4]
  528.  
  529.     cwd = os.getcwd()
  530.     if not os.path.isdir(directory):
  531.         os.mkdir(directory)
  532.    
  533.     os.chdir(directory)
  534.     if len(sys.argv) >= 6:
  535.         singer_slug = sys.argv[5]
  536.         write_info(singer, singer_slug)
  537.    
  538.     check_client_secret()
  539.  
  540.     # init gd for google drive upload
  541.     gauth = GoogleAuth()
  542.  
  543.     gauth.LoadCredentialsFile("mycreds.txt")
  544.     if gauth.credentials is None:
  545.         # Authenticate if they're not there
  546.         gauth.LocalWebserverAuth()
  547.     elif gauth.access_token_expired:
  548.         # Refresh them if expired
  549.         gauth.Refresh()
  550.     else:
  551.         # Initialize the saved creds
  552.         gauth.Authorize()
  553.        
  554.     drive = GoogleDrive(gauth)
  555.    
  556.     # some auxilary steps
  557.     if len(list_all_files('img/')) < 7:
  558.         print 'You have less than image, download 100 images.'
  559.         get_images(singer)
  560.     info = get_meta_info()
  561.     for xz in xrange(len(info)):
  562.         info[xz] = info[xz].replace('\n', '')
  563.  
  564.     if len(info) < 10:
  565.         with open('meta_info.txt', 'w') as f:
  566.             print 'Uploading image, and write meta_info'
  567.             for inf in info:
  568.                 f.write(inf + '\n')
  569.             for img in list_all_files('img/'):
  570.                 if img.endswith('jpg') or img.endswith('jpeg') or img.endswith('png'):
  571.                     [img_id, img_url] = image_upload('img/'+img)
  572.                     f.write(img_url+','+str(img_id)+'\n')
  573.                 else:
  574.                     pass
  575.     # reload the information
  576.     info = get_meta_info()
  577.     ARTIST_NAME = info[0]
  578.     ARTIST_SLUG = info[1]
  579.     # END auxilary steps
  580.     page_urls = get_wo(int(sys.argv[2]))
  581.  
  582.     video_urls = []
  583.     for page in page_urls:
  584.         video_urls =  video_urls + get_video_url(page)
  585.  
  586.     print 'Total: ' + str(len(video_urls)) + ' files to get'
  587.  
  588.     raw_input('Press any key to continue or Ctrl-C to cancel.')
  589.    
  590.     counter = 0
  591.    
  592.     for v in xrange(len(video_urls)):
  593.         try:
  594.             v_url = video_urls[v]
  595.             print '---------------'
  596.             print 'Begin iteration ' + str(v+1) + '/' + str(len(video_urls))
  597.             print 'Current time: ' + str(datetime.now())
  598.             print 'Checking the valid of url: ' + v_url[0]
  599.             if 'https://www.youtube.com/watch?v=' not in v_url[0]:
  600.                 print 'The link is invalid, continue to next iteration'
  601.                 continue
  602.            
  603.             print 'The link is valid, continue iteration'
  604.            
  605.             remove_downloaded_youtube()
  606.             SONG_NAME  = v_url[1]
  607.             idx = randint(2,len(info))
  608.             while idx >= len(info):
  609.                 print 'idx is rather too big. len(info): ' + str(len(info)) + ', idx: ' + str(idx)
  610.                 idx = randint(2,len(info))
  611.            
  612.  
  613.             tmp = info[idx].split(',')
  614.             try:
  615.                 THUMB_URL, FEATURE_IMG_ID = tmp[0], int(tmp[1])
  616.             except:
  617.                 print 'Error setting THUMB_URL & FEATURE_IMG_ID'
  618.                 print tmp[0]
  619.                 print tmp[1]
  620.                 sys.exit(0)
  621.  
  622.             lyrics = ''
  623.            
  624.             lyrics_link = search(SONG_NAME)
  625.             # check if is integer
  626.             try:
  627.                 lyrics_link = lyrics_link + 1
  628.                 print 'Song has no lyrics'
  629.                 lyrics = ''
  630.             except:
  631.                 print 'Song my have Lyrics, link is: ' + str(lyrics_link)
  632.                 try:
  633.                     lyrics = '<br/><strong>' + SONG_NAME + '</strong><br/>' + get_lyrics(lyrics_link)
  634.                 except:
  635.                     lyrics = ''
  636.                     pass
  637.  
  638.             print 'Downloading url: ' + v_url[0]
  639.            
  640.             # working with mp3
  641.             download_youtube_mp3(v_url[0])
  642.            
  643.             flist = []
  644.             for f in glob.glob("*.mp3"):
  645.                 flist.append(f)
  646.             print 'Uploading mp3 file: "' + flist[0] + '"'
  647.             mp3_url = ftp_ssh_mp3(flist[0], ARTIST_SLUG)
  648.             print 'mp3_url: '+mp3_url
  649.            
  650.             excerpt = prepare_post_excerpt(ARTIST_SLUG, ARTIST_NAME, mp3_url)
  651.             content = prepare_post_content_mp3(ARTIST_SLUG, ARTIST_NAME, SONG_NAME, THUMB_URL, mp3_url) + lyrics
  652.             content = content.replace('azlyrics.com', 'apkapp.info')
  653.             content = content.replace('Usage of', '#Usage of')
  654.            
  655.             print 'Making post for MP3 file'
  656.             status = post_to_server(SONG_NAME, content, excerpt, FEATURE_IMG_ID, 194)  # 194 is music category
  657.            
  658.             print 'Post_status: ' + str(status)
  659.            
  660.            
  661.             m = hashlib.md5()
  662.             m.update(v_url[0])
  663.  
  664.             mp4_file = m.hexdigest() + '.mp4'
  665.             # check if mp4 file existed already.
  666.             if gd_check_exist(drive, mp4_file) == False:
  667.                 print 'Mp4 file not existed yet, download it.'
  668.                 download_youtube_mp4(v_url[0])
  669.  
  670.                 print 'Get feature image for MP4 file'
  671.                 idx = randint(2,len(info))
  672.                 while idx >= len(info):
  673.                     print 'idx is rather too big. len(info): ' + str(len(info)) + ', idx: ' + str(idx)
  674.                     idx = randint(2,len(info))
  675.                
  676.  
  677.                 tmp = info[idx].split(',')
  678.                 try:
  679.                     THUMB_URL, FEATURE_IMG_ID = tmp[0], int(tmp[1])
  680.                 except:
  681.                     print 'Error setting THUMB_URL & FEATURE_IMG_ID'
  682.                     print tmp[0]
  683.                     print tmp[1]
  684.                     sys.exit(0)
  685.                
  686.                 flist = []
  687.                 for f in glob.glob('*.mp4'):
  688.                     flist.append(f)
  689.                
  690.                 old_mp4_file = flist[0]
  691.                 command = 'cp "' + old_mp4_file + '" ' + mp4_file
  692.                 print 'Executing command: ' + command
  693.                 os.system(command)
  694.                  
  695.                 mp4_url = upload_gd(mp4_file, drive)
  696.                 print 'Insert shared link into link table'
  697.                 mp4_database_id = insert_dlink(mp4_url)
  698.                 LINK_ID = "LINK_ID:A" + str(mp4_database_id) + "A"
  699.                 LINK_DESKTOP = "LINK_ID:AA" + str(mp4_database_id) + "A"
  700.                 LINK_MOBILE = "LINK_ID:AAA" + str(mp4_database_id) + "A"
  701.  
  702.                 content = prepare_post_content_mp4(ARTIST_SLUG, ARTIST_NAME, SONG_NAME, THUMB_URL, LINK_ID, LINK_DESKTOP, LINK_MOBILE) + lyrics
  703.                 content = content.replace('azlyrics.com', 'apkapp.info')
  704.                 content = content.replace('Usage of', '#Usage of')
  705.                
  706.                 excerpt = prepare_post_excerpt_mp4(ARTIST_SLUG, ARTIST_NAME, LINK_ID)
  707.                
  708.                 print 'Making post for MP4 file'
  709.                 status = post_to_server(SONG_NAME, content, excerpt, FEATURE_IMG_ID, 218)  # musics-videoclip
  710.                
  711.                 print 'Post_status: ' + str(status)        
  712.             else:
  713.                 print 'Mp4 file existed, do not download the file, go to next round.'
  714.                 continue
  715.  
  716.                 print 'Get feature image for MP4 file'
  717.                 idx = randint(2,len(info))
  718.                 while idx >= len(info):
  719.                     print 'idx is rather too big. len(info): ' + str(len(info)) + ', idx: ' + str(idx)
  720.                     idx = randint(2,len(info))
  721.                
  722.  
  723.                 tmp = info[idx].split(',')
  724.                 try:
  725.                     THUMB_URL, FEATURE_IMG_ID = tmp[0], int(tmp[1])
  726.                 except:
  727.                     print 'Error setting THUMB_URL & FEATURE_IMG_ID'
  728.                     print tmp[0]
  729.                     print tmp[1]
  730.                     sys.exit(0)
  731.                
  732.                 # TODO: Get mp4 link id
  733.                 print 'Get mp4 url'
  734.                 mp4_url = gd_get_dlink(drive, mp4_file)
  735.                 if mp4_url == False:
  736.                     print 'Could not get mp4_url, there is error'
  737.                     continue
  738.                
  739.                 print 'Mp4 url: ' + mp4_url
  740.                 print 'Get mp4 ID from database'
  741.                 mp4_database_id = getid_from_dlink(mp4_url)
  742.                 print 'mp4_id: ' + str(mp4_database_id)
  743.                 LINK_ID = "LINK_ID:A" + str(mp4_database_id) + "A"
  744.                 LINK_DESKTOP = "LINK_ID:AA" + str(mp4_database_id) + "A"
  745.                 LINK_MOBILE = "LINK_ID:AAA" + str(mp4_database_id) + "A"
  746.  
  747.                 content = prepare_post_content_mp4(ARTIST_SLUG, ARTIST_NAME, SONG_NAME, THUMB_URL, LINK_ID, LINK_DESKTOP, LINK_MOBILE) + lyrics
  748.                 content = content.replace('azlyrics.com', 'apkapp.info')
  749.                 content = content.replace('Usage of', '#Usage of')
  750.                
  751.                 excerpt = prepare_post_excerpt_mp4(ARTIST_SLUG, ARTIST_NAME, LINK_ID)
  752.                
  753.                 print 'Making post for MP4 file'
  754.                 status = post_to_server(SONG_NAME, content, excerpt, FEATURE_IMG_ID, 218)  # musics-videoclip
  755.                
  756.                 print 'Post_status: ' + str(status)        
  757.             remove_downloaded_youtube()
  758.            
  759.            
  760.         except Exception as e:
  761.             print 'Iteration error'
  762.             print 'Error: ' + str(e)
  763.             continue
  764.        
  765.        
  766.    
  767.     os.chdir(cwd)
  768.  
  769. do_the_work()
Add Comment
Please, Sign In to add comment