63 lines
2.8 KiB
Python
63 lines
2.8 KiB
Python
|
import urllib.request, json, pymysql
|
||
|
from datetime import datetime
|
||
|
import twitter
|
||
|
|
||
|
db = pymysql.connect('localhost', 'kingofdog', 'XrE#513*IOC&tA*B', 'kingofdog')
|
||
|
cur = db.cursor()
|
||
|
|
||
|
def insertIntoDatabase(platform, title, url, author, authorUrl, published, image, originalID):
|
||
|
print(platform, title, url, author, authorUrl, published, image, originalID)
|
||
|
try:
|
||
|
print('all right')
|
||
|
cur.execute(
|
||
|
"INSERT INTO social_posts (post_plattform, post_content, post_url, post_author, post_author_url, post_date, post_img_source, post_original_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE post_content = %s, post_img_source = %s",
|
||
|
(platform, title, url, author, authorUrl, published, image, originalID, title, image))
|
||
|
db.commit()
|
||
|
except:
|
||
|
print('didnt work out for us... sadly')
|
||
|
db.rollback()
|
||
|
|
||
|
|
||
|
def getYouTubeVideos():
|
||
|
apiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=20&playlistId=UUEDHiXaIhm2VFu-hi6CcOWw&key=AIzaSyDAZ_TwVMZeiKDQxgWM2OYRq3YskEpY9yw'
|
||
|
with urllib.request.urlopen(apiUrl) as url:
|
||
|
data = json.loads(url.read().decode())
|
||
|
for item in data['items']:
|
||
|
snippet = item['snippet']
|
||
|
published = datetime.strptime(snippet['publishedAt'], '%Y-%m-%dT%H:%M:%S.000Z').timestamp()
|
||
|
author = snippet['channelTitle']
|
||
|
authorUrl = 'https://youtube.com/channel/' + snippet['channelId']
|
||
|
title = snippet['title']
|
||
|
url = 'https://youtu.be/' + snippet['resourceId']['videoId']
|
||
|
|
||
|
if snippet['thumbnails']['maxres']['url']:
|
||
|
thumbnail = snippet['thumbnails']['maxres']['url']
|
||
|
else:
|
||
|
thumbnail = snippet['thumbnails']['standard']['url']
|
||
|
|
||
|
insertIntoDatabase('YouTube', title, url, author, authorUrl, published, thumbnail, 0)
|
||
|
|
||
|
|
||
|
def getTwitterPosts():
|
||
|
api = twitter.Api(consumer_key='TsUzd4stukv9Ix7TGG7RdYq4k',
|
||
|
consumer_secret='sTRq4WcELJZuciTrkNUttGgWhEiGaUkuqNhISgaG4uHRFgzm0B',
|
||
|
access_token_key='1880071790-Nij2RaBDVRGVWoWW2PSJUwAvuLAOaQFAAr5tAtC',
|
||
|
access_token_secret='ldhLg0SP3ycrrdIqhNcddj0042pdGY9vmZMKQJRClmDkD')
|
||
|
tweets = api.GetUserTimeline(screen_name='kingofdogtv')
|
||
|
|
||
|
for tweet in tweets:
|
||
|
content = tweet.text
|
||
|
published = datetime.strptime(tweet.created_at, '%a %b %d %H:%M:%S %z %Y').timestamp()
|
||
|
author = tweet.user.name
|
||
|
authorUrl = 'https://twitter.com/' + tweet.user.screen_name
|
||
|
url = authorUrl + '/status/' + tweet.id_str
|
||
|
originalID = tweet.id
|
||
|
|
||
|
image = ''
|
||
|
if tweet.media:
|
||
|
image = tweet.media[0].media_url
|
||
|
|
||
|
insertIntoDatabase('Twitter', content, url, author, authorUrl, published, image, originalID)
|
||
|
|
||
|
getTwitterPosts()
|
||
|
db.close()
|