Skip to content

Commit

Permalink
extract youtube views initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
patt0 committed Feb 25, 2015
1 parent b700bc0 commit 2255cd7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions appengine/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .activity_post import ActivityPost
from .activity_record import ActivityRecord
from .activity_record import ActivityMetaData
from .account import Account
from .activity_type import ActivityType
from .product_group import ProductGroup
Expand Down
53 changes: 53 additions & 0 deletions appengine/services/update_gplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@

from google.appengine.api import taskqueue
from google.appengine.api import mail
from google.appengine.api import app_identity

from apiclient.discovery import build

from models import ActivityPost
from models import ActivityRecord
from models import ActivityMetaData
from models import Account
from models import activity_record as ar

Expand Down Expand Up @@ -163,6 +165,25 @@ def is_gde_post(activity):
else:
return True

def is_youtube_video(activity):
"""Identify youtube video's."""
content = activity.links
result = re.search('www.youtube.com', content, flags=re.IGNORECASE)
if result is None:
return False
try:
id_pos = content.index("watch?v=") + 8

This comment has been minimized.

Copy link
@SmokyBob

SmokyBob Feb 25, 2015

"default" "shortened" youtube sharing link are like
http://youtu.be/QmszsH5RlKI
or
http://youtu.be/tQ_NRQUFthU?list=PLOU2XLYxmsIKU5doDlNwQlq7OO6hAY8K-

I think we should support them too

This comment has been minimized.

Copy link
@patt0

patt0 Mar 16, 2015

Author Owner

I searched the entire raw dump and these are not used ever ... what I can't get my head around is how to convert these links into the right video id

http://www.youtube.com/attribution_link?a=L6PhQhExMaU&u=/watch?v%3D7NO6ZMAkAiw%26feature%3Dshare

This comment has been minimized.

Copy link
@SmokyBob

SmokyBob Mar 16, 2015

Damn!
I think it's Google + doing something strange when sharing.
I'm 1000000% sure that this activity was shared using http://youtu.be/QmszsH5RlKI as I've then updated it inside the Meta link.

BTW judging by how the url is build I think from /watch is part of the encoded URL of the video.

watch?v%3D7NO6ZMAkAiw%26feature%3Dshare = watch?v=7NO6ZMAkAiw&feature=share

From the result the id is from = to &

video_id = content[id_pos:]
try:
param_pos = video_id.index("&")
video_id_trimed = video_id[:param_pos]
except ValueError:
video_id_trimed = video_id
except ValueError:
logging.info('badly formed video url')
return False

return video_id_trimed

class CronUpdateGplus(webapp2.RequestHandler):

Expand All @@ -172,6 +193,10 @@ def get(self):
"""create tasks."""
logging.info('crons/upd_gplus')

# taskqueue.add(queue_name='gplus',
# url='/tasks/upd_gplus',
# params={'gplus_id':111820256548303113275})

accounts = Account.query()
user_count = 0
for account in accounts:
Expand Down Expand Up @@ -204,6 +229,8 @@ def post(self):
#build the service object for the gplus api
API_KEY = get_server_api_key()
gplus_service = build('plus', 'v1', developerKey=API_KEY)
#build the service object of the yt api
yt_service = build('youtube', 'v3', developerKey=API_KEY)
#get the activities for the gde
activities = ActivityPost.query(ActivityPost.gplus_id == gplus_id)

Expand All @@ -217,6 +244,26 @@ def post(self):
if activity_record.deleted:
continue

# find out if it is a video
video_id = is_youtube_video(activity)
if video_id is not False:
logging.info('video found %s', video_id)
# get the stats for the video
stats = yt_service.videos().list(part="statistics", id=video_id).execute()
views = stats["items"][0]['statistics']['viewCount']
if activity_record.metadata:
if activity_record.metadata[0].impact != int(views):
activity_record.metadata[0].impact = int(views)
logging.info('stats updated: %s', views)
activity_record.put()
else:
activity_record.metadata.append(ActivityMetaData())
activity_record.metadata[0].impact = int(views)
logging.info('stats updated: %s', views)
activity_record.put()



# get the activity from gplus
fields = 'id,verb,actor/id,annotation,object(id,actor/id,plusoners/totalItems,replies/totalItems,resharers/totalItems,content)'
try:
Expand Down Expand Up @@ -304,6 +351,12 @@ def update_if_changed(self, entity, post):
def send_update_mail(self, bad_posts, gplus_id):
"""Send out a mail with a link to the post for update."""

#if we run this from the staging environment do not send emails
app = app_identity.get_default_version_hostname();
if app == "elite-firefly-737.appspot.com":
logging.info('running from %s, not sending bad post email', app)
return

logging.info('sending a bad_post mail')

# get the user
Expand Down

0 comments on commit 2255cd7

Please sign in to comment.