Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- print "Content-type: text/html\n\n"
- # -*- coding:Utf-8 -*-
- import urllib2
- def check_channel_update(new_data, channel):
- fichier = open('video.txt', 'r')
- data = fichier.read()
- fichier.close()
- base_url = 'http://www.youtube.com'
- data = data.split('\n\n')[:-1] #do not forget to add delimiter at end of each channel !
- channel_old_data = ''
- for element in data:
- if channel in element:
- channel_old_data = data.pop(data.index(element))
- break
- if not channel_old_data : return False
- channel_new_data = ''
- for new_video in new_data:
- if not new_video in channel_old_data:
- print 'new video avalaible'
- channel_new_data += base_url+new_video+' '+title[new_data.index(new_video)]+'\n'
- else:
- break #leave loop cause no new video after that
- if len(channel_new_data):
- before, after = channel_old_data.split('\n',1)
- update = before +'\n'+ channel_new_data + after
- data.insert(0, update)
- data = '\n\n'.join(data)
- data += '\n\n'
- write_data(data)
- def write_data(data):
- fichier = open('video.txt', 'w')
- fichier.write(data)
- fichier.close()
- def parse_html_source(url):
- data=urllib2.urlopen(url)
- data = data.read()
- start_tag = '<h3 class="yt-lockup-title">'
- data = [item.split('</h3>')[0] for item in data.split(start_tag)[1:]]
- title = [item.split('title="')[1].split('" aria-describedby')[0] for item in data]
- link = [item.split('href="')[1].split('">')[0] for item in data]
- return title, link
- def update_channel_file(): #used often
- fichier = open('channel.txt', 'r')
- data = fichier.readlines()
- fichier.close()
- name_channel_update = data.pop(0)
- data.append(name_channel_update)
- fichier = open('channel.txt', 'w')
- [fichier.write(item) for item in data]
- fichier.close()
- print name_channel_update
- return name_channel_update.split(' ',1)[0]
- url_channel = update_channel_file()
- title, link = parse_html_source(url_channel)
- check_channel_update(link, url_channel)
Advertisement
Add Comment
Please, Sign In to add comment