Load Tweets, load more tweets finished
This commit is contained in:
parent
e252e07179
commit
0f2d27d5c3
@ -26,7 +26,7 @@ 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'^getSixTweets', TwitterClass.getLastSixTweets),
|
||||||
url(r'^getTwelveTweets', TwitterClass.getLastSixTweets),
|
url(r'^getMoreTweets/(?P<twitter_id>\w{0,50})', TwitterClass.getMoreTweets),
|
||||||
url(r'feeds/<int:feed_id>', FeedViewSet.update_feed)
|
url(r'feeds/<int:feed_id>', FeedViewSet.update_feed)
|
||||||
]
|
]
|
||||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from app_be.views.twitter_api import twitter_api
|
||||||
from django.http import JsonResponse, HttpRequest
|
from django.http import JsonResponse, HttpRequest
|
||||||
|
|
||||||
from py_jwt_validator import PyJwtValidator, PyJwtException
|
from py_jwt_validator import PyJwtValidator, PyJwtException
|
||||||
@ -9,6 +10,7 @@ from rest_framework.viewsets import ModelViewSet
|
|||||||
|
|
||||||
from app_be.models import Feed
|
from app_be.models import Feed
|
||||||
from app_be.serializers import FeedSerializer
|
from app_be.serializers import FeedSerializer
|
||||||
|
from twitter import Status
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -60,9 +62,25 @@ class LoginClass:
|
|||||||
class TwitterClass:
|
class TwitterClass:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def getLastSixTweets():
|
def getLastSixTweets(request):
|
||||||
return JsonResponse({[{"asdf", "asdf", "sdfasdf", "asdf"}, {"asdf", "asdf", "sdfasdf", "asdf"}]}, safe=False,
|
|
||||||
status=200)
|
#TODO: Bearer Token im FE einfügen
|
||||||
|
'''user_sub = authorize(request)
|
||||||
|
if not user_sub:
|
||||||
|
return JsonResponse({}, status=401)'''
|
||||||
|
|
||||||
|
return JsonResponse(status=200,data=twitter_api.get_last_six_tweets(),safe=False)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@api_view(['GET'])
|
||||||
|
def getMoreTweets(request,twitter_id):
|
||||||
|
|
||||||
|
#TODO: Bearer Token im FE einfügen
|
||||||
|
'''user_sub = authorize(request)
|
||||||
|
if not user_sub:
|
||||||
|
return JsonResponse({}, status=401)'''
|
||||||
|
|
||||||
|
return JsonResponse(status=200,data=twitter_api.get_more_tweets(twitter_id),safe=False)
|
||||||
|
|
||||||
|
|
||||||
class FeedViewSet(ModelViewSet):
|
class FeedViewSet(ModelViewSet):
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import twitter
|
import twitter
|
||||||
import os
|
import os
|
||||||
from app_be.models import Tweet
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@ -19,18 +20,51 @@ class twitter_api:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_last_six_tweets():
|
def get_last_six_tweets():
|
||||||
last_six = api.GetUserTimeline('waecmg4',count=6)
|
|
||||||
print(last_six)
|
last_six = api.GetUserTimeline(screen_name='waecmg4',count=6,exclude_replies=True,include_rts=False)
|
||||||
|
print(type(last_six))
|
||||||
|
|
||||||
|
response = []
|
||||||
|
|
||||||
|
for tweet in last_six:
|
||||||
|
print(tweet)
|
||||||
|
|
||||||
|
parsed_date = datetime.strptime(tweet.created_at,"%a %b %d %H:%M:%S +0000 %Y")
|
||||||
|
tweet_date = datetime.strftime(parsed_date,"%d.%m.%y %H:%M")
|
||||||
|
|
||||||
|
if tweet.urls:
|
||||||
|
twitter_url = tweet.urls[0].expanded_url
|
||||||
|
else:
|
||||||
|
twitter_url = "No url for tweet found"
|
||||||
|
|
||||||
|
response.append({'icon': None,'text':tweet.text,'url':twitter_url,'created_date':tweet_date, 'tweet_id':tweet.id})
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_last_twelve_tweets():
|
def get_more_tweets(twitter_id):
|
||||||
last_twelve = api.GetUserTimeline('waecmg4',count=12)
|
|
||||||
print(last_twelve)
|
|
||||||
|
|
||||||
@staticmethod
|
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False, max_id=twitter_id)
|
||||||
def get_last_n_tweets(n):
|
|
||||||
last_twelve = api.GetUserTimeline('waecmg4',count=n)
|
response = []
|
||||||
print(last_twelve)
|
|
||||||
|
for tweet in last_six:
|
||||||
|
print(tweet)
|
||||||
|
|
||||||
|
parsed_date = datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
|
||||||
|
tweet_date = datetime.strftime(parsed_date, "%d.%m.%y %H:%M")
|
||||||
|
|
||||||
|
if tweet.urls:
|
||||||
|
twitter_url = tweet.urls[0].expanded_url
|
||||||
|
else:
|
||||||
|
twitter_url = "No url for tweet found"
|
||||||
|
|
||||||
|
response.append(
|
||||||
|
{'icon': None, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
||||||
|
'tweet_id': tweet.id})
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_update(text, url):
|
def post_update(text, url):
|
||||||
|
|||||||
@ -20,7 +20,8 @@ setup(name='waecm-2021-group-04',
|
|||||||
'python-twitter==3.5',
|
'python-twitter==3.5',
|
||||||
'python-dotenv==0.17.1',
|
'python-dotenv==0.17.1',
|
||||||
'feedsearch==1.0.12',
|
'feedsearch==1.0.12',
|
||||||
'Pillow==8.2.0'
|
'Pillow==8.2.0',
|
||||||
|
'python-dateutil==2.8.1'
|
||||||
],
|
],
|
||||||
license='BSD License', # example license
|
license='BSD License', # example license
|
||||||
description='DESCRIPTION')
|
description='DESCRIPTION')
|
||||||
|
|||||||
@ -31,6 +31,7 @@ import {MaterialFileInputModule} from 'ngx-material-file-input';
|
|||||||
import { DialogComponent } from './component/dialog/dialog.component';
|
import { DialogComponent } from './component/dialog/dialog.component';
|
||||||
import {MatDialogModule} from '@angular/material/dialog';
|
import {MatDialogModule} from '@angular/material/dialog';
|
||||||
import {InterceptorService} from './services/interceptor.service';
|
import {InterceptorService} from './services/interceptor.service';
|
||||||
|
import {MatCardModule} from "@angular/material/card";
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [LandingComponent, LoginComponent, NavigationComponent,
|
declarations: [LandingComponent, LoginComponent, NavigationComponent,
|
||||||
@ -54,7 +55,8 @@ import {InterceptorService} from './services/interceptor.service';
|
|||||||
MatSnackBarModule,
|
MatSnackBarModule,
|
||||||
MatCheckboxModule,
|
MatCheckboxModule,
|
||||||
MaterialFileInputModule,
|
MaterialFileInputModule,
|
||||||
MatDialogModule
|
MatDialogModule,
|
||||||
|
MatCardModule
|
||||||
],
|
],
|
||||||
// enables injecting
|
// enables injecting
|
||||||
providers: [
|
providers: [
|
||||||
|
|||||||
@ -5,24 +5,26 @@
|
|||||||
<br>
|
<br>
|
||||||
<a routerLink="/einstellungen/editieren">RSS-Feed erstellen</a>
|
<a routerLink="/einstellungen/editieren">RSS-Feed erstellen</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-center" *ngIf="feeds.length !== 0">
|
<div class="text-center" *ngIf="!(tweets.length > 0)">
|
||||||
<span>Keine Tweets vorhanden</span>
|
<span>Keine Tweets vorhanden</span>
|
||||||
<br>
|
<br>
|
||||||
<span>Schau später noch einmal vorbei!</span>
|
<span>Schau später noch einmal vorbei!</span>
|
||||||
</div>
|
</div>
|
||||||
<div *ngIf="feeds.length !== 0">
|
<div *ngIf="feeds.length !== 0">
|
||||||
<div>
|
<div>
|
||||||
<div class="container" *ngFor="let tweet of tweets">
|
<div style="padding-bottom: 10px" class="container" *ngFor="let tweet of tweets">
|
||||||
|
<mat-card style="background-color: #f5f5f5">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-2 text-center padding-0 margin-auto">
|
<div class="col-2 text-center padding-0 margin-auto">
|
||||||
<img class="feed-icon" src="{{tweet.icon}}" alt="Feed-Icon">
|
<img class="feed-icon" src="{{tweet.icon}}" alt="Feed-Icon">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-10">
|
<div class="col-10">
|
||||||
<p class="overflow-break">{{tweet.text}}
|
<p class="overflow-break">{{tweet.text}}
|
||||||
<span class="white-space-no-wrap">{{tweet.date_time}}</span></p>
|
<span class="white-space-no-wrap"> - {{tweet.created_date}}</span><br>
|
||||||
<a>{{tweet.url}}</a>
|
<a href="{{tweet.url}}" style="cursor: pointer">{{tweet.url}}</a></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</mat-card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import {FeedService} from '../../services/feed.service';
|
|||||||
import {IFeed} from '../../interfaces/feed.interface';
|
import {IFeed} from '../../interfaces/feed.interface';
|
||||||
import {Observable} from 'rxjs';
|
import {Observable} from 'rxjs';
|
||||||
import {Tweet} from "../../interfaces/interface";
|
import {Tweet} from "../../interfaces/interface";
|
||||||
|
import {SnackbarService} from "../../services/snackbar.service";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -19,7 +20,8 @@ export class TweetsComponent implements OnInit {
|
|||||||
|
|
||||||
constructor(private http: HttpClient,
|
constructor(private http: HttpClient,
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private _feedService: FeedService) {
|
private _feedService: FeedService,
|
||||||
|
private _snackbarService: SnackbarService) {
|
||||||
this._feedService.getFeeds().subscribe(
|
this._feedService.getFeeds().subscribe(
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
this.feeds = data;
|
this.feeds = data;
|
||||||
@ -33,29 +35,43 @@ export class TweetsComponent implements OnInit {
|
|||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|
||||||
this.fillTweets()
|
this.fillTweets()
|
||||||
|
console.log(this.tweets)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadMore() {
|
loadMore() {
|
||||||
|
|
||||||
const headerDict = {
|
console.log(this.tweets);
|
||||||
'Authorization': 'Bearer ' + this.authService.getToken(),
|
|
||||||
};
|
this.http.get<Tweet[]>('http://127.0.0.1:8000/getMoreTweets/'+this.tweets[this.tweets.length-1].tweet_id+'/').subscribe(data => {
|
||||||
|
console.log(data)
|
||||||
|
|
||||||
|
if(data.length == 0){
|
||||||
|
this._snackbarService.show("Keine weiteren Tweets vorhanden","Schließen");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return this.http.get('http://localhost:8000/api/login',
|
for(let tweet of data){
|
||||||
{
|
let position = tweet.text.search(": http");
|
||||||
headers: new HttpHeaders(headerDict),
|
tweet.text = tweet.text.substring(0,position);
|
||||||
observe: 'response',
|
this.tweets.push(tweet);
|
||||||
})
|
}
|
||||||
.subscribe(data => {
|
}
|
||||||
console.log(data);
|
);
|
||||||
alert('Returned with code: ' + data['status']);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fillTweets(){
|
fillTweets(){
|
||||||
|
|
||||||
|
this.http.get<Tweet[]>('http://127.0.0.1:8000/getSixTweets/').subscribe(data => {
|
||||||
|
console.log(data)
|
||||||
|
for(let tweet of data){
|
||||||
|
let position = tweet.text.search(": http");
|
||||||
|
tweet.text = tweet.text.substring(0,position);
|
||||||
|
this.tweets.push(tweet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,7 +11,8 @@ export interface Tweet {
|
|||||||
|
|
||||||
icon: any;
|
icon: any;
|
||||||
text: string;
|
text: string;
|
||||||
date_time: DateTimeFormat;
|
url: string;
|
||||||
url: String;
|
created_date: string;
|
||||||
|
tweet_id: number;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user