Tweet icons on frontend
This commit is contained in:
parent
0f2d27d5c3
commit
6f05749268
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
@ -9,7 +9,7 @@ class User(models.Model):
|
|||||||
class Feed(models.Model):
|
class Feed(models.Model):
|
||||||
url = models.TextField(blank=False, null=False, validators=[URLValidator(['http', 'https'])])
|
url = models.TextField(blank=False, null=False, validators=[URLValidator(['http', 'https'])])
|
||||||
active = models.BooleanField()
|
active = models.BooleanField()
|
||||||
icon = models.FileField(upload_to='feed-icons', blank=True, null=False, default='default-icon.svg',
|
icon = models.FileField(upload_to='feed-icons', blank=True, null=False, default='default-icon.png',
|
||||||
validators=[FileExtensionValidator(['png', 'svg'])])
|
validators=[FileExtensionValidator(['png', 'svg'])])
|
||||||
keywords = models.TextField(blank=False, null=False)
|
keywords = models.TextField(blank=False, null=False)
|
||||||
match_all_keywords = models.BooleanField(blank=True, default=False)
|
match_all_keywords = models.BooleanField(blank=True, default=False)
|
||||||
@ -21,7 +21,7 @@ class FeedEntry(models.Model):
|
|||||||
|
|
||||||
|
|
||||||
class Tweet(models.Model):
|
class Tweet(models.Model):
|
||||||
feed = models.ForeignKey(Feed, null=True, on_delete=models.SET_NULL)
|
icon = models.FileField(blank=True, null=False, default='default-icon.png')
|
||||||
text = models.CharField(max_length=500)
|
text = models.CharField(max_length=500)
|
||||||
date_time = models.DateTimeField()
|
date_time = models.DateTimeField()
|
||||||
url = models.CharField(max_length=500)
|
url = models.CharField(max_length=500)
|
||||||
|
|||||||
@ -2,7 +2,10 @@ 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
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@ -21,34 +24,11 @@ class twitter_api:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get_last_six_tweets():
|
def get_last_six_tweets():
|
||||||
|
|
||||||
last_six = api.GetUserTimeline(screen_name='waecmg4',count=6,exclude_replies=True,include_rts=False)
|
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False)
|
||||||
print(type(last_six))
|
print(type(last_six))
|
||||||
|
|
||||||
response = []
|
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
|
|
||||||
def get_more_tweets(twitter_id):
|
|
||||||
|
|
||||||
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False, max_id=twitter_id)
|
|
||||||
|
|
||||||
response = []
|
|
||||||
|
|
||||||
for tweet in last_six:
|
for tweet in last_six:
|
||||||
print(tweet)
|
print(tweet)
|
||||||
|
|
||||||
@ -60,8 +40,52 @@ class twitter_api:
|
|||||||
else:
|
else:
|
||||||
twitter_url = "No url for tweet found"
|
twitter_url = "No url for tweet found"
|
||||||
|
|
||||||
|
tmp_text = str(tweet.text)
|
||||||
|
end = tmp_text.find(": http")
|
||||||
|
tmp_text = tmp_text[0:end]
|
||||||
|
backend_tweet: Tweet = Tweet.objects.filter(text=tmp_text)
|
||||||
|
|
||||||
|
if not backend_tweet:
|
||||||
|
icon = "http://localhost:8000/media/default-icon.png"
|
||||||
|
else:
|
||||||
|
icon = "http://localhost:8000/media/feed-icons/" + os.path.basename(backend_tweet[0].icon.file.name)
|
||||||
|
|
||||||
|
response.append({'icon': icon, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
||||||
|
'tweet_id': tweet.id})
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_more_tweets(twitter_id):
|
||||||
|
|
||||||
|
last_six = api.GetUserTimeline(screen_name='waecmg4', count=6, exclude_replies=True, include_rts=False,
|
||||||
|
max_id=twitter_id)
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
tmp_text = str(tweet.text)
|
||||||
|
end = tmp_text.find(": http")
|
||||||
|
tmp_text = tmp_text[0:end]
|
||||||
|
backend_tweet: Tweet = Tweet.objects.filter(text=tmp_text)
|
||||||
|
|
||||||
|
if not backend_tweet:
|
||||||
|
icon = "http://localhost:8000/media/default-icon.png"
|
||||||
|
else:
|
||||||
|
icon = "http://localhost:8000/media/feed-icons/" + os.path.basename(backend_tweet[0].icon.file.name)
|
||||||
|
|
||||||
response.append(
|
response.append(
|
||||||
{'icon': None, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
{'icon': icon, 'text': tweet.text, 'url': twitter_url, 'created_date': tweet_date,
|
||||||
'tweet_id': tweet.id})
|
'tweet_id': tweet.id})
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
@ -46,7 +46,7 @@ class twitter_bot(threading.Thread):
|
|||||||
|
|
||||||
# preparing tweet
|
# preparing tweet
|
||||||
new_tweet = Tweet()
|
new_tweet = Tweet()
|
||||||
new_tweet.feed = feed
|
new_tweet.icon = feed.icon
|
||||||
new_tweet.text = current.title
|
new_tweet.text = current.title
|
||||||
|
|
||||||
if 'published' in current:
|
if 'published' in current:
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
<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="tweets.length !== 0">
|
||||||
<div>
|
<div>
|
||||||
<div style="padding-bottom: 10px" 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">
|
<mat-card style="background-color: #f5f5f5">
|
||||||
@ -21,7 +21,7 @@
|
|||||||
<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.created_date}}</span><br>
|
<span class="white-space-no-wrap"> - {{tweet.created_date}}</span><br>
|
||||||
<a href="{{tweet.url}}" style="cursor: pointer">{{tweet.url}}</a></p>
|
<a href="{{tweet.url}}" target='_blank' style="cursor: pointer">{{tweet.url}}</a></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</mat-card>
|
</mat-card>
|
||||||
|
|||||||
@ -3,9 +3,12 @@ import {AuthService} from '../../services/auth.service';
|
|||||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||||
import {FeedService} from '../../services/feed.service';
|
import {FeedService} from '../../services/feed.service';
|
||||||
import {IFeed} from '../../interfaces/feed.interface';
|
import {IFeed} from '../../interfaces/feed.interface';
|
||||||
import {Observable} from 'rxjs';
|
import {Observable, throwError} from 'rxjs';
|
||||||
import {Tweet} from "../../interfaces/interface";
|
import {Tweet} from "../../interfaces/interface";
|
||||||
import {SnackbarService} from "../../services/snackbar.service";
|
import {SnackbarService} from "../../services/snackbar.service";
|
||||||
|
import {DialogComponent} from "../dialog/dialog.component";
|
||||||
|
import {MatDialog} from "@angular/material/dialog";
|
||||||
|
import {NGXLogger} from "ngx-logger";
|
||||||
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -16,26 +19,32 @@ import {SnackbarService} from "../../services/snackbar.service";
|
|||||||
export class TweetsComponent implements OnInit {
|
export class TweetsComponent implements OnInit {
|
||||||
|
|
||||||
tweets: Tweet[] = [];
|
tweets: Tweet[] = [];
|
||||||
feeds: Observable<IFeed>[] = [];
|
feeds: IFeed[] = [];
|
||||||
|
|
||||||
constructor(private http: HttpClient,
|
constructor(private http: HttpClient,
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private _feedService: FeedService,
|
private _feedService: FeedService,
|
||||||
private _snackbarService: SnackbarService) {
|
private _snackbarService: SnackbarService,
|
||||||
|
private _dialog: MatDialog,
|
||||||
|
private _logger: NGXLogger) {
|
||||||
this._feedService.getFeeds().subscribe(
|
this._feedService.getFeeds().subscribe(
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
this.feeds = data;
|
this.feeds = data;
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
this.errorDialog();
|
||||||
|
this._logger.error(err);
|
||||||
|
return throwError(err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|
||||||
this.fillTweets()
|
this.fillTweets();
|
||||||
console.log(this.tweets)
|
console.log(this.tweets);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,36 +52,59 @@ export class TweetsComponent implements OnInit {
|
|||||||
|
|
||||||
console.log(this.tweets);
|
console.log(this.tweets);
|
||||||
|
|
||||||
this.http.get<Tweet[]>('http://127.0.0.1:8000/getMoreTweets/'+this.tweets[this.tweets.length-1].tweet_id+'/').subscribe(data => {
|
this.http.get<Tweet[]>('http://127.0.0.1:8000/getMoreTweets/' + this.tweets[this.tweets.length - 1].tweet_id + '/').subscribe(data => {
|
||||||
console.log(data)
|
console.log(data)
|
||||||
|
|
||||||
if(data.length == 0){
|
if (data.length == 0) {
|
||||||
this._snackbarService.show("Keine weiteren Tweets vorhanden","Schließen");
|
this._snackbarService.show("Keine weiteren Tweets vorhanden", "Schließen");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for(let tweet of data){
|
for (let tweet of data) {
|
||||||
let position = tweet.text.search(": http");
|
let position = tweet.text.search(": http");
|
||||||
tweet.text = tweet.text.substring(0,position);
|
tweet.text = tweet.text.substring(0, position);
|
||||||
this.tweets.push(tweet);
|
this.tweets.push(tweet);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
this.errorDialog();
|
||||||
|
this._logger.error(err);
|
||||||
|
return throwError(err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fillTweets(){
|
fillTweets() {
|
||||||
|
|
||||||
this.http.get<Tweet[]>('http://127.0.0.1:8000/getSixTweets/').subscribe(data => {
|
this.http.get<Tweet[]>('http://127.0.0.1:8000/getSixTweets/').subscribe(data => {
|
||||||
console.log(data)
|
console.log(data)
|
||||||
for(let tweet of data){
|
for (let tweet of data) {
|
||||||
let position = tweet.text.search(": http");
|
let position = tweet.text.search(": http");
|
||||||
tweet.text = tweet.text.substring(0,position);
|
tweet.text = tweet.text.substring(0, position);
|
||||||
this.tweets.push(tweet);
|
this.tweets.push(tweet);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
this.errorDialog();
|
||||||
|
this._logger.error(err);
|
||||||
|
return throwError(err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public errorDialog() {
|
||||||
|
const dialogRef = this._dialog.open(DialogComponent, {
|
||||||
|
data: {
|
||||||
|
title: 'Serverfehler',
|
||||||
|
body: 'Fehler bei der Kommunikation mit Twitter").',
|
||||||
|
abort: 'Abbrechen',
|
||||||
|
confirm: 'OK',
|
||||||
|
hideAbort: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user