Archived
1
0
This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
old/application/models/SocialMediaModel.php

114 lines
5.7 KiB
PHP
Raw Normal View History

2018-10-16 16:28:42 +00:00
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SocialMediaModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
private function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function getYouTubeVideos()
{
$apiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=20&playlistId=UUEDHiXaIhm2VFu-hi6CcOWw&key=AIzaSyDAZ_TwVMZeiKDQxgWM2OYRq3YskEpY9yw';
$apiResult = json_decode($this->getSslPage($apiUrl));
$items = $apiResult->items;
foreach ($items as $item) {
$snippet = $item->snippet;
$published = $snippet->publishedAt;
$author = $snippet->channelTitle;
$author_url = "https://youtube.com/channel/" . $snippet->channelId;
$title = $snippet->title;
if (isset($snippet->thumbnails->maxres->url)) $thumbnail = $snippet->thumbnails->maxres->url;
else $thumbnail = $snippet->thumbnails->standard->url;
$url = "http://youtu.be/" . $snippet->resourceId->videoId;
$this->db->query('INSERT INTO social_posts (post_plattform, post_content, post_url, post_author, post_author_url, post_date, post_img_source) VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE post_content = ?, post_img_source = ?;', ["YouTube", $title, $url, $author, $author_url, strtotime($published), $thumbnail, $title, $thumbnail]);
}
}
public function getTwitterPosts()
{
$consumer_key = 'TsUzd4stukv9Ix7TGG7RdYq4k';
$consumer_key_secret = 'sTRq4WcELJZuciTrkNUttGgWhEiGaUkuqNhISgaG4uHRFgzm0B';
$access_token = '1880071790-Nij2RaBDVRGVWoWW2PSJUwAvuLAOaQFAAr5tAtC';
$access_token_secret = 'ldhLg0SP3ycrrdIqhNcddj0042pdGY9vmZMKQJRClmDkD';
$oauth_hash = 'count=20&oauth_consumer_key=' . $consumer_key . '&oauth_nonce=' . time() . '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' . time() . '&oauth_token=' . $access_token . '&oauth_version=1.0';
$base = 'GET&' . urlencode('https://api.twitter.com/1.1/statuses/user_timeline.json') . '&' . rawurlencode($oauth_hash);
$key = rawurlencode($consumer_key_secret) . '&' . rawurlencode($access_token_secret);
$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$signature = rawurlencode($signature);
$oauth_header = 'oauth_consumer_key="' . $consumer_key . '", oauth_nonce="' . time() . '", oauth_signature="' . $signature . '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' . time() . '", oauth_token="' . $access_token . '", oauth_version="1.0", ';
$curl_header = array("Authorization: Oauth {$oauth_header}", 'Expect:');
$curl_request = curl_init();
curl_setopt($curl_request, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=20');
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($curl_request);
curl_close($curl_request);
$posts = json_decode($json);
foreach ($posts as $post) {
$content = str_replace("\n", "<br>", $post->text);
$author = $post->user->screen_name;
$author_url = 'https://twitter.com/' . $author;
$url = $author_url . '/status/' . $post->id_str;
$published = strtotime($post->created_at);
$original_id = $post->id_str;
if (isset($post->extended_entities->media[0]->media_url)) {
$image = $post->extended_entities->media[0]->media_url;
} else {
$image = '';
}
$this->db->query('INSERT INTO social_posts (post_plattform, post_content, post_url, post_author, post_author_url, post_date, post_img_source, post_original_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE post_content = ?, post_author_url = ?, post_original_id = ?;', ["Twitter", $content, $url, $author, $author_url, $published, $image, $original_id, $content, $author_url, $original_id]);
}
}
public function getAllPosts()
{
$this->getTwitterPosts();
$items = $this->db->query('SELECT * FROM social_posts ORDER BY post_date DESC')->result_array();
return $items;
}
public function getPosts($amount, $offset)
{
if($offset == 0) {
$items = $this->db->query('SELECT * FROM social_posts ORDER BY post_date DESC LIMIT ?', [$amount])->result_array();
} else {
$items = $this->db->query('SELECT * FROM social_posts ORDER BY post_date DESC LIMIT ? OFFSET ?', [$amount, $offset])->result_array();
}
return $items;
}
public function getPostsOfCategory($amount, $offset, $category) {
if($offset == 0) {
$items = $this->db->query('SELECT * FROM social_posts WHERE post_plattform LIKE ? ORDER BY post_date DESC LIMIT ?', [$category, $amount])->result_array();
} else {
$items = $this->db->query('SELECT * FROM social_posts WHERE post_plattform LIKE ? ORDER BY post_date DESC LIMIT ? OFFSET ?', [$category, $amount, $offset])->result_array();
}
return $items;
}
}