forked from egcodes/aristotle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainParseSources.py
1191 lines (1022 loc) · 44.8 KB
/
mainParseSources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import sys, os, ast
import urllib, urllib2, requests, json
import time
import simplejson
import signal
from sourceList import createNewsSourceByPresent
from datetime import datetime
from LinkHandler import LinkHandler
from LogHandler import LogHandler
from ServerDatabaseHandler import ServerDatabaseHandler
from BeautifulSoup import BeautifulSoup
class Main:
def __init__(self):
#=======================================================================
# Configuration values
#=======================================================================
self.wwwPath = '/var/www/html/'
#Resimler icin
self.linkImagePath = self.wwwPath + 'imageslink/'
# Url request'lerinde kullanilan agent
self.userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"
# Alinan ve islenen linkleri goster
self.showLink = False
#links database tablosu icin son ek
self.yearMonth = ""
#Turkce karakter
self.turkishDict = {'ı': 'i', 'ü': 'u', 'ö':'o', 'ş':'s', 'ç':'c', 'ğ':'g'}
#=======================================================================
# Configuration For Requests
#=======================================================================
#Bazi sitelerin haber resimleri icin class ile alinir
#birden fazla class girilebilir value kismina , karakteri ile bitisik yaz
self.imageClassForSources = {
'http://www.aksam.com.tr':'image',
'http://www.trthaber.com':'image',
'http://www.trthaber.com/haber/kultur-sanat/':'image',
'http://amkspor.sozcu.com.tr':'in_image',
'http://www.sosyalmedya.co':'attachment-large wp-post-image',
'http://www.teknolojioku.com':'newsImage',
'http://www.webrazzi.com': 'post-content',
'http://www.taraf.com.tr/yazarlar/':'info',
'http://www.donanimhaber.com':'entry',
'http://www.bugun.com.tr':'image',
'http://www.milliyet.com.tr/Yazar.aspx?aType=Yazarlar':'image',
'http://www.gazetevatan.com/yazarlar/':'aimg',
'http://www.cumhuriyet.com.tr/yazarlar':'author',
'http://www.sabah.com.tr/Yazarlar':'iBox',
'http://www.ensonhaber.com':'mansetresmi',
'http://www.yenisafak.com.tr/yazarlar/':'picture',
'http://www.haberler.com':'image',
'http://www.internethaber.com':'item img active',
'http://haber.sol.org.tr':'singlenews-image',
'http://haber.sol.org.tr':'singlenews-image',
'http://www.yeniakit.com.tr/yazarlar':'au-top-right',
'http://www.takvim.com.tr':'haberImg',
'http://www.haberturk.com':'image',
'http://www.haber7.com':'image_src',
'http://www.mynet.com':'twitter:image',
}
#Farkli meta etiketi icin tanim
self.descMetaTypes = {
'http://www.radikal.com.tr/yazarlar':{"name":"twitter:description"},
'http://www.radikal.com.tr/kultur':{"name":"twitter:description"},
'http://webtv.radikal.com.tr':{"name":"twitter:description"},
'http://www.radikal.com.tr':{"name":"twitter:description"},
'http://www.mynet.com/teknoloji':{"property":"og:description"},
'http://www.mynet.com':{"property":"og:description"},
'http://webtv.hurriyet.com.tr':{"property":"og:description"},
'http://www.haber7.com':{"name":"twitter:description"},
'http://www.samanyoluhaber.com':{"property":"og:description"},
}
#Desc'i alma title'i ata
self.notGetDesc = []
#Link'lerdeki ? karakteri icin
self.containQuestionCharacter = [
'http://www.posta.com.tr',
'http://www.odatv.com',
]
#Link sonundaki / karakteri silinmeyecek
self.notDeletedBackslashCharacter = [
'http://www.posta.com.tr',
'http://www.odatv.com',
'http://www.taraf.com.tr',
'http://www.taraf.com.tr/yazarlar/',
'http://www.samanyoluhaber.com',
'http://amkspor.sozcu.com.tr',
'http://www.haberler.com',
'http://www.diken.com.tr',
'http://www.fizikist.com',
]
#Haberden alinan resim cok kucuk ise yada yanlis ise title baz alinarak google'dan resim cekilir
#Google image limit'leri oldugundan surekli cekince yasaklandi
self.getGoogleImageList = []
#Link'in image link'inin icinde alindiktan sonra replace edilmesi gerek bir sey var ise
self.replaceStringForLink = {
'shiftdelete.net': ('/shiftdelete.net', '/s01.shiftdelete.net/img/general_b'),
}
#Asagiya girilen keyword'lar link icinde geciyor ise o link alinmaz
self.notGetLinkIfContainThisKeyword = ['javascript:', ]
#Hotlink korumasi olanlar bunlarin resimleri link vermek yerine indiriliyor
self.hotlinks = ['ensonhaber.com', 'bugun.com.tr', 'internethaber.com', 'haberler.com', 'odatv.com', 'cumhuriyet.com.tr', 'zaman.com.tr', 'donanimhaber.com', 'ajansspor.com', 'haber.sol.org.tr']
#Eger karakterler bozuk geliyor ise Request tpye
self.requestTypes = ['amkspor.sozcu.com.tr', 'yenisafak.com.tr', 'indir.com']
#Eger source'u encoding geliyor ise baska bir request yapilir
self.encodePageSource = ['mynet.com', 'trthaber.com']
#degeri 0 ise www eklenir , 1 ise www silinir, 2 ise http://www silinir
self.getTweetCountFix = {'yenisafak.com.tr':0, 'bigumigu.com':1, 'webrazzi.com':2, 'odatv.com':2}
#Eger title, desc karakterler bozuk geliyor temizlik icin
self.contentTitleDescReplace = ['zaman.com.tr', 'shiftdelete.net']
#Eger link'lerde , var ise facebook share count almak icin digeri kullanisin
self.linkContentComma = ['t24.com.tr', 'ntv.com.tr']
#Gormek istemedigin linkleri gec #imageLink'i icinde bulunanlar sadece
self.blackListLinkImage ={
}
#=======================================================================
# Class import
self.logHandler = LogHandler("Main")
# Database baglantisi
self.serverHandler = ServerDatabaseHandler()
# Local ve Server calisma ayrimi
self.getLinkCountLimit = 1500
if len(sys.argv) == 2:
category = sys.argv[1]
self.run(category)
elif len(sys.argv) == 3:
category = sys.argv[1]
source = sys.argv[2]
self.run(category, source)
else:
self.run()
def getGoogleImage(self, searchTerm):
searchTerm = self.multipleReplace(searchTerm, self.turkishDict)
searchTerm = '%20'.join(searchTerm.split()[:4])
# Set count to 0
count= 0
imageUrl = ""
# Notice that the start changes for each iteration in order to request a new set of images for each loop
try:
url = 'https://ajax.googleapis.com/ajax/services/search/images?' + 'v=1.0&q='+searchTerm
request = urllib2.Request(url, None, {'User-Agent' : self.userAgent})
response = urllib2.urlopen(request, timeout=10)
# Get results using JSON
results = simplejson.load(response)
data = results['responseData']
dataInfo = data['results']
# Iterate for each result and get unescaped url
for myUrl in dataInfo:
count = count + 1
if int(myUrl['width']) >= 300 and int(myUrl['height']) >= 300:
imageUrl = myUrl['unescapedUrl']
else:
imageUrl = ""
if imageUrl:
break
except:
imageUrl = ""
return imageUrl
def multipleReplace(self, text, wordDict):
for key in wordDict:
text = text.replace(key, wordDict[key])
return text
def downloadImage(self, source, link, path):
#Bu kaynaklar icin bot olmadigimizi anlatmak icin donen link'e istek yollanir
if source == 'cumhuriyet.com.tr':
try:
ln = 'http://www.cumhuriyet.com.tr'
req = urllib2.Request(ln, headers={'User-Agent' : self.userAgent})
html = urllib2.urlopen(req, timeout=5).read()
if len(html) < 250:
firstIndex = html.find('url=')
endIndex = html[firstIndex:].find('"') + firstIndex
url = html[firstIndex + len('url='): endIndex]
if url and url.find('http') != -1:
req = urllib2.Request(url, headers={'User-Agent' : self.userAgent})
html = urllib2.urlopen(req, timeout=5).read()
print url
except:
self.logHandler.logger("run")
#Bu kaynaklar icin bot olmadigimizi anlatmak icin donen link'e istek yollanir
elif source == 'odatv.com':
try:
ln = 'http://www.odatv.com'
req = urllib2.Request(ln, headers={'User-Agent' : self.userAgent})
html = urllib2.urlopen(req, timeout=5).read()
if len(html) < 300:
firstIndex = html.find('url=')
endIndex = html[firstIndex:].find('"') + firstIndex
url = html[firstIndex + len('url='): endIndex]
if url and url.find('http') != -1:
req = urllib2.Request(url, headers={'User-Agent' : self.userAgent})
html = urllib2.urlopen(req, timeout=5).read()
print url
except:
self.logHandler.logger("run")
req = urllib2.Request(link, headers={'User-Agent' : self.userAgent})
try:
imageId = "default.png"
if source == 'cumhuriyet.com.tr':
imageId = link[link[:link.rfind('/')].rfind('/') + 1:].replace('/', '')
else:
imageId = link[link.rfind('/') + 1:]
if imageId.find('?') != -1:
imageId = imageId[:imageId.find('?')]
elif imageId.find('#') != -1:
imageId = imageId[:imageId.find('#')]
if not os.path.exists(path + imageId):
htmlSource = urllib2.urlopen(req, timeout=5).read()
with open(path + imageId, "wb") as f:
f.write(htmlSource)
return imageId
except Exception, error:
print error, link
return 0
def getNewsLinkFromSource(self, present, category, sourceTitle, newsSourceLink, newsSourceDiffWords, newsSourceBlackWords):
"""
Verilen link icin kategoriye uygun haber linklerini cikarip en cok paylasim sayisina
ve bugunun tarine sahip olanlari liste olarak geri doner
"""
class BreakIt(Exception): pass
#=======================================================================
# Link source aliniyor
#=======================================================================
try:
self.encodePageSource.index(sourceTitle)
try:
htmlSource = requests.get(newsSourceLink, headers={'User-Agent' : self.userAgent}, timeout=10).text
except:
time.sleep(15)
try:
htmlSource = requests.get(newsSourceLink, headers={'User-Agent' : self.userAgent}, timeout=10).text
except Exception, error:
return []
except:
req = urllib2.Request(newsSourceLink, headers={'User-Agent' : self.userAgent})
try:
htmlSource = urllib2.urlopen(req, timeout=10).read()
except:
time.sleep(15)
try:
htmlSource = urllib2.urlopen(req, timeout=10).read()
except Exception, error:
return []
#=======================================================================
# Source alindiysa link analiz islemi basliyor
#=======================================================================
if htmlSource != -1:
#===================================================================
# #Verilen kaynak link'den (anasayfa) tum linkler aliniyor
#===================================================================
linkList = []
soup = BeautifulSoup(htmlSource)
for link in soup.findAll('a'):
href = link.get('href')
try:
if len(href) > 8:
flagGet = True
for keyword in self.notGetLinkIfContainThisKeyword:
if href.find(keyword) != -1:
flagGet = False
break
if flagGet:
linkList.append(href)
except:
pass
#===================================================================
# #Linklerin argumanlari atilip sade hale getiriliyor
#===================================================================
newLinkList = []
for link in linkList:
newLink = link.strip()
if len(newLink) > 3:
if newLink[5:].find('#') != -1:
newLink = newLink[:newLink.find('#')]
if newLink[5:].find(';') != -1:
newLink = newLink[:newLink.find(';')]
#Linklerin icin haberi belirleyen ?'den sonraki kisim ise bu islem yapilmaz
try:
self.containQuestionCharacter.index(newsSourceLink)
except:
if newLink[5:].find('?') != -1:
newLink = newLink[:newLink.find('?')]
#Asagidaki siteler'de sondaki / karakteri silinmez
try:
self.notDeletedBackslashCharacter.index(newsSourceLink)
except:
if newLink[-1] == '/':
newLink = newLink[:-1]
#Link listeye alinir
newLinkList.append(newLink)
#===================================================================
# Anasayfada olmayan ama database'de bugun yayimlanan linkleri ekle
#===================================================================
firstData = ""
try:
firstData = self.serverHandler.executeQuery("SELECT link FROM `links_%s` WHERE category='%s' AND source='%s' AND date='%s'"%(self.yearMonth, category, sourceTitle, str(present.strftime('%Y-%m-%d'))))
except:
self.serverHandler.executeQuery("""CREATE TABLE IF NOT EXISTS `links_%s` (
`id` int(11) NOT NULL,
`date` date NOT NULL,
`category` varchar(32) NOT NULL,
`source` varchar(255) NOT NULL,
`link` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`order` int(11) NOT NULL,
`tweetCount` int(11) NOT NULL,
`facebookCount` int(11) NOT NULL,
`googleCount` int(11) NOT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(1024) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`imgLink` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`clickedCount` int(11) NOT NULL DEFAULT '0',
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateFlag` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"""%(self.yearMonth))
time.sleep(3)
self.serverHandler.executeQuery("ALTER TABLE `links_%s` ADD PRIMARY KEY (`id`)"%self.yearMonth)
time.sleep(3)
self.serverHandler.executeQuery("ALTER TABLE `links_%s` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT"%self.yearMonth)
#Database'deki bu kaynaga ait diger
#datalar aliniyor
for link in firstData:
newLinkList.append(link[0])
#Anasayfa olmayan ama database'de source icin kayitli diger link'lerle ilgili bilgi
#Duplicate linkleri temizle
tmp = set(newLinkList)
linkList = list(tmp)
#En fazla limit link'e bak
if len(linkList) > self.getLinkCountLimit:
linkList = linkList[:self.getLinkCountLimit]
#============================================
#Kaynaktaki tum alinan linklere bakilir
#============================================
returnLinkDict = {}
countDatabase = 0
countHtmlSource = 0
notGetLinkFlag = False
self.logHandler.printMsg("Total Link Count: %d"%len(linkList), 2)
for link in linkList:
linkData = []
#Link var ise
if not link:
continue
#Turkce karakter temizle
for key in self.turkishDict:
link = link.replace(key, self.turkishDict[key])
#Ana source link duzenleme
sourceLink = newsSourceLink
if newsSourceLink[len('http://'):].find('/') != -1:
sourceLink = sourceLink[:sourceLink[len('http://'):].find('/') + len('http://')]
#Eger link icinde sourceTitle yok ise ve alttaki keyword'lardan biri var ise alakasi bir link gec
#Exm: https://itunes.apple.com.tr
if link.find(sourceTitle) == -1 and (link.find('http://') != -1 or link.find('www.') != -1 or link.find('https://') != -1):
continue
if link.count('http://') > 1 or (link.count('http://') > 0 and link.count('https://') > 0) or link.count('www.') > 1:
continue
#Eger link'de hostname yok ise ekle, link'i duzenle
if link.find(sourceTitle) == -1:
if link[0] == '/':
link = sourceLink + link
else:
link = sourceLink + '/' + link
else:
if link[0] != 'w' and link[0] != 'h':
link = 'http://' + link[2:]
#Domain yok ise linki alma baska sitenin linkidir
if link.find(sourceTitle) == -1:
continue
#Black word'ler varsa linkde gec
if newsSourceBlackWords:
flag = False
for word in newsSourceBlackWords:
if link.find(word) != -1:
flag = True
break
if flag:
continue
#Linkleri ayiraca gore ayikla, uygun olmayanlari alma
if newsSourceDiffWords:
flag = True
for word in newsSourceDiffWords:
if link.find(word) != -1:
flag = False
break
if flag:
continue
if self.showLink:
self.logHandler.printMsg(link, 3)
#===============================================================
# Link links_* database'de var ise tekrar LinkHandlerr cagrilmaz, eger ana sayfada ise meta etiketleri kontrol icin cagrilir
#===============================================================
# Injection onleme
linkInj = link.replace("'", "''")
try:
linkData = self.serverHandler.executeQuery("SELECT date, title, description, imgLink FROM `links_%s` WHERE `link`='%s'"%(self.yearMonth, linkInj))
except:
self.logHandler.logger("getNewsLinkFromSource")
continue
if linkData:
#Link tarihi database'de olan bugun ise database'den bilgileri al
if str(linkData[0][0]) == str(present.strftime('%Y-%m-%d')):
countDatabase += 1
linkData = linkData[0]
#Eger link ana sayfada ise meta tag'ler kontrol edilir
linksAllMainPage = []
linksAllMainPage.extend(list(self.serverHandler.executeQuery("SELECT imgLink, title, link, tweetCount, facebookCount, googleCount, category, description, source, id FROM `links_%s` WHERE `date`='%s' AND category = 'gundem' AND tweetCount+facebookCount+googleCount > 5 ORDER BY id DESC LIMIT 0, 12"%(self.yearMonth, present))))
limitMozaikCount = 25
linksAllMainPage.extend(list(self.serverHandler.executeQuery("SELECT imgLink, title, link, tweetCount, facebookCount, googleCount, category, description, source FROM (SELECT imgLink, title, link, tweetCount, facebookCount, googleCount, category, description, source FROM (SELECT imgLink, title, link, `tweetCount`, `facebookCount`, `googleCount`, category, description, source FROM `links_%s` WHERE `date`='%s' AND category = '%s') AS `NEWTABLE` WHERE category='gundem' ORDER BY `tweetCount`+`facebookCount`+`googleCount` DESC LIMIT 0, %d) AS NEWTABLE"%(self.yearMonth, present, category, limitMozaikCount))))
limitCategoryCount = 8
linksAllMainPage.extend(list(self.serverHandler.executeQuery("SELECT imgLink, title, link, tweetCount, facebookCount, googleCount, category, description, source FROM (SELECT imgLink, title, link, tweetCount, facebookCount, googleCount, category, description, source FROM (SELECT imgLink, title, link, `tweetCount`, `facebookCount`, `googleCount`, category, description, source FROM `links_%s` WHERE `date`='%s' AND category = '%s') AS `NEWTABLE` ORDER BY `tweetCount`+`facebookCount`+`googleCount` DESC LIMIT 0, %d) AS NEWTABLE"%(self.yearMonth, present, category, limitCategoryCount))))
for data in linksAllMainPage:
lnMainPage = data[2]
if lnMainPage != link:
returnLinkDict[link] = (linkData[1], linkData[2], linkData[3])
continue
#===========================================================================================
#Eger link database'de var ise tarihine bakilir bugun ise alinir yoksa gecilir sonraki linke
#===========================================================================================
try:
linkDate = self.serverHandler.executeQuery("SELECT date FROM `tempLinks` WHERE `link`='%s'"%linkInj)
except:
self.serverHandler.executeQuery("""
CREATE TABLE IF NOT EXISTS `tempLinks` (
`id` int(11) NOT NULL,
`date` date NOT NULL,
`link` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;""")
time.sleep(3)
self.serverHandler.executeQuery("ALTER TABLE `tempLinks` ADD PRIMARY KEY (`id`)")
time.sleep(3)
self.serverHandler.executeQuery("ALTER TABLE `tempLinks` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT")
linkDate = self.serverHandler.executeQuery("SELECT date FROM `tempLinks` WHERE `link`='%s'"%linkInj)
if linkDate:
if str(linkDate[0][0]) != str(present.strftime('%Y-%m-%d')):
continue
#===========================================================
# #Timeout olur diye 2 defa deneyecek
#===========================================================
count = 2
countHtmlSource += 1
flagContinue = False
while count:
getLinkHandler= ""
#=======================================================
# Link'ler icin ozel LinkHandlerr argumanlari
#=======================================================
imageClass = ""
requestType = ""
cleanBrokenCh = ""
descMetaType = ""
try:
imageClass = self.imageClassForSources[newsSourceLink]
except:
pass
try:
self.requestTypes.index(sourceTitle)
requestType = 1
except:
requestType = 0
try:
self.contentTitleDescReplace.index(sourceTitle)
cleanBrokenCh = True
except:
cleanBrokenCh = False
try:
descMetaType = self.descMetaTypes[newsSourceLink]
except:
descMetaType = ""
getLinkHandler = LinkHandler(link, requestType=requestType, imageClass=imageClass, descMetaType=descMetaType, cleanBrokenCh=cleanBrokenCh)
getLinkHandler.run()
soup = getLinkHandler.soup
#Source gelmez ise link'i gec
if soup == -1:
time.sleep(3)
count -= 1
else:
break
if count == 0:
flagContinue = True
break
if flagContinue:
continue
#===========================================================
# Bugunun tarihi arastiriliyor link htmlSource'unda
# Varsa bugunun tarihi returnLinkList'e ekleniyor
#===========================================================
try:
for categories in self.newsSources.values():
for source in categories:
if source[1] == newsSourceLink:
# -'den oncenin alinmasi
# Eger dashCount' kadari title'da var ise temizle
title = getLinkHandler.titleContent
desc = getLinkHandler.descContent
img = getLinkHandler.imgContent
#Tanim yok ise title atanir
if len(desc) < 5:
desc = title
#Gormek istemedigin linkleri gec #imageLink'i icinde bulunanlar sadece
for src, lnnk in self.blackListLinkImage.iteritems():
if src == sourceTitle :
for i in lnnk:
if img.find(i) != -1:
notGetLinkFlag = True
break
if notGetLinkFlag:
notGetLinkFlag = False
continue
if title.find(''') != -1:
title = title.replace(''', "'")
if title.find('"') != -1:
title = title.replace('"', '"')
if desc.find(''') != -1:
desc = desc.replace(''', "'")
if desc.find('"') != -1:
desc = desc.replace('"', "'")
#Title silme islemleri
try:
if len(source) > 5:
seperator = source[5].keys()[0]
seperatorCount = source[5].values()[0]
if title.count(seperator) >= seperatorCount:
for i in range(seperatorCount):
if title.rfind(seperator) > 20:
title = title[:title.rfind(seperator)].strip()
except:
self.logHandler.logger("getNewsLinkFromSource", "Title replace")
try:
if len(source) > 6:
seperator = source[6]
desc = desc.split(seperator)[0]
except:
self.logHandler.logger("getNewsLinkFromSource", "Desc replace")
#link'de 2013/09/09 sekli icin, 0 ise direk al
if source[4] == 0:
returnLinkDict[link] = (title, desc, img)
elif source[4] == 1:
todayFormat = source[2][0]
if link.find(todayFormat) != -1:
returnLinkDict[link] = (title, desc, img)
elif source[4] == 2:
todayFormat = source[2][0]
monthDay = todayFormat[4:]
if link.find(monthDay) != -1:
returnLinkDict[link] = (title, desc, img)
else:
for dateFormats in source[4]:
dateKeyword = dateFormats
dateClass = dateKeyword.keys()[0]
dateToday = dateKeyword.values()[0]
soupDateDiv = soup.findAll("div", { "class" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("meta", { "itemprop" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("div", { "id" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("span", { "class" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("span", { "style" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("time", { "class" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("div", { "style" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("span", { "id" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("p", { "class" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("div", { "itemprop" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("li", { "class" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("p", { "itemprop" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("time", { "itemprop" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll("span", { "itemprop" : "%s"%dateClass})
if not soupDateDiv:
soupDateDiv = soup.findAll(dateClass)
if self.showLink:
print "\t",dateKeyword
print "\t", soupDateDiv
#Tarihin string olarak aliniyor
findedDate = ""
for item in soupDateDiv:
findedDate = str(item.text)
if not findedDate:
try:
findedDate = item['value']
except:
pass
if findedDate.find('önce') != -1:
try:
findedDate = soupDateDiv[0]["title"]
except:
pass
for i in soupDateDiv:
try:
if i.getText("title").find("saat") != -1:
findedDate = str(present.strftime('%d.%m.%Y'))
break
except:
findedDate = ""
#Bir tane keyword bulundu ama istenen degil tarih olup olmadigi kontrol edilir
if len(findedDate) == 0:
continue
findedDate = findedDate.replace('sı', 'ı')
if findedDate.find(str(datetime.now().year)) == -1:
continue
break
if not findedDate:
continue
#Eger linkDate yoksa ekle 1 gun oncesi olarak
#Eger bugunun link'i ise asagida duzeltilir
if not linkDate:
self.serverHandler.executeQuery("INSERT INTO `tempLinks` VALUES(NULL, CURRENT_DATE()-1, '%s')"%linkInj)
try:
dateText = findedDate
#Html ü karakteri
dateText = dateText.replace('ü', 'ü')
dateText = dateText.replace('&#305;', 'ı')
dateText = dateText.replace('ü', 'ü')
dateText = dateText.replace('&#350;', 'Ş')
#===============================
# Bazi tarihlerde guncelleme var onu almasin diye
#===============================
if dateText[10:].find("Güncelleme") != -1:
firstIndex = dateText.find("Güncelleme")
endIndex = firstIndex + len('Güncelleme') + 10
dateText = dateText[:firstIndex] + dateText[endIndex:]
if dateText.find(dateToday) != -1:
#Eger linkDate yoksa ekle
self.serverHandler.executeQuery("UPDATE `tempLinks` SET date=CURRENT_DATE() WHERE link='%s'"%linkInj)
returnLinkDict[link] = (title, desc, img)
else:
if self.showLink:
print "Link bugunun degil: | %s | %s |"%(dateText, dateToday)
break
except AttributeError, error:
print error
continue
raise BreakIt
except BreakIt:
continue
#Duplicate linkleri temizle
tmp = set(returnLinkDict)
returnLinkList = list(tmp)
#===================================================================
# Twitter ve Facebook count'lari alinip gerekli tum seyler donduruluyor
#===================================================================
self.logHandler.printMsg("Link parse locate (Database / Source) - ( %d / %d)"%(countDatabase, countHtmlSource), 2)
self.logHandler.printMsg("Share count : %d"%len(returnLinkList), 2)
linkCountDict = self.getSocialCount(category, sourceTitle, returnLinkList, returnLinkDict)
choiceList = {}
for i in range(0, len(linkCountDict)):
try:
maxCountLink = max(linkCountDict, key=linkCountDict.get)
choiceList[maxCountLink] = linkCountDict[maxCountLink]
del linkCountDict[maxCountLink]
except:
self.logHandler.logger('getNewsLinkFromSource', "Hic sayfa gelmedi: %s"%newsSourceLink)
break
return choiceList
def getSocialCount(self, category, sourceTitle, returnLinkList, returnLinkDict):
linkCountDict = {}
for link in returnLinkList:
if self.showLink:
self.logHandler.printMsg(link, 3)
#Facebook count
linkForCount = link.replace("'", "''")
try:
#ProxyHandler
#proxy = urllib2.ProxyHandler({'http': '89.185.235.15'})
#opener = urllib2.build_opener(proxy)
#html = opener.open('http://api.facebook.com/method/fql.query?query=select%%20total_count%%20from%%20link_stat%%20where%%20url="%s"'%linkForCount, timeout=5).read()
try:
self.linkContentComma.index(sourceTitle)
html = urllib2.urlopen('http://api.facebook.com/method/fql.query?query=select%%20total_count%%20from%%20link_stat%%20where%%20url="%s"'%linkForCount, timeout=5).read()
indexCount = html.find('<total_count>') + len('<total_count>')
countFacebook = int(html[indexCount : html[indexCount:].find('</') + indexCount])
except:
html = urllib2.urlopen('http://api.facebook.com/method/links.getStats?urls=%s&format=json'%linkForCount, timeout=5).read()
countFacebook = json.loads(html)[0]['total_count']
except Exception, error:
self.logHandler.printMsg("Link count-error(facebook)", 3)
try:
countFacebook = self.serverHandler.executeQuery("SELECT facebookCount FROM `links_%s` WHERE link='%s'"%(self.yearMonth, linkForCount))[0][0]
except:
self.logHandler.printMsg("Link count alinamadi: %s"%link, 3)
countFacebook = 0
#Twitter count
try:
try:
#degeri 0 ise www eklenir 1 ise www silinir
value = self.getTweetCountFix[sourceTitle]
if value == 0:
if link.find('www.') == -1:
newLink = link.replace("http://", "http://www.")
linkForCount = urllib.quote_plus(newLink)
elif value == 1:
if link.find('www.') != -1:
newLink = link.replace("http://www.", "http://")
linkForCount = urllib.quote_plus(newLink)
elif value == 2:
if link.find('www.') != -1:
newLink = link.replace("http://www.", "")
linkForCount = urllib.quote_plus(newLink)
except:
linkForCount = urllib.quote_plus(link)
except:
self.logHandler.printMsg("Link url not converted: %s"%link, 3)
try:
html = urllib2.urlopen("http://urls.api.twitter.com/1/urls/count.json?url=%s"%linkForCount, timeout=5).read()
indexCount = html.find('"count":') + len('"count":')
countTwitter = int(html[indexCount : html[indexCount:].find(',') + indexCount])
except:
self.logHandler.printMsg("Link count-error(twitter)", 3)
try:
countTwitter = self.serverHandler.executeQuery("SELECT tweetCount FROM `links_%s` WHERE link='%s'"%(self.yearMonth, linkForCount))[0][0]
except:
self.logHandler.printMsg("Link count alinamadi: %s"%link, 3)
countTwitter = 0
#GooglePlus count
try:
data = '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"%s","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' % link
url = "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ""
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req, timeout=5)
response = f.read()
f.close()
result = json.loads(response)
countGoogle = int(result[0]['result']['metadata']['globalCounts']['count'])
except:
self.logHandler.printMsg("Link count-erro(googleplus)", 3)
try:
countGoogle = self.serverHandler.executeQuery("SELECT googleCount FROM `links_%s` WHERE link='%s'"%(self.yearMonth, linkForCount))[0][0]
except:
self.logHandler.printMsg("Link count alinamadi: %s"%link, 3)
countGoogle = 0
#===================================================================
# Ozel Durumlar
#===================================================================
if countFacebook > 2000 and countTwitter < 100:
countGoogle = 0
countFacebook = 0
linkTitle = returnLinkDict[link][0]
linkDesc = returnLinkDict[link][1]
linkImage = returnLinkDict[link][2]
#Gundem'de hic paylasim olmayan link'ler gecilir
#00'dan sonra taraf, sabah gazetelerinin tum linkleri yeni gune yenilemesiden dolayi
if countFacebook == 0 and countTwitter == 0 and countGoogle == 0:
continue
linkCountDict[link] = (countTwitter + countFacebook + countGoogle, countTwitter, countFacebook, countGoogle, category, sourceTitle, linkTitle, linkDesc, linkImage)
#LinkDatabase'de var ise count'lari update et
self.serverHandler.executeQuery("UPDATE `links_%s` SET tweetCount=%d, facebookCount=%d, googleCount=%d WHERE `link`='%s'"%(self.yearMonth, countTwitter, countFacebook, countGoogle, link))
return linkCountDict
def isAscii(self, s):
return all(ord(c) < 128 for c in s)
def insertToDatabase(self, category, newsLinkDict, limit):
"""
Verilen link listesindeki haber linklerini table icin uygun formatta geri dondurur
"""
self.logHandler.printMsg("Insert To Database", 1)
self.logHandler.printMsg("---------------------", 1)
newsResult = ""
try:
index = 1
while len(newsLinkDict):
try:
#100 haber al
if index >= limit:
break
maxCountLink = max(newsLinkDict, key=newsLinkDict.get)
linkTitle = newsLinkDict[maxCountLink][6]
linkDesc = newsLinkDict[maxCountLink][7]
linkImage = newsLinkDict[maxCountLink][8]
#Eger kaynak icin getGoogleImage'de tanimli ise resim site yerine
#Google'dan alinir, link database'de var ise tekrar aranmaz google api limit'i
#asmamak icin
for sourceHttp in self.getGoogleImageList:
if maxCountLink.find(sourceHttp) != -1:
try:
self.serverHandler.executeQuery("SELECT imgLink FROM `links_%s` WHERE link='%s'"%(self.yearMonth, maxCountLink.replace("'", "''")))[0][0]
except:
imageName = self.multipleReplace(linkTitle, self.turkishDict)
imageName = '%20'.join(linkTitle.split()[:4])
imgLinkGoogle = self.getGoogleImage(linkTitle)
if imgLinkGoogle:
linkImage = imgLinkGoogle
source = newsLinkDict[maxCountLink][5]
#Description limit
try:
chIndex = 350
if len(linkDesc) >= 351:
count = 10
while count:
try:
if self.isAscii(linkDesc[chIndex]):
linkDesc = linkDesc[:chIndex] + '...'
break
count -= 1
chIndex += 1
except:
count -= 1
chIndex = 340
continue
except:
self.logHandler.logger("generateHtmlFormat: %s"%maxCountLink)
#===========================================================
# #Image dogrulama
#===========================================================
imageId = linkImage[linkImage.rfind('/') + 1:]
if self.showLink:
self.logHandler.printMsg(maxCountLink, 3)
self.logHandler.printMsg("Image: " + imageId, 3)
if imageId:
#Title var ise al
if linkTitle:
maxCountLinkInj = maxCountLink.replace("'", "''")
try:
searchLink = maxCountLinkInj.replace('http://', '')
searchLink = searchLink.replace('www.', '')
linkOrder = self.serverHandler.executeQuery("SELECT `order` FROM `links_%s` WHERE `link` LIKE '%%%s%%'"%(self.yearMonth, searchLink))
except:
self.logHandler.logger("insertToDatabase")
index -= 1
del newsLinkDict[maxCountLink]
continue
imgSrc = ""
if linkImage.find('imageslink/') == -1:
#Hotlink ise yani indirilecek
try:
self.hotlinks.index(source)
#DownloadImage var ise tekrar indirmez
imageId = self.downloadImage(source, linkImage, self.linkImagePath)
if imageId == 0:
#Hotimage link indirlememisse google'da ara yok ise default kullan
imgLinkGoogle = self.getGoogleImage(linkTitle)
if imgLinkGoogle:
imgSrc = imgLinkGoogle
else:
imgSrc = 'imageslink/default.png'
else:
imgSrc = self.linkImagePath.split('/')[-2] + '/' + imageId
except:
if linkImage.find('.gif') != -1:
#Hotimage link indirlememisse google'da ara yok ise default kullan
imgLinkGoogle = self.getGoogleImage(linkTitle)
if imgLinkGoogle:
imgSrc = imgLinkGoogle
else:
imgSrc = linkImage
else:
imgSrc = linkImage
else:
imgSrc = linkImage
#===============================================
# Ozel link degisiklikleri
#===============================================
for srcLink, replaceTuple in self.replaceStringForLink.iteritems():
if srcLink == source:
imgSrc = imgSrc.replace(replaceTuple[0], replaceTuple[1])
#Linkler database kaydediliyor
#Var ise update yapiliyor
linkTitle = linkTitle.replace("'", "''")
linkTitle = linkTitle.replace("\\", "")
linkDesc = linkDesc.replace("'", "''")
linkDesc = linkDesc.replace("\\", "")
#Desc bozuk ise desc'e title'i ata