קוד: |
import sys, urllib2
from BeautifulSoup import BeautifulSoup term = sys.argv[1] url = 'http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word='+term code = BeautifulSoup(urllib2.urlopen(url).read()) print code.div.prettify().replace('<div align="right" dir="rtl">\n<font color="BLUE">\n ', '').replace('\n', '').replace('</font>', ' ').replace('<br /><br /></div>', '').replace('<hr /><font color="BLUE"> ', '\n') |
קוד: |
bjesus@tilon ~/projects/pytrans % ./pytrans.py hello | iconv -f utf8 -t windows-1252 -c | iconv -f iso88598 -t utf8
(מ"ק) הלו, היי (ברכת שלום, ברכה בזמן פגישה); הלו! (קריאה למשוך תשומת-לב) (ש"ע) ברכת שלום, שלום, הלו |
קוד: |
#!/usr/bin/env python
# Pytrans is a frontend to babylon # 0.2 import sys, urllib2, codecs from BeautifulSoup import BeautifulSoup term = sys.argv[1] url = 'http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word='+term code = BeautifulSoup(urllib2.urlopen(url).read()) result = code.div.prettify().replace('<div align="right" dir="rtl">\n<font color="BLUE">\n ', '').replace('\n', '').replace('</font>', ' ').replace('<br /><br /></div>', '').replace('<hr /><font color="BLUE"> ', '\n').decode('utf8').encode('windows-1252').decode('iso8859-8').encode('utf8') print result |
קוד: |
#!/usr/bin/env python
# Pytrans is a frontend to babylon # 0.5 import sys, urllib2 from BeautifulSoup import BeautifulSoup babylon='http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word=' def translate(term): url=babylon+term code=BeautifulSoup(urllib2.urlopen(url).read()) try: result = code.div.prettify().replace( '<div align="right" dir="rtl">\n<font color="BLUE">\n ', '').replace( '\n', '').replace('</font>', ' ').replace('<br /><br /></div>', '' ).replace('<hr /><font color="BLUE"> ', '\n').decode('utf8').encode( 'windows-1252').decode('iso8859-8').encode('utf8') except: result = "no translation found" return result result = "" for term in sys.argv[1:]: result = result + term + "\n" result = result + translate(term) + "\n" print result |
קוד: |
./pytrans.py hello world | xargs -0I 1 notify-send 1 |
קוד: |
#!/usr/bin/env python # -*- coding: utf-8 -*- from urllib2 import urlopen from sys import argv from BeautifulSoup import BeautifulSoup en_base_url = "http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word='" def translate(term): search_url = "%s%s" % (en_base_url, term) page = BeautifulSoup(urlopen(search_url).read()) try: result = page.div.prettify().replace('<div align="right" dir="rtl">\n<font color="BLUE">\n ', '') result = result.replace('\n', '') result = result.replace('</font>', ' ') result = result.replace('<br /><br /></div>', '') result = result.replace('<hr /><font color="BLUE"> ', '\n') result = result.decode('utf8').encode('windows-1252').decode('iso8859-8').encode('utf8') return result except: return None if __name__ == "__main__": for term in argv[1:]: output = translate(term) if output: print "%s:\n%s" % (term, output) else: print "No translation found for the term %s" % term |
קוד: |
xclip -o | xargs -I $ ./pytrans.py $ | xargs -0I 1 notify-send 1 |
חמוס במשקפיים : |
והנה מגיעות הבקשות.
הסקריפט כבר התחיל לעזור לי. שני דברים קטנים שהייתי רוצה: 1. ניקוי התגיות לא מושלם עדיין. כשמבקשים את ההגדרה למחרוזת faq, למשל, מקבלים כמה תגיות html-יות. 2. הודעות השגיאה לא מושלמות עדיין. כאשר מחפשים מילה אנגלית לא מוכרת מקבלים הודעת שגיאה, אבל כאשר מחפשים צירוף לא מוכר או מילה עברית לא מקבלים כלום. הייתי רוצה לקבל משוב כל פעם שאני מפעיל את הסקריפט. אגב, הייתי שמח גם לקבל שגיאה מיוחדת אם אין שום טקסט ב-x selection, אבל זו כבר באמת התקטננות. |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 -*- from urllib2 import urlopen from sys import argv from BeautifulSoup import BeautifulSoup en_base_url = "http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word='" def translate(term): search_url = "%s%s" % (en_base_url, term) page = BeautifulSoup(urlopen(search_url).read()) try: result = page.div.prettify().replace('\n<font color="BLUE">\n ', '') result = result.replace('\n', '') result = result.replace('</font>', ' ') result = result.replace('<br /><br /></div>', '') result = result.replace('<div align="right" dir="rtl">', '') result = result.replace('<hr /><font color="BLUE"> ', '\n') result = result.decode('utf8').encode('windows-1252').decode('iso8859-8').encode('utf8') return result except: return None if __name__ == "__main__": for term in argv[1:]: output = translate(term) if output: print "%s:\n%s" % (term, output) else: print "No translation found for the term %s" % term |
קוד: |
#!/usr/bin/env python # -*- coding: utf-8 - # # Pytrans is a frontend to babylon # 0.8 from urllib2 import urlopen from sys import argv from BeautifulSoup import BeautifulSoup en_base_url = "http://info.babylon.com/cgi-bin/info.cgi?s=educaterra&lang=14&word='" def translate(term): search_url = "%s%s" % (en_base_url, term) page = BeautifulSoup(urlopen(search_url).read()) try: result = page.div.prettify().replace('\n<font color="BLUE">\n ', '') result = result.replace('\n', '') result = result.replace('</font>', ' ') result = result.replace('<br /><br /></div>', '') result = result.replace('<div align="right" dir="rtl">', '') result = result.replace('<hr />', '\n') result = result.decode('utf8').encode('windows-1252').decode('iso8859-8').encode('utf8') return result except: return None if __name__ == "__main__": for term in argv[1:]: output = translate(term) if output: print "%s:\n%s" % (term, output) else: print "No translation found for the term %s" % term if len(argv) == 1: print "What do you want to translate?" |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 0.9 from urllib2 import urlopen from urllib import urlencode from sys import argv from re import compile from BeautifulSoup import BeautifulSoup base_url = "http://translation.babylon.com/post_post.php" english = compile("[A-z]") def translate(term): if english.search(term): params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) page = BeautifulSoup(urlopen(base_url, params).read()) try: result = "" for div in page.findAll("div", {"class": "definition"}): try: result = result + div.span.span.renderContents().replace("\n","") except: result = result + div.span.renderContents().replace("\n","") try: result = result + div.span.nextSibling.renderContents().replace("\n","") except: result = result + div.span.nextSibling.replace("\n","") result = result + "\n" result = result.replace(" ","") return result except: return None if __name__ == "__main__": for term in argv[1:]: output = translate(term) if output: print term print output else: print "No translation found for the term '%s'" % term if len(argv) == 1: print "What do you want to translate?" |
קוד: |
No translation found for the term 'u05d9u05e9' |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 1.0 from urllib2 import urlopen from urllib import urlencode from sys import argv from re import compile, sub base_url = "http://translation.babylon.com/post_post.php" english = compile("[A-z]") def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def translate(term): if english.search(term): params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) page = urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") if __name__ == "__main__": if len(argv) == 1: print "What do you want to translate?" exit() term = " ".join(argv[1:]) output = translate(term) print term print output |
קוד: |
xclip -o | xargs -I $ python pytrans.py $ | xargs -0I 1 notify-send 1 |
קוד: |
yy@yy-desktop:~$ xclip -o | xargs -I $ ./pytrans.py $ | xargs -0I 1 notify-send 1 from: can't read /var/mail/urllib2 from: can't read /var/mail/urllib from: can't read /var/mail/sys from: can't read /var/mail/re ./pytrans.py: line 12: base_url: command not found ./pytrans.py: line 13: syntax error near unexpected token `(' ./pytrans.py: line 13: `english = compile("[A-z]")' |
קוד: |
/usr/bin/env python |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 1.1 from urllib2 import urlopen from urllib import urlencode from sys import argv base_url = "http://translation.babylon.com/post_post.php" def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def translate(term): if term[0] == '\xd7': params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) elif term[0] in ['\xd9','\xd8']: params = urlencode(dict(lps=15, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) page = urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") if __name__ == "__main__": if len(argv) == 1: print "What do you want to translate?" exit() term = " ".join(argv[1:]) output = translate(term) print term print output |
קוד: |
import gtk clipboard = gtk.clipboard_get(selection = "PRIMARY") term = clipboard.wait_for_text() |
yp : |
תודה...מה שקרה זה שהיה שורה רווח בתחילת הקוד
כנראה מקופי פסט שעשיתי מהאתר ,לא חשבתי שזה יכול לגרום לבעיות ,איזה ילד אני השלב הבא זה לנסות למצוא פעולה יותר נוחה להפעלת הסקריפט ,חשבתי משו עם העכבר שוב תודה ,באמת סקריפט מוצלח ,ועובד מהר בצורה די מפתיעה |
חמוס_במשקפיים : |
אוקיי - יש! האח הידד!
צריך להשתמש בגירסה 0.11 ומעלה של xclip כדי שהתרגום מעברית יעבוד, ואין צורך בשום פרמטר נוסף. אנשי פדורה החביבים העלו פאץ' שמומש, סוף סוף, באפסטרים. ניתן להוריד מכאן: http://sourceforge.net/projects/xclip ולהדר. צריך להתקין את חבילות הפיתוח של xorg. באובונטו, למרבית הצער, גירסת xclip אינה עולה 0.8. אני אנסה לבקש יפה בפורומים שלהם לייבא את הגירסה החדשה לפחות לג'אונטי. |
קוד: |
./configure |
iceman23 : | ||||
אפשר בבקשה הסבר לאיך להתקין xclip 0.11 הורדתי והסתבכתי.. אחרי
אני לא מצליח לעשות לא make ולא make install אני יודע שאני מפספס משהו.. תודה |
iceman23 : | ||||
אפשר בבקשה הסבר לאיך להתקין xclip 0.11 הורדתי והסתבכתי.. אחרי
אני לא מצליח לעשות לא make ולא make install אני יודע שאני מפספס משהו.. תודה |
חמוס_במשקפיים : |
אייסמן, ככל הנראה חסרה לך החבילה xorg-dev. כמו כן וודא שמותקנת אצלך החבילה build-essentials. |
bjesus : |
זה לא אמור להיות מסובך בכלל, אבל - למה? מה הוא מציע שאין בממשק של בבילון? |
Ddorda : | ||
|
bjesus : |
זה לא אמור להיות מסובך בכלל, אבל - למה? מה הוא מציע שאין בממשק של בבילון? |
קוד: |
import pygtk pygtk.require('2.0') import gtk, gobject import urllib2 from urllib import urlencode import re base_url = "http://translation.babylon.com/post_post.php" def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) class gtranslator: def translate(self,term): if term[0] == '\xd7': params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) elif term[0] in ['\xd9','\xd8']: params = urlencode(dict(lps=15, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) page = urllib2.urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") def fetch_clipboard_info(self): self.clipboard.request_text(self.clipboard_text_received) return True def clipboard_text_received(self, clipboard, text, data): if not text or text == '': return if self.term==text: return self.textentry.set_text (text) t=self.translate (text) self.textbuffer.set_text (t) self.term=text def enter_callback (self, widget, entry): term=entry.get_text () if term=='': return t=self.translate (term) self.textbuffer.set_text (t) self.term=term def __init__(self): self.window = gtk.Window() self.window.set_title("Babylon") self.window.connect("destroy", lambda w: gtk.main_quit()) vbox = gtk.VBox() vbox.show() self.textentry = gtk.Entry() self.textentry.connect("activate", self.enter_callback, self.textentry) self.textentry.show() vbox.pack_start(self.textentry) scrolledwin = gtk.ScrolledWindow() scrolledwin.show() self.textview = gtk.TextView() self.textview.show() self.textview.set_size_request(275,150) self.textview.set_wrap_mode(gtk.WRAP_CHAR) self.textbuffer = self.textview.get_buffer() scrolledwin.add(self.textview) vbox.pack_start(scrolledwin) self.window.add(vbox) self.window.show() self.clipboard = gtk.Clipboard(display=gtk.gdk.display_get_default(), selection="PRIMARY") self.clipboard.request_text(self.clipboard_text_received) self.term = '' self.clipboard.wait_for_text() def main(): gtrans = gtranslator () gtk.main() return 0 if __name__ == '__main__': main() |
קוד: |
/home/yotam/Software/scripts/pytrans.py: line 2: syntax error near unexpected token `'2.0'' /home/yotam/Software/scripts/pytrans.py: line 2: `pygtk.require('2.0')' |
קוד: |
xclip -o | xargs -I $ ./pytrans.py $ |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 1.2 # Change from 1.1: using pynotify to send a system notification instead of just printing the result. Requires pynotify from urllib2 import urlopen from urllib import urlencode from sys import argv import pynotify base_url = "http://translation.babylon.com/post_post.php" def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def translate(term): if term[0] == '\xd7': params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) elif term[0] in ['\xd9','\xd8']: params = urlencode(dict(lps=15, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) page = urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") if __name__ == "__main__": if len(argv) == 1: print "What do you want to translate?" exit() term = str(" ".join(argv[1:])) translation = str(translate(term)) if not pynotify.init("Basics"): sys.exit(1) n = pynotify.Notification(term, '\n' + translation) if not n.show(): print "Failed to send notification" sys.exit(1) |
queency : |
אשמח לקבל הסבר מה זה בדיוק הפקודה הבאה
xclip -o | xargs -I $ ./pytrans.py $ זה דוגמת הרצה בווינדוס ? או דוגמת הרצה דרך X ? אם זה דרך X איך עושים את זה ? אצלי אני מריץ דרך טרמינל BASH רושם PYTHON ואת שם הקובץ וארגומנט עכשיו עם הודעת המערכת הוא גם סידר לי מימין לשמאל שלא מסודר בטרמינל לא רע אפילו מGNUV |
קוד: |
#!/usr/bin/env python
# -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 1.3 # Change from 1.1: using pynotify to send a system notification instead of just printing the result. Requires pynotify # Change from 1.2: using xsel in-line so no need for complex activation line - just run the script. also added notification when no text is selected. from urllib2 import urlopen from urllib import urlencode import pynotify import subprocess base_url = "http://translation.babylon.com/post_post.php" def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def translate(term): if term[0] == '\xd7': params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) elif term[0] in ['\xd9','\xd8']: params = urlencode(dict(lps=15, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) page = urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") def get_x_selection(): """Get X selection""" proc = subprocess.Popen(['xsel', ], stdout=subprocess.PIPE) x_selection = proc.stdout.read() proc.stdout.close() proc.wait() return x_selection if __name__ == "__main__": if not pynotify.init("Basics"): sys.exit(1) term = str(get_x_selection()) if len(term) < 1: n = pynotify.Notification('No text selected', '') else: translation = translate(term) n = pynotify.Notification(term, '\n' + translation) if not n.show(): print "Failed to send notification" sys.exit(1) |
קוד: |
#!/usr/bin/env python # -*- coding: utf-8 - # # Pytrans is a frontend to babylon # Version 1.3 # Change from 1.1: using pynotify to send a system notification instead of just printing the result. Requires pynotify # Change from 1.2: using xsel in-line so no need for complex activation line - just run the script. also added notification when no text is selected. from urllib2 import urlopen from urllib import urlencode import pynotify import subprocess base_url = "http://translation.babylon.com/post_post.php" def strip(s): intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def translate(term): if term[0] == '\xd7': params = urlencode(dict(lps=14, lpt=0, mytextarea1=term)) elif term[0] in ['\xd9','\xd8']: params = urlencode(dict(lps=15, lpt=14, mytextarea1=term)) else: params = urlencode(dict(lps=0, lpt=14, mytextarea1=term)) page = urlopen(base_url, params).read() return strip(page).replace("\n", '').replace(" ", "\n").replace(" ", "") def get_x_selection(): """Get X selection""" proc = subprocess.Popen(['xsel', ], stdout=subprocess.PIPE) x_selection = proc.stdout.read() proc.stdout.close() proc.wait() return x_selection if __name__ == "__main__": if not pynotify.init("Basics"): sys.exit(1) term = str(get_x_selection()) if len(term) < 1: n = pynotify.Notification('No text selected', '') else: translation = translate(term) n = pynotify.Notification(term, '\n' + translation) if not n.show(): print "Failed to send notification" sys.exit(1) |
ציטוט: |
. הצמידו את הפקודה ~/.pytrans.py לצירוף מקשים (כגון Ctrl+d) בהגדרות מנהל החלונות שלכם (קומפיז, מטסיטי וכו') |
Anonymous : | ||
אפשר בבקשה הסבר לזה:
איך עושים את זה באבונטו 8.10? תודה |
קוד: |
~/.pytrans.py |
קוד: |
~/.pytrans.py |
קוד: |
python ~/.pytrans.py |
ציטוט: |
ארעה שגיאה בהפעלת "pytrans.py":
כשלון בהרצת תהליך ילד "pytrans.py" (No such file or directory). |