Archived
1
0

Upload twitter images to server

This commit is contained in:
Marcel 2018-11-01 13:20:46 +01:00
parent f168cf6863
commit 03495d8a35

View File

@ -1,10 +1,20 @@
import urllib.request, json, pymysql import json
import os
import random
import pymysql
import urllib.request
from datetime import datetime from datetime import datetime
from io import BytesIO
import requests
import twitter import twitter
from PIL import Image
db = pymysql.connect('localhost', 'kingofdog', 'XrE#513*IOC&tA*B', 'kingofdog') db = pymysql.connect('localhost', 'kingofdog', 'XrE#513*IOC&tA*B', 'kingofdog')
cur = db.cursor() cur = db.cursor()
def insertIntoDatabase(platform, title, url, author, authorUrl, published, image, originalID): def insertIntoDatabase(platform, title, url, author, authorUrl, published, image, originalID):
print(platform, title, url, author, authorUrl, published, image, originalID) print(platform, title, url, author, authorUrl, published, image, originalID)
try: try:
@ -18,6 +28,30 @@ def insertIntoDatabase(platform, title, url, author, authorUrl, published, image
db.rollback() db.rollback()
def generateImageName():
return random.getrandbits(128)
def uploadImage(imageUrl, originalName):
response = requests.get(imageUrl)
img = Image.open(BytesIO(response.content))
name = '%032x' % generateImageName()
path = 'files/userContent/%s.jpg' % name
img.save(path)
fileSize = os.path.getsize(path)
try:
cur.execute(
"INSERT INTO files (name, original_name, type, size, path, isUserData) VALUES (%s, %s, 'image/jpeg', %s, %s, 1)",
(name, originalName, fileSize, path))
db.commit()
except:
print('it didnt work')
db.rollback()
return '/f/' + name
def getYouTubeVideos(): def getYouTubeVideos():
apiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=20&playlistId=UUEDHiXaIhm2VFu-hi6CcOWw&key=AIzaSyDAZ_TwVMZeiKDQxgWM2OYRq3YskEpY9yw' 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: with urllib.request.urlopen(apiUrl) as url:
@ -56,8 +90,11 @@ def getTwitterPosts():
image = '' image = ''
if tweet.media: if tweet.media:
image = tweet.media[0].media_url image = tweet.media[0].media_url
uploadImage(image, tweet.id_str)
insertIntoDatabase('Twitter', content, url, author, authorUrl, published, image, originalID) insertIntoDatabase('Twitter', content, url, author, authorUrl, published, image, originalID)
getYouTubeVideos()
getTwitterPosts() getTwitterPosts()
db.close() db.close()