Twitter API + Bot start
This commit is contained in:
parent
e90ae52267
commit
0a212a6a65
4
.env
Normal file
4
.env
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
TWITTER_API_KEY = 5eQwRc16aXnLwDYrLkxQHbcRS
|
||||||
|
TWITTER_API_SECRET = LUpU222xtAc92RKQEP3bMqrzTApvJTR6JVOOm4Uxx9DvnaOLOQ
|
||||||
|
TWITTER_ACCESS_TOKEN = 1386734481734787073-Xt0CUL467DJJUrVX3oPxAWVFbZB2A8
|
||||||
|
TWITTER_ACCESS_TOKEN_SECRET = CgRPGogf8XhZ3nsLkaEYvvjddBLGTPAeieM9UVsusQYMS
|
||||||
@ -3,11 +3,11 @@ from django.db import models
|
|||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
class User(models.Model):
|
class User(models.Model):
|
||||||
...
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Tweet(models.Model):
|
class Icon(models.Model):
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
image = models.ImageField(upload_to='feed_icons')
|
||||||
|
|
||||||
|
|
||||||
class Feed(models.Model):
|
class Feed(models.Model):
|
||||||
@ -17,3 +17,10 @@ class Feed(models.Model):
|
|||||||
|
|
||||||
class FeedEntry(models.Model):
|
class FeedEntry(models.Model):
|
||||||
tweeted = models.BooleanField()
|
tweeted = models.BooleanField()
|
||||||
|
|
||||||
|
|
||||||
|
class Tweet(models.Model):
|
||||||
|
icon = models.ForeignKey(Icon, on_delete=models.CASCADE)
|
||||||
|
text = models.CharField(max_length=137)
|
||||||
|
date_time = models.DateTimeField()
|
||||||
|
url = models.CharField(max_length=100)
|
||||||
@ -18,10 +18,13 @@ from django.contrib import admin
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
from app_be.views.rest_api import *
|
from app_be.views.rest_api import *
|
||||||
|
from app_be.views.twitter import *
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
url(r'^api/login', LoginClass.login),
|
url(r'^api/login', LoginClass.login),
|
||||||
|
url(r'^getSixTweets', TwitterClass.getLastSixTweets),
|
||||||
|
url(r'^getTweleveTweets',TwitterClass.getLastSixTweets)
|
||||||
]
|
]
|
||||||
|
|
||||||
router = DefaultRouter()
|
router = DefaultRouter()
|
||||||
|
|||||||
@ -50,3 +50,9 @@ class LoginClass:
|
|||||||
return JsonResponse({}, status=401)
|
return JsonResponse({}, status=401)
|
||||||
|
|
||||||
return JsonResponse({'user': user_sub}, safe=False, status=200)
|
return JsonResponse({'user': user_sub}, safe=False, status=200)
|
||||||
|
|
||||||
|
class TwitterClass:
|
||||||
|
@staticmethod
|
||||||
|
@api_view(['GET'])
|
||||||
|
def getLastSixTweets():
|
||||||
|
return JsonResponse({[{"asdf","asdf","sdfasdf","asdf"},{"asdf","asdf","sdfasdf","asdf"}]}, safe=False, status=200)
|
||||||
50
backend/app_be/views/twitter.py
Normal file
50
backend/app_be/views/twitter.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import twitter
|
||||||
|
import os
|
||||||
|
import sched, time
|
||||||
|
|
||||||
|
s = sched.scheduler(time.time(), time.sleep)
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
api = twitter.Api(consumer_key=os.getenv('TWITTER_API_KEY'),
|
||||||
|
consumer_secret=os.getenv('TWITTER_API_SECRET'),
|
||||||
|
access_token_key=os.getenv('TWITTER_ACCESS_TOKEN'),
|
||||||
|
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET'))
|
||||||
|
|
||||||
|
|
||||||
|
class twitter_api:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def check_credentials():
|
||||||
|
print(api.VerifyCredentials())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_last_six_tweets():
|
||||||
|
print(api.GetUserTimeline(screen_name='WAECMG4', count=6))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_last_twelve_tweets():
|
||||||
|
print(api.GetUserTimeline(screen_name='WAECMG4', count=12))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def post_update(text, url):
|
||||||
|
update = text + ": " + url
|
||||||
|
api.PostUpdate(update)
|
||||||
|
|
||||||
|
|
||||||
|
class twitter_bot:
|
||||||
|
|
||||||
|
def scan_active_feed(self):
|
||||||
|
starttime = time.time()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# Search RSS Feed
|
||||||
|
|
||||||
|
|
||||||
|
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
|
||||||
|
|
||||||
|
|
||||||
|
twitter_bot = twitter_bot()
|
||||||
|
twitter_bot.scan_active_feed()
|
||||||
18458
frontend/package-lock.json
generated
18458
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,10 @@ import { Component, OnInit } from '@angular/core';
|
|||||||
import {AuthService} from '../../services/auth.service';
|
import {AuthService} from '../../services/auth.service';
|
||||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||||
|
|
||||||
|
class Tweet {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-tweets',
|
selector: 'app-tweets',
|
||||||
templateUrl: './tweets.component.html',
|
templateUrl: './tweets.component.html',
|
||||||
@ -9,6 +13,8 @@ import {HttpClient, HttpHeaders} from '@angular/common/http';
|
|||||||
})
|
})
|
||||||
export class TweetsComponent implements OnInit {
|
export class TweetsComponent implements OnInit {
|
||||||
|
|
||||||
|
tweets:Tweet[] = [];
|
||||||
|
|
||||||
constructor(private http: HttpClient,
|
constructor(private http: HttpClient,
|
||||||
private authService: AuthService) { }
|
private authService: AuthService) { }
|
||||||
|
|
||||||
@ -16,10 +22,13 @@ export class TweetsComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadMore() {
|
loadMore() {
|
||||||
console.log('TODO: Implement');
|
|
||||||
const headerDict = {
|
const headerDict = {
|
||||||
'Authorization': 'Bearer ' + this.authService.getToken(),
|
'Authorization': 'Bearer ' + this.authService.getToken(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return this.http.get('http://localhost:8000/api/login',
|
return this.http.get('http://localhost:8000/api/login',
|
||||||
{
|
{
|
||||||
headers: new HttpHeaders(headerDict),
|
headers: new HttpHeaders(headerDict),
|
||||||
|
|||||||
@ -1,7 +1,17 @@
|
|||||||
import {Observable} from 'rxjs';
|
import {Observable} from 'rxjs';
|
||||||
|
import DateTimeFormat = Intl.DateTimeFormat;
|
||||||
|
|
||||||
export interface WSEvents {
|
export interface WSEvents {
|
||||||
message: Observable<Event>;
|
message: Observable<Event>;
|
||||||
error: Observable<Event>;
|
error: Observable<Event>;
|
||||||
close: Observable<Event>;
|
close: Observable<Event>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Tweet {
|
||||||
|
|
||||||
|
icon: any
|
||||||
|
text: string
|
||||||
|
date_time: DateTimeFormat
|
||||||
|
url: String
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
30
frontend/src/app/services/twitter.service.ts
Normal file
30
frontend/src/app/services/twitter.service.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import {HttpClient} from '@angular/common/http';
|
||||||
|
import {Injectable} from '@angular/core';
|
||||||
|
import {NGXLogger} from 'ngx-logger';
|
||||||
|
import {environment} from '../../environments/environment';
|
||||||
|
import {Observable} from 'rxjs';
|
||||||
|
import {Tweet} from "../interfaces/interface";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TwitterService {
|
||||||
|
private currentLocation = 'http://' + environment.location + ':' + environment.port + '/';
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private logger: NGXLogger,
|
||||||
|
private http: HttpClient
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
getSixTweets(): Observable<Tweet[]> {
|
||||||
|
const url = this.currentLocation + 'getSixTweets/';
|
||||||
|
this.logger.debug('Performing get Six Tweets');
|
||||||
|
return this.http.get<Tweet[]>(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
getTwelveTweets(): Observable<Tweet[]> {
|
||||||
|
const url = this.currentLocation + 'getTwelveTweets/';
|
||||||
|
this.logger.debug('Performing get Twelve Tweets');
|
||||||
|
return this.http.get<Tweet[]>(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user