Back to Parent

import sys
import urllib.request, urllib.response, urllib.error
from bs4 import BeautifulSoup
import re
from apiclient.discovery import build
from oauth2client.tools import argparser
import requests

flag = 0;
dLIKE = 0 ;
dDis = 0;
like = [];
dis = [];
like.append(0);
dis.append(0);


class SoupScrape:

    def GetStats(self, code):
        #

        global originalRatio
        global ratio
        global factor

        video_url = 'http://www.youtube.com/watch?v=' + code
        startpage = urllib.request.urlopen(video_url)
        soup = BeautifulSoup(startpage.read(), "lxml")
        # Get Number of Views

        rawview_data = soup.find('div', {'class': 'watch-view-count'})
        view_number = rawview_data.contents[0]
        view_number = re.sub('[^0-9]', '', view_number)
        # Get Number of Likes and Dislikes

        raw_like = soup.find('button', {'class': 'like-button-renderer-like-button'})
        raw_dislike = soup.find('button', {'class': 'like-button-renderer-dislike-button'})
        # Likes

        likes = raw_like.contents[0]
        for data in likes:
            num_of_likes = data

        # Dislikes

        dislikes = raw_dislike.contents[0]
        for data in dislikes:
            num_of_dislikes = data
        #video name

        raw_name=soup.find('span', {'class': 'watch-title'})
        video_name=raw_name.contents[0]
        video_name=video_name.replace('\n', '')
        #print(video_name,"-","VIEWS",view_number, "-", "LIKES", num_of_likes, "-","DISLIKES", num_of_dislikes,'\n')

        num_of_likes = int(num_of_likes.replace(",", ""))
        num_of_dislikes = int(num_of_dislikes.replace(",", ""))
        like.append(num_of_likes);
        dis.append(num_of_dislikes);

        dLike= like[-1]- like[-2]
        dDis= dis[-1]- dis[-2]

        if (dLike<0):
            dDis = dDis-dLike
        elif (dDis<0):
            dLike = dLike-dDis

        print(dLike, dDis)

        if (dLike == 0):
            if (dDis == 0):
                r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/neu", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})
            else:
                r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/dis", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})
        elif (dDis == 0):
            r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/like", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dLike})
        else:
            if (dLike >100):
                originalRatio = (num_of_likes/num_of_dislikes)
                factor = ((num_of_likes+1)/num_of_likes)
                print("originalRatio, factor")
                print(originalRatio,factor)

            else:
                if (dLike/dDis == originalRatio):
                    r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/neu", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})
                elif (dLike/dDis > originalRatio):
                    r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/like", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dLike})
                elif (dLike/dDis < originalRatio):
                    r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/dis", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})
                else:
                    print("error")






        """
        if (dLike == dDis):
            r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/neu", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})
        elif (dLike > dDis):
            if (dLike >100):
                originalRatio = (num_of_likes/num_of_dislikes)
                factor = ((num_of_likes+1)/num_of_likes)
                print("originalRatio, factor")
                print(originalRatio,factor)
            else:
                r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/like", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dLike})
        elif (dLike < dDis):
            r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/dis", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': dDis})

        """


        ratio = (num_of_likes/num_of_dislikes)
        print('ratio, originalRatio')
        print(ratio,originalRatio)
        if (ratio/originalRatio >= factor):
            #if the increase is more than a certain percent, call function youtube to pull the trigger to LIFT UP

            r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/youtube", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': ratio})
            #set the baseline to this new ratio


            originalRatio = ratio
            print("newbaseline-higher")
            print(originalRatio)

        elif (ratio/originalRatio <= (2-factor)):
            #if the decrease is more than a certain percent, call function youtube2 to pull the trigger to PULL DOWN

            r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/youtube2", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': ratio})
            #set the baseline to this new ratio

            originalRatio = ratio
            print("newbaseline-lower")
            print(originalRatio)
        #r = requests.post("https://api.particle.io/v1/devices/22002b001351353432393433/youtube2", data={'access_token': 'ada24d2045a440d9f975c0861224febe6b414065', 'args': num_of_dislikes})


        #print(r.text)

        #a=r.text.split(',')

        #b=a[3].split(':')[1]

        #b=b[:-1]

        #b= int(b)

        #print(b)







DEVELOPER_KEY = "AIzaSyDXvzq3fviM_WaXeuoqpLUK74ezsS9LzOc"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"



#   Youtube Search Functions


def youtube_search(search_string, max_results=25):
    input = search_string.q
    print(input)
    outfile = open('temp.txt', 'w')
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
    search_response = youtube.search().list(q=search_string.q, part="id,snippet", maxResults=max_results).execute()
    videos = []
    # Add each result to the appropriate list, and then display the lists of

    # matching videos

    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"]))

    for x in range(0, len(videos)):
        outfile.write(videos[x] + '\n')

    outfile.close()


def search(artist):
    argparser.add_argument("--q", help="Search term", default=artist)
    argparser.add_argument("--max-results", help="Max results", default=5)
    args = argparser.parse_args()
    youtube_search(args)


# Main Program


if __name__ == "__main__":
    #

    # Youtube

    #


    while (flag != -1):
        stat_fetch = SoupScrape()
        stat_fetch.GetStats('HVyq6kAN6u4')
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0